ユーザーがどの通知に触れても、キャンセルされたにもかかわらず、常に同じ通知を受け取るという問題があります。
ユーザーが最初にタッチした通知は、デバイスが再起動されるまで保留中のインテントに配信される唯一の通知です (その後、最初にタッチされた通知が新しい 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));
どの通知に触れTouched notification ID: 1234
ても、触れた通知の ID が であっても、ログには常に表示されます56789
。
テストデバイスの問題であるとは思えませんが、役立つ場合があります。Dalvik ランタイム (ART ではない) Build KTU84P を搭載した KitKat 4.4.4 を搭載した 2013 Nexus 7 を使用しています。