Android 通知不堆叠 API 24

标签 android mobile notifications android-notifications

我正在尝试堆叠来自应用程序的通知,以便它们显示为应用程序分组,例如 gmail。以下代码每次都在通知栏中生成一个新通知。

android 文档 https://developer.android.com/guide/topics/ui/notifiers/notifications.html讨论使用 setGroup 的通知 channel 组。但 NotificationChannelGroup 仅适用于 API 26。该功能适用​​于我的手机 (API 24) 的其他应用程序,因此必须有办法使通知堆栈。有谁知道我怎样才能做到这一点?

更新: 我找到了它,结果是为了使用 setGroup("MY SUPER DUPER GROUP") 将消息分组到堆栈中,您首先必须发送一个通知,作为其他具有 设置组摘要(真)。并为该通知使用零 ID nmng.notify("CROWMAIL", 0, sum) 这样,如果先前的摘要已被删除,它将创建一个新的,但如果已经存在,则它不会有任何影响。

更新的工作代码:

NotificationManagerCompat nmng = NotificationManagerCompat.from(context);
Message[] msgs = folder.getMessagesByUID(a.data.uidnext, uidnext-1);
Notification sum = new Notification.Builder(context)
        .setSmallIcon(R.drawable.notif)
        .setGroupSummary(true)
        .setGroup("CROWMAIL")
        .build();
nmng.notify("CROWMAIL", 0, sum);
for(int i = 0; i < msgs.length; i++) {
    Notification n = new Notification.Builder(context)
            .setContentTitle(msgs[i].getFrom()[0].toString())
            .setContentText(msgs[i].getSubject())
            .setSmallIcon(R.drawable.notif)
            .setGroupSummary(false)
            .setGroup("CROWMAIL")
            .build();
    nmng.notify("CROWMAIL", previous+i, n);
}

最佳答案

我不太确定您的用例是什么,例如为不同的事件提供单独的通知,或者将它们 bundle 在一起。对于后者,Android 通过 InboxStyle 通知特别为小于 26 的 API 提供此功能。

请注意,InboxStyle 通知用于需要单个通知的情况,例如来自一个人的个别消息,或单行文本项的列表表示。

这是文档中的示例

NotificationCompat.Builder mBuilder =
    new NotificationCompat.Builder(this, CHANNEL_ID)
        .setSmallIcon(R.drawable.notification_icon)
        .setContentTitle("Event tracker")
        .setContentText("Events received");
NotificationCompat.InboxStyle inboxStyle =
        new NotificationCompat.InboxStyle();
String[] events = new String[6];
// Sets a title for the Inbox in expanded layout
inboxStyle.setBigContentTitle("Event tracker details:");
...
// Moves events into the expanded layout
for (int i=0; i < events.length; i++) {
    inboxStyle.addLine(events[i]);
}
// Moves the expanded layout object into the notification object.
mBuilder.setStyle(inboxStyle);
...
// Issue the notification here.

关于Android 通知不堆叠 API 24,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48123868/

相关文章:

到达位置时iOS本地通知

android - 我们如何在 android TextView 中显示英镑和欧元符号?

html - 为什么打开一个宽度为480px的网站会有空白?

java - 使用 Android Nanohttpd 轻量级服务器进行文件目录导航

javascript - 为什么jplayer在iphone上玩?

javascript - 如何获取 TouchSwipe 事件目标

notifications - Windows 8 Metro 应用程序 - toast 通知

android.app.RemoteServiceException : Bad notification posted from package. 无法扩展 RemoteViews:StatusBarNotification

java - Android:如何以用户的语言获取当前的星期几(星期一等)?

Javascript延迟关闭IOS上的数字键盘