如何像WhatsApp一样对Android通知进行分组?

37

我不知道如何将两个或多个通知分组成一个,然后显示一条消息,例如“您有两条新消息”。


1
文档中提到:注意:如果您的应用程序发送了四个或更多通知并且没有指定组,则系统会在Android 7.0及更高版本上自动将它们分组。 https://developer.android.com/training/notify-user/group - Wini
4个回答

33

从下面的代码中需要注意的步骤。

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());
        }
}

为不同的通知分配不同的 NOTIFICATION_IDs。


2
我已经在FcmMessagingService类中的onMessageRecieved内部类中编写了构造器的代码。我能够收到所有详细的通知,但看起来这些通知来自不同的应用程序。 - Nazim ch

1

为了完整的逻辑,请考虑查看我的答案。我使用了共享偏好和广播接收器的逻辑,因为我需要将每个用户消息分组成一个单独的消息,并在活动通知中可见。由于只有针对api级别23进行定位才能获得活动通知,这对我没有任何帮助。所以我决定写一些简单的逻辑。如果您感兴趣,请在此处查看。

https://dev59.com/LGIi5IYBdhLWcg3w_QmG#38079241


0

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

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

  1. 更新或创建NotificationCompat.Builder对象
  2. 从中构建一个通知对象
  3. 使用先前使用的相同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 文档中关于堆叠通知的部分https://developer.android.com/training/wearables/notifications/stacks.html


8
没问题。我发现API很令人困惑:NotificationCompatBuilder.setGroup()方法来自Wearable API。它的目的是在可穿戴设备上管理通知组,但它也会影响手机。但大多数人正在寻找类似Gmail-Whatsapp的手机通知组。答案是:您必须使用notificationIDs自行更新通知,并在每个通知之间保存数据,以便计算“摘要”通知 - 再次由您构建并使用相同的ID发送以替换先前的“非摘要”通知。 - John

-2
你可以使用setGroup方法并将groupId字符串作为参数传递来将所有通知堆叠到单个组中。

builer.setGroup("GROUP ID STRING" ) ;

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());

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接