【问题标题】:Android notifications grouping not workingAndroid通知分组不起作用
【发布时间】:2020-02-15 23:53:25
【问题描述】:

我正在尝试设置通知。

private fun sendNotification(id: Int, name: String) {
    val channelId = getString(R.string.default_notification_channel_id)
    val defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
    val notification = NotificationCompat.Builder(this, channelId)
        .setSmallIcon(R.drawable.ic_notification)
        .setContentTitle(getString(R.string.msg_new_ticket))
        .setContentText(name)
        .setGroup(GROUP_KEY)
        .setAutoCancel(true)
        .setSound(defaultSoundUri)
        .build()

    val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
    notificationManager.notify(id, notification)
}

通知可以很好地发送和接收,但不会自动分组。每次都使用唯一的id 调用该函数。在 Android 8 (API 26) 上进行测试。

任何想法请为什么分组不起作用?

更新

documentation

如果同一个应用发送四个或更多通知并且未指定 分组,系统会自动将它们分组在一起。

我已尝试发送 5 个通知,但即使定义了组,它们仍然没有分组。

【问题讨论】:

    标签: android kotlin android-notifications


    【解决方案1】:

    虽然我有java代码,只要稍作改动就可以了,你必须先为旧设备创建一个摘要通知,而新设备将只使用此代码中的“.setSummartText”...

    所以,总结通知:

                  Notification summaryNotification = new NotificationCompat.Builder(this, 
                 App.CHANNEL_1)
                .setSmallIcon(R.drawable.o)
                .setStyle(new NotificationCompat.InboxStyle()
                        .addLine(" Olicia : Hey!,what's the progress of the project")
                        .addLine("James : Bud,Check This Out ")
                        .setBigContentTitle("2 new messages")
                        .setSummaryText("Friends Group"))
                .setPriority(NotificationCompat.PRIORITY_LOW)
                .setGroup("example_group")
                .setGroupAlertBehavior(NotificationCompat.GROUP_ALERT_CHILDREN)
                .setGroupSummary(true)
                .build();
    

    然后添加其他通知

    最后,按以下顺序通知通知管理器

    {首先是摘要通知

    然后是所有其他通知}

    代码:

        notificationManagerCompat.notify(6, summaryNotification);
    
        notificationManagerCompat.notify(2, notification1);
    
        notificationManagerCompat.notify(3, notification2);
    
        notificationManagerCompat.notify(4,notification3);
    
        notificationManagerCompat.notify(5,notification4);
    

    由于一些奇怪的原因,android 需要几秒钟来对通知进行分组

    【讨论】:

    • 是的,我也需要几秒钟的时间来对通知进行分组。我以为我做错了什么,直到我读到这篇文章。我希望谷歌能解决这个问题,因为它会导致负面的用户体验
    猜你喜欢
    • 1970-01-01
    • 2019-09-26
    • 2018-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多