通知单击显示唯一数据问题

时间:2015-08-06 11:12:52

标签: android android-notifications

Not able to identify id for item clicked on notification用于唯一ID,效果很好,现在用于同一个甚至不同的ID,当我点击通知时,我会获得唯一的发票ID,我将传递给网络服务以获取其发票详细信息,即使它为该id提取数据,itemActivity页面仅显示以前的详细信息,我将如何使用新内容更新页面?

发送通知代码

public class SampleSchedulingService extends IntentService {
    public SampleSchedulingService() {
        super("SchedulingService");
    }
    List<GetReminder> newReminderList;
    int invoiceId=0;
    String remMes;
    InvoiceData1 data1;
    int  InvM_Id;

    public static final String TAG = "Scheduling Demo";
    // An ID used to post the notification.
    public static int NOTIFICATION_ID = 1;
    private NotificationManager mNotificationManager;
    NotificationCompat.Builder builder;

    @Override
    protected void onHandleIntent(Intent intent) {
        // BEGIN_INCLUDE(service_onhandle)
        // The URL from which to fetch content.
        Log.d("MyService", "About to execute MyTask");
     //    
 newReminderList=WebService.invokeGetReminderWS("GetReminder",41);

        if(newReminderList!=null){
         for(int i=0;i<newReminderList.size();i++) {
                         sendNotification(newReminderList.get(i).getRemMessage(),newReminderList.get(i).getInvM_Id());
        }
      }
        // Release the wake lock provided by the BroadcastReceiver.
        SampleAlarmReceiver.completeWakefulIntent(intent);
        // END_INCLUDE(service_onhandle)
    }
    // Post a notification indicating whether a doodle was found.
    private void sendNotification(String msg, int invM_id) {

        try {
        Intent notificationIntent = new Intent(this, ItemActivity.class);
        notificationIntent.setAction(Intent.ACTION_MAIN);
        notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);
        data1=WebService.InvoiceDetailForExeedDiscount1(invM_id);
        notificationIntent.putExtra("invoiceList", data1);
        notificationIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        PendingIntent contentIntent = PendingIntent.getActivity(this, NOTIFICATION_ID, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        mNotificationManager = (NotificationManager)
               this.getSystemService(Context.NOTIFICATION_SERVICE);

        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(this)
        .setSmallIcon(R.drawable.ic_launcher)
        .setContentTitle(getString(R.string.invoice_alert))
        .setStyle(new NotificationCompat.BigTextStyle()
        .bigText(msg))
        .setContentText(msg);

        mBuilder.setContentIntent(contentIntent);
        mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
        NOTIFICATION_ID++;}
         catch (IOException e) {

        } catch (XmlPullParserException e) {

        }

    }

}

和itemActivity我正在读取像

这样的数据
public class ItemActivity extends Activity implements View.OnClickListener {
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
       final boolean customTitleSupported =
                requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        setContentView(R.layout.itemlist);
       if(customTitleSupported){
            getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,R.layout.item);
       }
       InvoiceData1 invoiceList = (InvoiceData1) getIntent().getSerializableExtra("invoiceList");
}

就像当我们退出应用程序并点击第二个通知时,它只是温和地显示前一个的发票详细信息,而且没有调用来获取点击的数据。

1 个答案:

答案 0 :(得分:1)

当您在FLAG_ACTIVITY_SINGLE_TOP中使用PendingIntent标记时,您还必须在onNewIntenet()中实施ItemActivity方法以正确处理此启动模式。根据{{​​3}}:

  

这是为将launchMode设置为&#34; singleTop&#34;在   他们的包,或者如果客户端使用FLAG_ACTIVITY_SINGLE_TOP标志   当调用startActivity(Intent)时。在任何一种情况下,当活动   在活动堆栈的顶部而不是新的重新启动   正在启动的活动的实例,将调用onNewIntent()   在具有用于重新启动的Intent的现有实例上   它

     

在收到新意图之前,活动将始终暂停,所以   你可以指望在这个方法之后调用onResume()。

     

请注意,getIntent()仍会返回原始的Intent。您可以使用   setIntent(Intent)将它更新为这个新的Intent。

所以将一些代码从onResume()移动并限制onNewIntent()实施到单setIntent()次调用就足够了。