安卓:分组通知和总结仍在4.4以下单独列出通知

2023-09-06 01:39:35 作者:任性是放纵网名靑春旳骄傲ら

我要实现对Android Wear 堆叠通知要做到这一点我创建1总结的通知并为每个项的N个别通知。我只想总结要在手机上显示。这是我的code:

 私人无效showNotifications(){
    NotificationManager notificationManager =(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
    showNotification1(notificationManager);
    showNotification2(notificationManager);
    showGroupSummaryNotification(notificationManager);
}

私人无效showNotification1(NotificationManager notificationManager){
    showSingleNotification(notificationManager,标题1,消息1,1);
}

私人无效showNotification2(NotificationManager notificationManager){
    showSingleNotification(notificationManager,标题2,消息2,2);
}

保护无效showSingleNotification(NotificationManager notificationManager,
                                      串题,
                                      字符串消息,
                                      INT notificationId){
    NotificationCompat.Builder建设者=新NotificationCompat.Builder(本);
    builder.setContentTitle(标题)
            .setContentText(消息)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setGroupSummary(假)
            .setGroup(「本集团」);
    通知通知= builder.build();
    notificationManager.notify(notificationId,通知);
}

私人无效showGroupSummaryNotification(NotificationManager notificationManager){
    NotificationCompat.Builder建设者=新NotificationCompat.Builder(本);
    builder.setContentTitle(虚拟内容标题)
            .setContentText(虚拟内容的文字)
            .setStyle(新NotificationCompat.InboxStyle()
                    .addLine(1号线)
                    .addLine(2号线)
                    .setSummaryText(收件箱摘要文本)
                    .setBigContentTitle(大内容标题))
            .setNumber(2)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setCategory(Notification.CATEGORY_EVENT)
            .setGroupSummary(真)
            .setGroup(「本集团」);
    通知通知= builder.build();
    notificationManager.notify(123456,通知);
}
 

这只是正常工作在Android 5.1中,只有摘要显示在手机的通知栏:

不过,在Android 4.4还显示个别通知1和2:

在上Android Wear这两种情况的通知显示在一个堆栈,如所希望的。如何使Android 4.4系统只显示在通知栏中的摘要通知?

解决方案

通过使用固定该

  NotificationManagerCompat notificationManager = NotificationManagerCompat.from(本);
 
按查询结果分组 SQL 分组和汇总分析

而不是

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

和与NotificationManagerCompat更换NotificationManager相应方法签名。

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?

解决方案

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.