如何对像whatsapp这样的android通知进行分组?

如何对像whatsapp这样的android通知进行分组?,android,notifications,stack,google-cloud-messaging,grouping,Android,Notifications,Stack,Google Cloud Messaging,Grouping,我不知道如何将两个或多个通知分组为一个通知,并显示类似“You have two new messages”的消息。您需要创建通知,以便通过调用NotificationManager.notify(ID,notification)使用通知ID更新通知 需要创建以下步骤来更新通知: 更新或创建NotificationCompat.Builder对象 从中生成通知对象 使用之前使用的相同ID发出通知 android开发者文档中的一个示例: mNotificationManager = (Notifi

我不知道如何将两个或多个通知分组为一个通知,并显示类似“You have two new messages”的消息。

您需要创建通知,以便通过调用
NotificationManager.notify(ID,notification)
使用通知ID更新通知

需要创建以下步骤来更新通知:

  • 更新或创建
    NotificationCompat.Builder
    对象
  • 从中生成通知对象
  • 使用之前使用的相同ID发出通知
  • android开发者文档中的一个示例:

    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());
    ...
    
    另请参见Android文档中的堆叠通知

    以下代码中需要注意的步骤

    NotificationCompat.Builder:contains the UI specification and action information
    NotificationCompat.Builder.build() :used to create notification (Which returns Notification object)
    Notification.InboxStyle: used to group the notifications belongs to same ID
    NotificationManager.notify():to issue the notification.
    
    使用以下代码创建通知并将其分组。在按钮单击中包含该功能

    private final int NOTIFICATION_ID = 237;
    private static int value = 0;
    Notification.InboxStyle inboxStyle = new Notification.InboxStyle();
    Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.push_notify_icon);
    public void buttonClicked(View v)
    {
            value ++;
            if(v.getId() == R.id.btnCreateNotify){
                NotificationManager nManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
                Notification.Builder builder = new Notification.Builder(this);            
                builder.setContentTitle("Lanes");
                builder.setContentText("Notification from Lanes"+value);
                builder.setSmallIcon(R.drawable.ic_launcher);
                builder.setLargeIcon(bitmap);
                builder.setAutoCancel(true);
                inboxStyle.setBigContentTitle("Enter Content Text");
                inboxStyle.addLine("hi events "+value);
                builder.setStyle(inboxStyle);
                nManager.notify("App Name",NOTIFICATION_ID,builder.build());
            }
    }
    

    对于单独的通知,分配不同的NoTIFICTIONIDS.

    < P>作为完整的逻辑,请考虑检查我的答案。我使用共享偏好和广播接收器的逻辑,将每个用户消息分组为单一的,并看到主动通知。因为只有通过API级别23,才能得到有效的通知。通知,这对我一点帮助都没有。所以我决定写一些轻微的逻辑。如果你愿意,在这里检查一下


    您可以使用
    setGroup
    方法将所有通知堆叠到一个组中,并将您的groupId字符串作为参数传递

    builer.setGroup(“组ID字符串”)


    正确的。我发现API令人困惑:NotificationCompatingBuilder.setGroup()方法来自可穿戴API。它的目的是管理可穿戴设备上的通知组,尽管它也会影响手机。但大多数人都在电话里寻找Gmail Whastapp之类的群组。答案是:您必须自己使用NotificationID更新通知。您必须在每次通知之间保存数据,以便能够计算“摘要”通知,这同样取决于您使用相同的ID生成和发送以替换以前的“非摘要”通知。我在
    FcmMessagingService
    类中为生成器编写了代码。在一个内部类中
    onMessageRecieved
    。我能够详细接收所有通知,但它看起来像是从不同的应用程序接收到的通知。文档说明:如果你的应用程序发送四个或更多通知,但没有指定组,系统会在Android 7.0及更高版本上自动将它们分组。
    NotificationManager nManager = (NotificationManager) 
    getSystemService(NOTIFICATION_SERVICE);
    Notification.Builder builder = new Notification.Builder(this);            
    builder.setContentTitle("Lanes");
    builder.setGroup("GROUP_ID_STRING");
    builder.setContentText("Notification from Lanes"+value);
    builder.setSmallIcon(R.drawable.ic_launcher);
    builder.setLargeIcon(bitmap);
    builder.setAutoCancel(true);
    inboxStyle.setBigContentTitle("Enter Content Text");
    inboxStyle.addLine("hi events "+value);
    builder.setStyle(inboxStyle);
    nManager.notify("App Name",NOTIFICATION_ID,builder.build());