我不知道如何将两个或多个通知分组成一个,然后显示一条消息,例如“您有两条新消息”。
我不知道如何将两个或多个通知分组成一个,然后显示一条消息,例如“您有两条新消息”。
从下面的代码中需要注意的步骤。
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。
FcmMessagingService
类中的onMessageRecieved
内部类中编写了构造器的代码。我能够收到所有详细的通知,但看起来这些通知来自不同的应用程序。 - Nazim ch为了完整的逻辑,请考虑查看我的答案。我使用了共享偏好和广播接收器的逻辑,因为我需要将每个用户消息分组成一个单独的消息,并在活动通知中可见。由于只有针对api级别23进行定位才能获得活动通知,这对我没有任何帮助。所以我决定写一些简单的逻辑。如果您感兴趣,请在此处查看。
您需要创建通知,以便通过调用NotificationManager.notify(ID, notification)
更新通知ID。
需要创建以下步骤来更新通知:
NotificationCompat.Builder
对象以下是从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
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());