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

被刻印的时光 ゝ 提交于 2019-12-18 11:17:18

问题


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:

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

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?


回答1:


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.




回答2:


You Just Remove showSingleNotification method and replace

notificationManager.notify(123456, notification); 

with

notificationManager.notify(123456, builder); 

and its work fine.



来源:https://stackoverflow.com/questions/31483772/android-grouped-notifications-and-summary-still-shown-separately-on-4-4-and-bel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!