android 通知中的setgroup()不起作用

qv7cva1a  于 2023-06-04  发布在  Android
关注(0)|答案(3)|浏览(323)

我特灵创建通知组,这是我的代码:

// Build the notification, setting the group appropriately
 Notification notif = new NotificationCompat.Builder(getApplicationContext())
          .setContentTitle("New mail from " + 1)
          .setContentText("cv")
          .setSmallIcon(R.drawable.rh_logo)
          .setStyle(new NotificationCompat.InboxStyle()
            .addLine("Alex Faaborg   Check this out")
            .addLine("Jeff Chang   Launch Party")
            .setBigContentTitle("2 new messages")
            .setSummaryText("johndoe@gmail.com"))
          .setGroup(GROUP_KEY_EMAILS)
          .setGroupSummary(true)
          .build();

 // Issue the notification


 NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
 notificationManager.notify(++NOTIFICATION_ID, notif);

当我运行应用程序,并发送通知消息,他们不显示在组中。有人能告诉我我需要改变什么吗?

lnxxn5zx

lnxxn5zx1#

创建自定义通知之前,必须先创建群组通知。就像这样:

NotificationCompat.Builder groupBuilder =
            new NotificationCompat.Builder(context)
                    .setContentTitle(title)
                    .setContentText(content)
                    .setGroupSummary(true)
                    .setGroup("GROUP_1")
                    .setStyle(new NotificationCompat.BigTextStyle().bigText(content))
                    .setContentIntent(pendingIntent);

不要忘记setGroupSummary为true。
然后创建您的子通知,其中group值groupBuilder的值相同,这里是“GROUP_1”。

NotificationCompat.Builder builder =
            new NotificationCompat.Builder(context)
                    .setSmallIcon(R.drawable.ic_stat_communication_invert_colors_on)
                    .setContentTitle(title)
                    .setContentText(content)
                    .setGroup("GROUP_1")
                    .setStyle(new NotificationCompat.BigTextStyle().bigText(content))
                    .setContentIntent(pendingIntent)

最后使用NoticationManagerCompat通知他们。

NotificationManagerCompat manager = NotificationManagerCompat.from(context);
    manager.notify(GROUP_ID, groupBuilder.build());
    manager.notify(id, builder.build());
6ioyuze2

6ioyuze22#

更换

NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(++NOTIFICATION_ID, notif);

NotificationManagerCompat.from(mCtx).notify(++NOTIFICATION_ID,notif);

因为您正在使用NotificationCompat

nkhmeac6

nkhmeac63#

有一个奇怪的通知行为。如果添加自动取消通知组不起作用,如果添加setGroup或setGroupSummary。这行不通。删除所有这些为我解决了问题。基本上,你不需要提到任何组,它将自动组。测试上红米10 pr0,Poco x3 NFC,华为设备.

return new NotificationCompat.Builder(context, CHANNEL_ID)
            .setSmallIcon(R.drawable.ic_action_name)
            .setContentTitle(title)
            .setContentText(body)
            .setStyle(new NotificationCompat.BigTextStyle().bigText(body))
            .setPriority(NotificationCompat.PRIORITY_LOW);

相关问题