2

I am showing notification at the notification bar like this. I am getting those , but I cant show multiple notifications there,only one at a time. When a new one comes , previous one go. What shall be the problem?

 public void createNotificationRecever(Context context, String payload) {
        Toast.makeText(context, commentor  +  "commented on your post "   ,Toast.LENGTH_LONG).show();
        //New message received
        NotificationManager notificationManager = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);
        Notification notification = new Notification(R.drawable.flag,
                payload, System.currentTimeMillis());
        // Hide the notification after its selected
        notification.flags |= Notification.FLAG_AUTO_CANCEL;

        Intent intent = new Intent(context, MessageReceivedActivity.class);
        intent.putExtra("id", groupid);
        intent.putExtra("userid", text);
        intent.putExtra("cname", groupname);
        intent.putExtra("image", "");

        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
                intent, 0);
        notification.setLatestEventInfo(context, "Message",
                payload, pendingIntent);         
        notificationManager.notify(0, notification);


    }}
BenMorel
  • 34,448
  • 50
  • 182
  • 322
jihbvsdfu
  • 65
  • 1
  • 7

4 Answers4

5

Depending on how many Notifications you need there are several solutions. You could add an id that increments to your notification so it would have a different name and therefor wont replace the other one with the same id or if you need only two notifications max then just create a second notification with different names of the strings/variables you are using.

Have a look here for the ID increment:

Android: Managing Multiple Notifications

If you just need a second or third notification change your strings to something like this for example:

public void createNotificationRecever(Context context2, String payload2) {
    Toast.makeText(context2, commentor  +  "commented on your post "   ,Toast.LENGTH_LONG).show();
    //New message received
    NotificationManager notificationManager = (NotificationManager) context2
            .getSystemService(Context.NOTIFICATION_SERVICE);
    Notification notification2 = new Notification(R.drawable.flag,
            payload2, System.currentTimeMillis());
    // Hide the notification after its selected
    notification.flags |= Notification.FLAG_AUTO_CANCEL;

    Intent intent = new Intent(context2, MessageReceivedActivity.class);
    intent.putExtra("id", groupid2);
    intent.putExtra("userid", text2);
    intent.putExtra("cname", groupname2);
    intent.putExtra("image", "");

    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
            intent, 0);
    notification.setLatestEventInfo(context, "Message",
            payload, pendingIntent);         
    notificationManager.notify(0, notification2);


}}

I hope you get the gist and that it helps you.

Community
  • 1
  • 1
SunnySonic
  • 1,318
  • 11
  • 37
5

Use this code it will bring multiple notifications in list int NOTIFICATION_ID = 0; notificationManager.notify(NOTIFICATION_ID, notification2); NOTIFICATION_ID++

Narendra Kumar
  • 551
  • 5
  • 16
0

@SunnySonic, You need to use the latest stable release of "Android Support Library".

To download the latest stable release of "Android Support Libraray", Goto SDK Manager -> Extras - > click on Android Support Library and update it.

and goto build.gradle and under "dependencies", change the version.

dependencies {
    compile 'com.android.support:support-v4:22.1.1'  //<-change this
    compile files('libs/bolts-android-1.1.4.jar')
    compile files('libs/gcm.jar')

}
Sohail
  • 303
  • 1
  • 10
0

You can generate random number as NotificationId in notify method.

notificationManager.notify(generateRandom(), notificationBuilder.build());

public int generateRandom()
{
    Random rn = new Random();
    int n = maximum - minimum + 1;
    int i = rn.nextInt() % n;
    return  minimum + i;

}
Akash Bisariya
  • 3,855
  • 2
  • 30
  • 42