Android Notifications Not Stacking API 24

时间:2018-01-06 02:59:06

标签: android mobile notifications android-notifications

我正在尝试从应用中堆叠通知,以便它们显示为分组为gmail等应用。以下代码每次都会在通知栏中生成新通知。

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

更新: 我找到了它,结果是为了使用setGroup("MY SUPER DUPER GROUP")将消息分组到堆栈中,您首先必须发送一个通知,作为其他具有setGroupSummary(true)的容器的容器。并为该通知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);
}

1 个答案:

答案 0 :(得分:0)

我不太确定你的用例是什么,例如对不同的事件有单独的通知,或捆绑它们。对于后者,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.