Show multiple notifications on receiving gcm notification

jihbvsdfu picture jihbvsdfu · Sep 27, 2012 · Viewed 8.1k times · Source

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);


    }}

Answer

SunnySonic picture SunnySonic · Sep 27, 2012

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.