0

This class won't to handle multiple GCM messages and show them on notification bar,currently handle only one, how can make it to handle multiple messages?

public class GCMIntentService extends GcmListenerService {
    private static final String TAG = "GCMIntentService";
    int notifyid = 0;

    @Override
    public void onMessageReceived(String from, Bundle data) {
        String message = data.getString("message");
        Log.d(TAG, "from:" + from);
        Log.d(TAG, "message:" + message);
        sendNotification(message);
    }

    private void sendNotification(String message) {
        Intent intent = new Intent(this, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, notifyid, intent, PendingIntent.FLAG_ONE_SHOT);
        Uri defaultSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        if(Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT) {
            NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.logo)
                .setContentTitle("New Messsage")
                .setContentText(message)
                .setAutoCancel(true)
                .setSound(defaultSound)
                .setContentIntent(pendingIntent);
            int numMessages = 0;
            notificationBuilder.setContentText(message).setNumber(++numMessages);
            NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
            notificationManager.notify(notifyid, notificationBuilder.build());
        }
    }
}
Antti29
  • 2,953
  • 12
  • 34
  • 36
mohammed02
  • 85
  • 5
  • since you are using the same notification id i.e 0, all the notifications which will be generated are overlapped and hence you can see only single notification. You should use different notifyid if you want different notifications. – Gautam Jan 31 '16 at 08:15

1 Answers1

0

From the documentation:

public void notify (int id, Notification notification)

Post a notification to be shown in the status bar. If a notification with the same id has already been posted by your application and has not yet been canceled, it will be replaced by the updated information.

notifyid never appears to change in the code you've posted, which is why you only see a single notification.

Michael
  • 57,169
  • 9
  • 80
  • 125
  • So do I have to move NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.notify(notifyid, notificationBuilder.build()); inside this method^^^^ ??? – mohammed02 Jan 31 '16 at 11:14
  • I'm not sure I understand your comment. What you need to do is generate a different ID for each call to `notify`. You could do that e.g. by [using an `AtomicInteger`](http://stackoverflow.com/questions/25713157/generate-int-unique-id-as-android-notification-id), or by generating a random number. – Michael Jan 31 '16 at 11:22