通知中的setgroup()不起作用

时间:2016-03-17 11:12:46

标签: android notifications

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

 // 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);

当我运行应用程序并发送通知消息时,它们不会显示在组中。 有人可以解释一下我需要改变什么吗?

2 个答案:

答案 0 :(得分:26)

您必须在创建自定义通知之前创建通知。就像这样:

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。

然后创建您的子通知,组值 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());

答案 1 :(得分:1)

替换

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

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

因为您正在使用NotificationCompat