Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Grouped notifications and summary still shown separately on 4.4 and below

I want to implement stacked notifications on Android Wear To do that I create 1 summary notification and N individual notifications for each "item". I want only the summary to be shown on the phone. Here's my code:

private void showNotifications() {
    NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
    showNotification1(notificationManager);
    showNotification2(notificationManager);
    showGroupSummaryNotification(notificationManager);
}

private void showNotification1(NotificationManager notificationManager) {
    showSingleNotification(notificationManager, "title 1", "message 1", 1);
}

private void showNotification2(NotificationManager notificationManager) {
    showSingleNotification(notificationManager, "title 2", "message 2", 2);
}

protected void showSingleNotification(NotificationManager notificationManager,
                                      String title,
                                      String message,
                                      int notificationId) {
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
    builder.setContentTitle(title)
            .setContentText(message)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setGroupSummary(false)
            .setGroup("group");
    Notification notification = builder.build();
    notificationManager.notify(notificationId, notification);
}

private void showGroupSummaryNotification(NotificationManager notificationManager) {
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
    builder.setContentTitle("Dummy content title")
            .setContentText("Dummy content text")
            .setStyle(new NotificationCompat.InboxStyle()
                    .addLine("Line 1")
                    .addLine("Line 2")
                    .setSummaryText("Inbox summary text")
                    .setBigContentTitle("Big content title"))
            .setNumber(2)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setCategory(Notification.CATEGORY_EVENT)
            .setGroupSummary(true)
            .setGroup("group");
    Notification notification = builder.build();
    notificationManager.notify(123456, notification);
}

This works just fine on Android 5.1, only the summary is shown in the phone's notification bar:

enter image description here

But on Android 4.4 it also shows individual notifications 1 and 2:

enter image description here

In both cases notifications on Android Wear are shown in a stack, as desired. How do I make Android 4.4 only show the summary notification in the notification bar?

like image 839
Anton Cherkashyn Avatar asked Jul 17 '15 19:07

Anton Cherkashyn


People also ask

How do I stop notifications from stacking on Android?

To find it, launch the Settings menu on your phone, select Apps and notifications, and choose Notifications. Toggle the feature off and your notifications will start streaming in.

How do I change my current notifications on Android?

To set up a notification so it can be updated, issue it with a notification ID by calling NotificationManager. notify(ID, notification) . To update this notification once you've issued it, update or create a NotificationCompat.


1 Answers

Fixed this by using

NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);

instead of

NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);

and replacing NotificationManager with NotificationManagerCompat in corresponding method signatures.

like image 143
Anton Cherkashyn Avatar answered Oct 23 '22 23:10

Anton Cherkashyn