次のコードを使用して、BroadcastReceiver 内に通知を作成しています:
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(ns);
int icon = R.drawable.ic_stat_notification;
CharSequence tickerText = "New Notification";
long when = System.currentTimeMillis();
Notification notification = new Notification(icon, tickerText, when);
notification.defaults |= Notification.DEFAULT_VIBRATE;
long[] vibrate = {0,100,200,200,200,200};
notification.vibrate = vibrate;
notification.flags |= Notification.FLAG_AUTO_CANCEL;
CharSequence contentTitle = "Title";
CharSequence contentText = "Text";
Intent notificationIntent = new Intent(context, NotificationActivity.class);
notificationIntent.putExtra(Global.INTENT_EXTRA_FOO_ID, foo_id);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
int mynotification_id = 1;
mNotificationManager.notify(mynotification_id, notification);
通知をクリックすると、NotificationActivityが開き、Activity内でIntent-Bundleからfoo_idを取得できます(例:1)
ただし、別の通知がトリガーされ、それを再度クリックすると、アクティビティは Intent-Bundle から「古い」値 (1) を受け取ります。clear() を使用してバンドルをクリアしようとしましたが、同じ結果になります。コードに何か問題があると思います。
ベストアンサー1
保留中のインテンスに同じリクエスト コードを送信しています。これを変更してください:
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
に:
PendingIntent contentIntent = PendingIntent.getActivity(context, UNIQUE_INT_PER_CALL, notificationIntent, 0);
同じパラメータを送信した場合、インテントは作成されません。再利用されます。