如前所述,当设备收到 2 个或更多通知时,我无法在通知中成功显示不同的消息。我已经尝试了一些我尝试过的解决方案,但它们没有奏效。我创建通知的代码如下:
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Intent showMessageIntent = new Intent(context, AdminMessageActivity.class);
showMessageIntent.putExtra(WorkspaceConstants.ADMIN_MESSAGE_KEY, message);
showMessageIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
int Id = new Random().nextInt();
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, showMessageIntent, 0);
String messageShort = (message.length() > 25) ? message.substring(0, 25) + "..." : message;
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle(context.getText(R.string.admin_message))
.setContentText(messageShort)
.setContentIntent(pendingIntent)
.setAutoCancel(true);
mNotificationManager.notify(Id, mBuilder.build());
我使用以下代码提取消息内容:
if(getIntent() != null && getIntent().getExtras() != null){
mMessageContent = getIntent().getExtras().getString(ADMIN_MESSAGE_KEY);
}
mMessage.setText(mMessageContent);
日志中没有异常,也没有崩溃。