如何像whatsapp一样对Android通知进行分组?因为 setGroup 不适合我

Ers*_*han 7 notifications android

我目前正在奥利奥和棒棒糖设备上进行测试。到目前为止我所做的:

final static String GROUP_KEY_NOTIFY = "group_key_notify";
int notificationId0 = 100;
int notificationId1 = 101;
int notificationId2 = 102;
int notificationId3 = 103;

NotificationCompat.Builder builderSummary =
        new NotificationCompat.Builder(this)
                .setSmallIcon(android.R.drawable.ic_dialog_info)
                .setContentTitle("A Bundle Example")
                .setContentText("You have 3 new messages")
                .setGroup(GROUP_KEY_NOTIFY)
                .setGroupSummary(true);

NotificationCompat.Builder builder1 =
        new NotificationCompat.Builder(this)
                .setSmallIcon(android.R.drawable.ic_dialog_info)
                .setContentTitle("New Message")
                .setContentText("You have a new message from Kassidy")
                .setGroup(GROUP_KEY_NOTIFY);

NotificationCompat.Builder builder2 =
        new NotificationCompat.Builder(this)
                .setSmallIcon(android.R.drawable.ic_dialog_info)
                .setContentTitle("New Message")
                .setContentText("You have a new message from Caitlyn")
                .setGroup(GROUP_KEY_NOTIFY);

NotificationCompat.Builder builder3 =
        new NotificationCompat.Builder(this)
                .setSmallIcon(android.R.drawable.ic_dialog_info)
                .setContentTitle("New Message")
                .setContentText("You have a new message from Jason")
                .setGroup(GROUP_KEY_NOTIFY);

NotificationManager notifyMgr =
        (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

notifyMgr.notify(notificationId1, builder1.build());
notifyMgr.notify(notificationId2, builder2.build());
notifyMgr.notify(notificationId3, builder3.build());
notifyMgr.notify(notificationId0, builderSummary.build());
Run Code Online (Sandbox Code Playgroud)

我注意到,如果有 4 个或更多通知发生,那么它们会捆绑在一起,但是当少于 4 个通知时,它们不会捆绑在 N 以上的 Android 设备中。我阅读了文档并按照他们所说的使用 setGroup 方法并为summaryNotification创建单独的通知对象。但对我来说没有任何工作。

Ami*_*gid 3

您可以使用此链接作为创建捆绑通知的参考。

例子:

String GROUP_KEY_WORK_EMAIL = "com.android.example.WORK_EMAIL";

Notification newMessageNotification = new 
NotificationCompat.Builder(MainActivity.this, CHANNEL_ID)
    .setSmallIcon(R.drawable.new_mail)
    .setContentTitle(emailObject.getSenderName())
    .setContentText(emailObject.getSubject())
    .setLargeIcon(emailObject.getSenderAvatar())
    .setGroup(GROUP_KEY_WORK_EMAIL)
    .build();

Notification summaryNotification =
new NotificationCompat.Builder(MainActivity.this, CHANNEL_ID)
    .setContentTitle(emailObject.getSummary())
    //set content text to support devices running API level < 24
    .setContentText("Two new messages")
    .setSmallIcon(R.drawable.ic_notify_summary_status)
    //build summary info into InboxStyle template
    .setStyle(new NotificationCompat.InboxStyle()
            .addLine("Alex Faarborg  Check this out")
            .addLine("Jeff Chang    Launch Party")
            .setBigContentTitle("2 new messages")
            .setSummaryText("janedoe@example.com"))
    //specify which group this notification belongs to
    .setGroup(GROUP_KEY_WORK_EMAIL)
    //set this notification as the summary for the group
    .setGroupSummary(true)
    .build();

NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
notificationManager.notify(emailNotificationId1, newMessageNotification);
notificationManager.notify(SUMMARY_ID, summaryNotification);
Run Code Online (Sandbox Code Playgroud)

  • 如何获取 addLine 文本?如何获取之前的通知消息并将其添加到 addLine 方法中?我应该将其保存在sharedPref 上吗? (2认同)