我遇到了一个问题,无论用户触摸什么通知,我总是收到相同的通知,即使它被取消了。
用户触摸的第一个通知是唯一会传递给我的待定意图的通知,直到设备重新启动(然后第一个触摸的通知成为新的加利福尼亚酒店通知)。
这是我构建通知的方式:
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
.
我非常怀疑这是我的测试设备的问题,但如果它有帮助;我正在使用带有 KitKat 4.4.4 的 2013 Nexus 7 和 Dalvik 运行时(不是 ART)构建 KTU84P。