【问题标题】:Android Notifications Not Stacking API 24Android 通知未堆叠 API 24
【发布时间】:2018-01-06 12:05:07
【问题描述】:

我正在尝试堆叠来自应用的通知,以便它们显示为 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) 的容器。并为该通知使用零 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);
}

【问题讨论】:

    标签: android mobile notifications android-notifications


    【解决方案1】:

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

    请注意,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.
    

    【讨论】:

    • inboxstyle 不错,但我希望将多个通知分组到一个列表中,但该列表是可扩展的,并且该列表中的每个通知都可以单独点击
    • 好的,我明白了,让我编辑我的答案来合并这个。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多