我正在开发即时消息应用,并希望在收到消息时显示通知。
收到第一条消息后,会弹出通知,状态栏中会出现一个图标,如预期的那样。
在第一条消息之后,我更新状态栏中反映的通知,但通知不会再次弹出。
我已经尝试过多项内容,例如更改通知ID,但这不起作用,因为我只需要一个通知,即更新和显示,而不是几个通知。
我现在被困住了,请告知。
编辑:添加了代码。
if (messageNotifications.isEmpty()) {
return null;
}
int messageCount = 0;
for (MessageNotification messageNotification : messageNotifications) {
messageCount += messageNotification.getCount();
}
MessageNotification message = messageNotifications.get(messageNotifications.size() - 1);
boolean showText = false;
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(application);
notificationBuilder.setContentTitle("Secure Messenger");
notificationBuilder.setContentText("Secure Message");
notificationBuilder.setTicker(getText(message, showText));
notificationBuilder.setSmallIcon(getSmallIcon());
notificationBuilder.setLargeIcon(getLargeIcon(message));
notificationBuilder.setColor(ColorManager.getInstance().getAccountPainter().getAccountMainColor(message.getAccount()));
notificationBuilder.setStyle(getStyle(message, messageCount, showText));
notificationBuilder.setContentIntent(getIntent(message));
notificationBuilder.setCategory(NotificationCompat.CATEGORY_MESSAGE);
notificationBuilder.setPriority(NotificationCompat.PRIORITY_MAX);
notificationBuilder.setOnlyAlertOnce(false);
NotificationManager.addEffects(notificationBuilder, messageItem);
return notificationBuilder.build();
private PendingIntent getIntent(MessageNotification message) {
Intent i = new Intent();
i.setAction(Constants.ACTION_SPECIFIC_CHAT);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
i.addFlags(Notification.FLAG_ONGOING_EVENT);
i.addFlags(Notification.FLAG_NO_CLEAR);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.putExtra("com.xabber.android.data.user", message.getUser().toString());
i.putExtra("com.xabber.android.data.account", message.getAccount().toString());
i.setData((Uri.parse("custom://" + System.currentTimeMillis())));
return PendingIntent.getActivity(application, UNIQUE_REQUEST_CODE++, i, PendingIntent.FLAG_CANCEL_CURRENT);
}