Notification Click display unique data issue

2019-08-05 09:21发布

问题:

Not able to identify id for item clicked on notification was for unique id which is working out well,now for the same even with different id when i click on notifications i get unique invoice id ,which i am passing to a webservice to get its invoice details,even though its fetching data for that id,the itemActivity page is showing previous details only,how will i update the page with new contents ?

Send Notification code is

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) {

        }

    }

}

and itemActivity i am reading data like

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");
}

its like when we come out of the application and click on second notification,its just blandly showing the invoice details of previous one,der is no call going on to fetch data for clicked on.

回答1:

As you are using FLAG_ACTIVITY_SINGLE_TOP flag in your PendingIntent you must also implement onNewIntenet() method in ItemActivity to properly handle this launch mode. As per documentation:

This is called for activities that set launchMode to "singleTop" in their package, or if a client used the FLAG_ACTIVITY_SINGLE_TOP flag when calling startActivity(Intent). In either case, when the activity is re-launched while at the top of the activity stack instead of a new instance of the activity being started, onNewIntent() will be called on the existing instance with the Intent that was used to re-launch it.

An activity will always be paused before receiving a new intent, so you can count on onResume() being called after this method.

Note that getIntent() still returns the original Intent. You can use setIntent(Intent) to update it to this new Intent.

so it should be sufficient to move some code from your onResume() and limit onNewIntent() implementation to single setIntent() call.