[英]Android: 2 Different Notifications with different IDs and texts shows the same text
如前所述,當設備收到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);
日志中沒有異常,也沒有崩潰。
如果要一次顯示2條通知,則應使用不同的值調用showMessageIntent上的setData。
如果你只改變的額外Intent
傳遞給PendingIndent
,則默認情況下它會重復使用最初的Intent
,而不是更新的演員-中通PendingIntent.FLAG_UPDATE_CURRENT作為最后一個參數PendingIntent.getActivity
以確保您的額外更新。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.