Android 7 上的分组通知行为

Behaviour of grouped notification on Android 7

我创建了几个这样的通知:

public class NotificationCreator {
    Context context;
    int c = 0;

    public NotificationCreator(final Context context) {
        this.context = context;
    }

    void create() {

        String text = "" + c  + " " + new Date().toGMTString();

        // Intent
        Intent intent = new Intent(context, SecondActivity.class);
        intent.putExtra(SecondActivity.KEY, text);
        Intent[] intents = new Intent[1];
        intents[0] = intent;
        PendingIntent pendingIntent = PendingIntent.getActivities(
                context,
                c,
                intents,
                PendingIntent.FLAG_CANCEL_CURRENT);

        // Build notification
        NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
        builder.setSmallIcon(R.drawable.notification);
        builder.setContentTitle("Test");
        builder.setContentText(text);
//        builder.setGroup("");
        builder.setContentIntent(pendingIntent);
        Notification notification = builder.build();

        // Send notification
        NotificationManager notificationManager = (NotificationManager)
                context.getSystemService(NOTIFICATION_SERVICE);

        notificationManager.notify(c, notification);

        c++;
    }
}

Android 7 的结果是:

系统已对所有通知进行分组。

当我在构建器上明确设置时:

 builder.setGroup("myGroup");

结果是:

通知分组,但全部单独显示,尽管都具有相同的组密钥。

  1. 这是预期的行为吗?

  2. 第一种情况(分组),能否判断用户点击分组通知时发生了什么?个人意图似乎被忽略了,只是打开了应用程序。

遇到了同样的问题,并通过 "Summary notification" 解决了这个问题。基本上,您创建一个包含所需组的常规通知,以及包含同一组的 "Summary notification" 和 setGroupSummary(true).

这里有更多内容https://blog.stylingandroid.com/nougat-bundled-notifications/