我的要求是,如果收到新通知,则该内容应添加到组中

时间:2019-08-13 09:20:31

标签: android android-notifications

我的要求很简单,如果通知到达,则应将其添加到现有通知组(如Whats App)中

 public void showNotifications(String title, String message) {
        int SUMMARY_ID = 0;
        String GROUP_KEY_WORK_EMAIL = "com.android.example.WORK_EMAIL";
        NotificationManager notificationManager = (NotificationManager) 
        getSystemService(NOTIFICATION_SERVICE);
        StatusBarNotification[] notifications = notificationManager.getActiveNotifications();

        String msg = message + String.valueOf(i++);
        NotificationCompat.Builder mBuilder = new 
  NotificationCompat.Builder(this, "MYNOTIF")
                .setSmallIcon(android.R.drawable.ic_dialog_email)
                .setContentTitle(title)
                .setContentText(msg)
                .setAutoCancel(true)
                .setDefaults(Notification.DEFAULT_SOUND);
        notificationManager.notify(notif.getID(), mBuilder.build());
}

此代码发送通知,但我想将其分组...  有什么建议吗?

1 个答案:

答案 0 :(得分:0)

您可以使用Notification Builder类的setGroup()方法。

val GROUP_KEY_WORK_EMAIL = "com.android.example.WORK_EMAIL"
val newMessageNotification = NotificationCompat.Builder(this@MainActivity, CHANNEL_ID)
        .setSmallIcon(R.drawable.new_mail)
        .setContentTitle(emailObject.getSenderName())
        .setContentText(emailObject.getSubject())
        .setLargeIcon(emailObject.getSenderAvatar())
        .setGroup(GROUP_KEY_WORK_EMAIL)
        .build()

您可以在这里https://developer.android.com/training/notify-user/group

获取更多信息