简体   繁体   中英

How to know the position of notification in android

I am trying to display list of elements in notification bar. Now I have a set of notifications but every notification I am clicking takes me to same activity ie I was not able to store the position of notification.

Here is my code

    if (list != null && !list.isEmpty()) {
        for (int i = 0; i < list.size(); i++) {
            NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
                    context)
                    .setSmallIcon(R.drawable.ic_launcher)
                    .setContentTitle(
                            "Coupon Received From "
                                    + list.get(i).merchantName)
                    .setContentText(
                            "Coupon Value : "
                                    + list.get(i).couponValue)
                    .setAutoCancel(true)
                    .setTicker("Coupon alert message from Kluebook")
                    .setVibrate(new long[] { 100, 250, 100, 250 });

            Intent resultIntent = new Intent(context,
                    CouponDetailActivity.class);
            resultIntent.putExtra("couponid",
                    list.get(i).couponid);
            resultIntent.putExtra("position", list.get(i).i);
            resultIntent.putExtra("branchid",
                    list.get(i).issuedBranchid);
            PendingIntent resultPendingIntent = PendingIntent.getActivity(
                    context, 0, resultIntent,
                    PendingIntent.FLAG_UPDATE_CURRENT);
            mBuilder.setContentIntent(resultPendingIntent);
            NotificationManager mNotifyMgr = (NotificationManager) context
                    .getSystemService(context.NOTIFICATION_SERVICE);
            mNotifyMgr.notify(i + 1, mBuilder.build());

        }
    } else {
        System.out.println("+++++++++++ IT IS EMPTY +++++++++++++++ list");
      }
   }

If I get 5 notifications in notification bar, every notification takes me to first notification activity.

Since you are using the same context to create all the notifications' PendingIntent, I think all these notifications are taking you to the same activity (in your case CouponDetailActivity). So I would try creating different notifications with different Activity's context and pending intent and that should take you to your desired activity!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM