Search code examples
androidandroid-intentandroid-pendingintent

Second notification is not working


I try to create multiple notifications. If click on the notification, it will link to another Activity. After following code, it's create two lines of notification. But when I click to the first line of notification, it's not working. Only second works.

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 to find my mistake. Thank you so much.


Solution

  • You should create the PendingIntent with different request codes. Change this:

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

    To this:

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

    Note that I've changed the 2nd argument to your loop index (i) instead of 0. Intents are not created if you use the same arguments but rather they use a previous intent with the same arguments.

    This should solve your problem.