【问题标题】:Showing multiple notification显示多个通知
【发布时间】:2015-11-09 11:15:45
【问题描述】:

我向用户发送多个通知,并使用不同的通知 ID 来创建和通知它们,并且所有通知只显示一个图标。一切都很完美。但是,当我滑动状态栏以查看通知时,它会显示单独的通知。

正如您在图片中看到的,我有两个单独的通知,而我喜欢像电报一样显示它们(2 条新消息)。 Android 是否有此功能,或者我必须使用检查以前通知的消息并计算未读通知并将其显示为 ContentText。

这是我的代码:

private final String GROUP_HEALTH = "HEALTH";

    Random random = new Random();
    int randInt = random.nextInt(9999 - 1000) + 1000;
    int mNotificationId = randInt;

    PendingIntent resultPendingIntent =
            PendingIntent.getActivity(
                    mContext, 0,intent,
                    PendingIntent.FLAG_CANCEL_CURRENT
            );

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(mContext);

    mBuilder.setAutoCancel(true)
            .setDefaults(Notification.DEFAULT_ALL)
            .setWhen(System.currentTimeMillis())
            .setSmallIcon(R.drawable.ic_launcher)
            .setTicker(mContext.getString(R.string.app_name))
            .setContentTitle(title)
            .setContentText(message)
            .setContentIntent(resultPendingIntent)
            .setGroup(GROUP_HEALTH)
            .setContentInfo(notifType);

    NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(mNotificationId, mBuilder.build());

【问题讨论】:

  • 请参考这个link或参考这个问题link
  • 感谢您的链接。我添加了.setGroup(GROUP_HEALTH),但没有任何区别,我仍然收到分开的消息!!!另外为了显示消息计数和摘要,我应该自己计算和总结。
  • 您能用您所做的更改更新您的代码吗?

标签: android notifications


【解决方案1】:

我猜每次这段代码运行时,它都会为每个通知生成随机 ID,使用常量 ID。

    int mNotificationId = 1;

是的,您应该检查未读消息,并自己显示该数量。

【讨论】:

  • 当我对所有通知都使用相同的ID时,如果有一个看不见的通知,系统不会通知新的,但旧的会替换为新的。
猜你喜欢
  • 2012-06-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多