androidandroid-notificationsandroid-wear-notification

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?


Solution

  • 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.