所以我在GcmListenerService中收到通知,并尝试像这样显示它们:
private void showNotification(String msg, String messageId, String patternIn) {
NotificationCompat.Builder builder =
new NotificationCompat.Builder(this)
.setSmallIcon(getNotificationIcon())
.setAutoCancel(true)
.setContentTitle(getString(R.string.app_name))
.setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
.setSound(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.cha_ching))
.setContentText(msg);
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
int pushId = sharedPreferences.getInt(KEY_UNIQUE_PUSH_ID, 1);
pushId++;
sharedPreferences.edit().putInt(KEY_UNIQUE_PUSH_ID, pushId).apply();
Intent resultIntent = new Intent(this, SplashActivity.class);
resultIntent.putExtra(PARAM_PUSH_MESSAGE_ID, messageId);
resultIntent.putExtra(PARAM_PUSH_PATTERN_ID, patternIn);
resultIntent.putExtra(PARAM_PUSH_ID, pushId);
PendingIntent resultPendingIntent =
PendingIntent.getActivity(
this,
pushId,
resultIntent,
PendingIntent.FLAG_UPDATE_CURRENT
);
builder.setContentIntent(resultPendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notification = builder.build();
notificationManager.notify(pushId, notification);
这在大多数情况下工作正常。但是有一种情况是它不起作用。所以:
- 杀死应用
- 发送 2 条通知
- 单击第一个 ->它将打开活动
- 单击第二个 ->它不会打开活动<- 这是一个问题。
设备: 三星银河S5安卓6.0.1
非常感谢任何帮助:)
在您描述的情况下,打开第一个Notification
将创建一个新任务(因为应用程序未运行),并在该任务中启动SplashActivity
。当您现在单击第二个Notification
时,它只会将现有任务向前推进,而不会启动另一个SplashActivity
实例。这是启动作为任务"根"Activity
Activity
时的标准行为。
要获得您想要的行为,您应该让Notification
启动NotificationActivity
(这不是您的SplashActivity
)。在NotificationActivity.onCreate()
中,您可以启动SplashActivity
并将Intent
传递给它。然后打电话给finish()
onCreate()
,以确保您的NotificationActivity
消失。
通过添加以下行进行修复:
resultIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);