Update Notification for Multiple Messages

Below is my code that generates a notification when a new message arrives in my application,

code

 private static void generateNotification(Context context, String message) {
        int icon = R.drawable.ic_launcher;
        long when = System.currentTimeMillis();
        int counter = 0;
        counter++;

        NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
        Notification notification = new Notification(icon, message, when);

        String title = context.getString(R.string.app_name);

        Intent notificationIntent = new Intent(context, MessageActivity.class);
        // set intent so it does not start a new activity
        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
        notification.setLatestEventInfo(context, title, message, intent);
        notification.flags |= Notification.FLAG_AUTO_CANCEL;
        notification.defaults |= Notification.DEFAULT_SOUND;
        notification.defaults |= Notification.DEFAULT_VIBRATE;
        notificationManager.notify(counter, notification);

    }

When I receive several messages, there are several notifications.

eg

Suppose I get 3 new messages, then there are 3 separate notifications. But I want only one notification for several messages. When I receive multiple messages, the notification should be updated as shown in WhatsApp . How can i do this?

+4
source share
1 answer

, , JsonArray, . , , , _ID. , (.. ), 1 . Android-SDK NotificationCompat.InboxStyle( android.v4.support) Notification.InboxStyle InboxStyle.addLine .

+1

Source: https://habr.com/ru/post/1526288/


All Articles