Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 459
Yoo Avatar asked Sep 04 '11 08:09

Yoo


People also ask

Why are my notifications not showing?

Check if the notification appears in your Android notification bar or tray. Check that you have enabled notifications on your phone. Android Settings > Apps (then Manage Apps for some users) > Signal > Check on Show notifications. > Notifications > Enable message notifications.

Why are my notifications not showing up on Android?

Cause of Notifications Not Showing up on AndroidDo Not Disturb or Airplane Mode is on. Either system or app notifications are disabled. Power or data settings are preventing apps from retrieving notification alerts. Outdated apps or OS software can cause apps to freeze or crash and not deliver notifications.

How do I manage multiple notifications on Android?

Set the unique id to let Notification Manager knows this is a another notification instead of same notification. If you use the same unique Id for each notification, the Notification Manager will assume that is same notification and would replace the previous notification.


2 Answers

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.

like image 190
IncrediApp Avatar answered Nov 15 '22 08:11

IncrediApp


    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);
like image 20
Shivam Kumar Avatar answered Nov 15 '22 07:11

Shivam Kumar