如何分组 android 通知,如 whatsapp?
How to group android notifications like whatsapp?
我不知道如何将两个或多个通知合并为一个并显示 "You have two new messages" 之类的消息。
您需要创建通知,以便可以通过调用 NotificationManager.notify(ID, notification)
使用通知 ID 对其进行更新。
需要创建以下步骤来更新通知:
- 更新或创建
NotificationCompat.Builder
对象
- 从中构建一个 Notification 对象
- 使用您之前使用的相同 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
以下代码中需要注意的步骤。
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..
对于完整的逻辑,请考虑检查我的 answer.I 使用具有共享首选项和广播接收器的逻辑,因为我需要将每个用户消息分组为单个消息并且只能看到活动的 notifications.As瞄准 api 级别 23 你可以获得主动通知,它对我在 all.So 没有帮助,如果你愿意的话,我决定在这里写一些轻微的 logic.Check 它。
您可以使用 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());
我不知道如何将两个或多个通知合并为一个并显示 "You have two new messages" 之类的消息。
您需要创建通知,以便可以通过调用 NotificationManager.notify(ID, notification)
使用通知 ID 对其进行更新。
需要创建以下步骤来更新通知:
- 更新或创建
NotificationCompat.Builder
对象 - 从中构建一个 Notification 对象
- 使用您之前使用的相同 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
以下代码中需要注意的步骤。
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..
对于完整的逻辑,请考虑检查我的 answer.I 使用具有共享首选项和广播接收器的逻辑,因为我需要将每个用户消息分组为单个消息并且只能看到活动的 notifications.As瞄准 api 级别 23 你可以获得主动通知,它对我在 all.So 没有帮助,如果你愿意的话,我决定在这里写一些轻微的 logic.Check 它。
您可以使用 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());