The second notification does not work

I am trying to create some notifications. If you click on a notification, it will link to another activity. After the following code, it creates two lines of notification. But when I click on the first line of the notification, it does not work.

for (int i = 0; i < missionName.size(); i++) { mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); final Notification notifyDetails = new Notification(R.drawable.icon, "Mission Completed, Click Me!", System.currentTimeMillis()); notifyDetails.defaults |= Notification.DEFAULT_SOUND; notifyDetails.defaults |= Notification.DEFAULT_VIBRATE; Context context = getApplicationContext(); CharSequence contentTitle = missionName.get(i) + " is completed"; CharSequence contentText = "Please click to view the mission"; Intent notifyIntent = new Intent(getApplicationContext(),MissionMap.class); notifyIntent.putExtra("missionName", missionName.get(i)); PendingIntent intent = PendingIntent.getActivity(ApplicationMenus.this, 0, notifyIntent, android.content.Intent.FLAG_ACTIVITY_NEW_TASK); notifyDetails.setLatestEventInfo(context, contentTitle, contentText, intent); mNotificationManager.notify(i, notifyDetails); } 

So please help me find my mistake. Thank you very much.

+4
source share
2 answers

You must create a PendingIntent with different request codes. Change this:

 PendingIntent intent = PendingIntent.getActivity(ApplicationMenus.this, 0, notifyIntent, android.content.Intent.FLAG_ACTIVITY_NEW_TASK); 

For this:

 PendingIntent intent = PendingIntent.getActivity(ApplicationMenus.this, i, notifyIntent, android.content.Intent.FLAG_ACTIVITY_NEW_TASK); 

Note that I changed the second argument to your loop index (i) instead of 0. Jobs are not created if you use the same arguments, but instead they use previous intentions with the same arguments.

This should solve your problem.

+17
source
  Random random = new Random(); int randomNumber = random.nextInt(9999 - 1000) + 1000; Intent resultIntent = new Intent(this, MainActivity.class); TaskStackBuilder TSB = TaskStackBuilder.create(this); TSB.addParentStack(MainActivity.class); TSB.addNextIntent(resultIntent); PendingIntent resultPendingIntent = TSB.getPendingIntent( randomNumber, PendingIntent.FLAG_UPDATE_CURRENT ); nb.setContentIntent(resultPendingIntent); 
0
source

All Articles