Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stacking Notifications does not work

I need to form a group of notifications. I made the example enter link description here

but my notice are not grouped. what did I do wrong?

my code:

private static int id =0;
    final static String GROUP_KEY_GUEST = "group_key_guest";


     private static void generateNotification(Context context, String message) {

            NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);

            Intent intent = new Intent(context,MyActivity.class);
            PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);

            Notification notification   = new NotificationCompat.Builder(context)
                    .setContentIntent(pendingIntent)
                    .setSmallIcon(R.drawable.ic_stat_gcm)
                    .setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_stat_gcm))
                    .setTicker("New Message")
                    .setWhen(System.currentTimeMillis())
                    .setAutoCancel(true)
                    .setContentTitle("Message")
                    .setContentText(message)
                    .setGroup(GROUP_KEY_GUEST)
                    .build();

            notificationManager.notify(id++, notification);
        }

method call:

 @Override
    protected void onMessage(Context context, Intent intent) {
        Log.d(TAG, "Поступило сообщение: " + intent.getExtras());
        String message = intent.getStringExtra("content");
        generateNotification(context, message);
    }

EDIT, full code: This is my service which I generate notifications

 package com.managment.pavel.managmentgradle;

    public class GCMIntentService extends GCMBaseIntentService {
        private static int id =0;
        final static String GROUP_KEY_GUEST = "group_key_guest";
        NotificationManagerCompat notificationManager;
        public GCMIntentService() {
            super(SENDER_ID);
         notificationManager = NotificationManagerCompat.from(getApplicationContext());
        }

        @Override
        protected void onMessage(Context context, Intent intent) {
            String message = intent.getStringExtra("content");
            generateNotification(context, message);
        }

        @Override
        protected void onError(Context context, String s) {
            Toast.makeText(context,"Error",Toast.LENGTH_LONG).show();
        }

        @Override
        protected void onRegistered(Context context, String registrationId) {
            ServerUtilities.register(context, registrationId);
        }

        @Override
        protected void onUnregistered(Context context, String registrationId) {
            ServerUtilities.unregister(context, registrationId);
        }

        private void generateNotification(Context context, String message) {
            Intent intent = new Intent(context,MyActivity.class);
            PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);

            Notification notification   = new NotificationCompat.Builder(context)
                    .setContentIntent(pendingIntent)
                    .setSmallIcon(R.drawable.ic_stat_gcm)
                    .setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_stat_gcm))
                    .setTicker("Новое сообщение")
                    .setWhen(System.currentTimeMillis())
                    .setAutoCancel(true)
                    .setContentTitle("Сообщение")
                    .setContentText(message)
                    .setGroup(GROUP_KEY_GUEST)
                    .build();

            notificationManager.notify(id++, notification);
        }
    }
like image 394
Pavel Petrashov Avatar asked Dec 05 '14 04:12

Pavel Petrashov


2 Answers

Try initialize NotificationManagerCompat in generateNotification if NotificationManagerCompat instance is null and remove initialization code form default constructor :

private void generateNotification(Context context, String message) {
    if(notificationManager==null){
       notificationManager = NotificationManagerCompat.from(context);
    }

    Intent intent = new Intent(context,MyActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);

    Notification notification   = new NotificationCompat.Builder(context)
    .setContentIntent(pendingIntent)
    .setSmallIcon(R.drawable.ic_stat_gcm)
    .setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_stat_gcm))
    .setTicker("Новое сообщение")
    .setWhen(System.currentTimeMillis())
    .setAutoCancel(true)
    .setContentTitle("Сообщение")
    .setContentText(message)
    .setGroup(GROUP_KEY_GUEST)
    .build();
    notificationManager.notify(id++, notification);
}
like image 29
Haresh Chhelana Avatar answered Sep 20 '22 06:09

Haresh Chhelana


You must to create a summary notification with details from the previous. It is this summary notification that is visible, not the previous. It's something like this:

private static int id =0;
private static int unread_notif = 0;
final static String GROUP_KEY_GUEST = "group_key_guest";

private static void generateNotification(Context context, String message) {

        NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);

        Intent intent = new Intent(context,MyActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);

        Notification notification   = new NotificationCompat.Builder(context)
                .setContentIntent(pendingIntent)
                .setSmallIcon(R.drawable.ic_stat_gcm)
                .setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_stat_gcm))
                .setTicker("New Message")
                .setWhen(System.currentTimeMillis())
                .setAutoCancel(true)
                .setContentTitle("Message")
                .setContentText(message)
                .setGroup(GROUP_KEY_GUEST)
                .build();

        notificationManager.notify(id++, notification);

        unread_notif++;

        if (unread_notif>1) {
           Notification summaryNotification = new NotificationCompat.Builder(mContext)
                .setContentTitle("Your summary message")
                .setSmallIcon(R.drawable.ic_small_icon)
                .setLargeIcon(largeIcon)
                .setStyle(new NotificationCompat.InboxStyle()
                    .addLine("Details about your first notification")
                    .addLine("Details about your second notification")
                .setBigContentTitle(Integer.toString(unread_notif)+" new notifications")
                .setSummaryText("More details in app"))
                .setGroup(GROUP_KEY_GUEST)
                .setGroupSummary(true)
                .build();

            notificationManager.notify(id++, summaryNotification);
        }
    }

Please see the documentation in Add a Summary Notification

like image 165
Ricardo Carmo Avatar answered Sep 23 '22 06:09

Ricardo Carmo