[英]Cancelled Notification being redelivered when other notification is touched. Android
我遇到一個問題,無論用戶觸摸了什么通知,即使被取消,我也總是收到相同的通知。
用戶觸摸的第一個通知是唯一會一直傳遞到我待定意圖的通知,直到設備重新啟動為止(然后第一個觸摸的通知將成為新的Hotel California通知)。
這是我構造通知的方式:
final Bundle extras = new Bundle();
final int notificationId = Integer.parseInt(payload.getObjectId());
extras.putInt(NOTIFICATION_ID, notificationId);
final Intent intent = new Intent(context,
PushNotificationLauncherActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtras(extras);
final NotificationCompat.Builder builder = new NotificationCompat.Builder(
context);
builder.setContentTitle(context.getString(R.string.app_name));
builder.setContentText(payload.getText());
builder.setSmallIcon(R.drawable.icon_launcher);
builder.setAutoCancel(true);
final PendingIntent pIntent = PendingIntent.getActivity(context, 0,
intent, 0, extras);
builder.setContentIntent(pIntent);
final Notification notification;
if (Build.VERSION.SDK_INT < 16) {
notification = builder.getNotification();
} else {
notification = builder.build();
}
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(notificationId, notification);
然后我在PushNotificationLauncherActivity
有這個:
final Bundle extras = getIntent().getExtras();
final int notificationId = extras
.getInt(SocialcastGoogleCloudMessageReceiver.NOTIFICATION_ID);
final NotificationManager notificationManager = (NotificationManager) this
.getSystemService(NOTIFICATION_SERVICE);
// please go away
notificationManager.cancel(notificationId);
Log.d(PushNotificationLauncherActivity.class.getSimpleName(),
"Touched notification ID: " + String.valueOf(notificationId));
無論我觸摸哪個通知,即使我觸摸的通知的ID為56789
,日志中也會始終顯示Touched notification ID: 1234
。
我高度懷疑這是我的測試設備存在的問題,但萬一有幫助, 我正在使用帶有KitKat 4.4.4的2013 Nexus 7和Dalvik運行時(不是ART)Build KTU84P。
在此處傳遞您的notificationId
作為request code
:
final PendingIntent pIntent = PendingIntent.getActivity(context, 0,
intent, 0, extras);
更改為:
final PendingIntent pIntent = PendingIntent.getActivity(context, notificationId,
intent, 0, extras);
比較兩個Intent
實例時,不比較包含的Bundles
。 因此,從android的角度來看,這兩個意圖是相同的。
有趣的是, reqCode
(在第二個參數PendingIntent.getActivity(...)
相比較。 這Intents
您的Intents
變得獨一無二。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.