Not able to identify id for item clicked on notification

十年热恋 提交于 2019-12-12 02:51:52

问题


I have multiple notifications, when I click on a single notification I need pass its invoice id which I'm getting from a webservice to other Activity and display its details.

The issue I'm facing is that the below code is giving me same invoice id for all notifications, I know something is wrong, but I wasn't able to figure out.

Please point out my mistake.

public class SampleSchedulingService extends IntentService {
    public SampleSchedulingService() {
        super("SchedulingService");
    }
    List<GetReminder> reminderList;
    int invoiceId=0;
    String remMes;
    InvoiceData1 data1;
    int  InvM_Id;
    public static final String TAG = "Scheduling Demo";
    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");
        reminderList = WebService.invokeGetReminderWS("GetReminder", 41);

        if(reminderList!=null){
            for(int i=0;i<reminderList.size();i++) {              sendNotification(reminderList.get(i).getRemMessage(),reminderList.get(i).getInvM_Id());
            }
        }
        // Release the wake lock provided by the BroadcastReceiver.
        SampleAlarmReceiver.completeWakefulIntent(intent);
        // END_INCLUDE(service_onhandle)
    }

    private void sendNotification(String msg, int invM_id) {

        try {
            Intent notificationIntent = new Intent(this, Result.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, 0, 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) {

        }

    }
}

I need to get different invm_id for each notification based on that I'm passing data to other result Activity.


回答1:


You are using the same PendingIntent with all your notifications, even if you think otherwise. This is actually clearly documented here:

If the creating application later re-retrieves the same kind of PendingIntent (same operation, same Intent action, data, categories, and components, and same flags), it will receive a PendingIntent representing the same token if that is still valid, and can thus call cancel() to remove it.

Because of this behavior, it is important to know when two Intents are considered to be the same for purposes of retrieving a PendingIntent. A common mistake people make is to create multiple PendingIntent objects with Intents that only vary in their "extra" contents, expecting to get a different PendingIntent each time. This does not happen.

To solve this you need to provide i.e. different requestCode, so insead of

PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
             notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

you should write

PendingIntent contentIntent = PendingIntent.getActivity(this, NOTIFICATION_ID,
             notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);



回答2:


The flag FLAG_UPDATE_CURRENT here:

PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent,
    PendingIntent.FLAG_UPDATE_CURRENT);

Flag indicating that if the described PendingIntent already exists, then keep it but replace its extra data with what is in this new Intent. For use with getActivity(Context, int, Intent, int), getBroadcast(Context, int, Intent, int), and getService(Context, int, Intent, int).

This can be used if you are creating intents where only the extras change, and don't care that any entities that received your previous PendingIntent will be able to launch it with your new extras even if they are not explicitly given to it.

Means that the last one you create will replace all the previous one. To avoid that, you must make them different according to the criteria that IntentFilter uses:

There are three Intent characteristics you can filter on: the action, data, and categories

You are using the same action and category, and don't specify data. It will not use whatever you put in Extras for filtering them. My suggestion that instead of this:

        notificationIntent.putExtra("invoiceList", data1);

You turn data1 somehow into a Uri, and Intent.setData() to store it.



来源:https://stackoverflow.com/questions/31836529/not-able-to-identify-id-for-item-clicked-on-notification

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!