如何对像whatsapp这样的android通知进行分组?因为 setGroup 对我不起作用 [英] How to group android notifications like whatsapp? as setGroup not work for me

查看:110
本文介绍了如何对像whatsapp这样的android通知进行分组?因为 setGroup 对我不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在 Oreo 和棒棒糖设备上进行测试.到目前为止我做了什么:

I am currently test on Oreo and lollipop devices. What I am done so far:

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

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

What I am noticing, if there is 4 or more notifications occurs then they are bundled together but when there are less than 4 notification they are not bundled in android device above N. I read the documentations and doing what they are saying like using setGroup method and make separate notification object for summaryNotification. But nothing gets work for me.

推荐答案

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

示例:

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

这篇关于如何对像whatsapp这样的android通知进行分组?因为 setGroup 对我不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆