How to group android notifications like whatsapp?

后端 未结 4 604
说谎
说谎 2020-12-02 17:19

I don´t know how to group two or more notifications into only one and show a message like \"You have two new messages\".

4条回答
  •  南笙
    南笙 (楼主)
    2020-12-02 17:55

    You need to create the notification so that it can be updated with a notification ID by calling NotificationManager.notify(ID, notification).

    The following steps need to be created to update the notification:

    1. Update or create a NotificationCompat.Builder object
    2. Build a Notification object from it
    3. Issue the Notification with the same ID you used previously

    An example taken from the android developer docs:

    mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    
    // Sets an ID for the notification, so it can be updated
    int notifyID = 1;
    
    mNotifyBuilder = new NotificationCompat.Builder(this)
        .setContentTitle("New Message")
        .setContentText("You've received new messages.")
        .setSmallIcon(R.drawable.ic_notify_status)
    numMessages = 0;
    
    // Start of a loop that processes data and then notifies the user
    ...
    mNotifyBuilder.setContentText(currentText).setNumber(++numMessages);
    
    // Because the ID remains unchanged, the existing notification is updated.
    mNotificationManager.notify(notifyID, mNotifyBuilder.build());
    ...
    

    Also see the Android docs on Stacking Notifications https://developer.android.com/training/wearables/notifications/stacks.html

提交回复
热议问题