当通过单击通知打开应用程序时,FCM 不会再次打开 activity
FCM not opening activity again when app opened by clicking notification
Intent i = new Intent(this, MainActivity.class);
i.addFlags(Intent.FLAG_FROM_BACKGROUND);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, i, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_ONE_SHOT| PendingIntent.FLAG_IMMUTABLE);
NotificationCompat.Builder builder = (NotificationCompat.Builder) new NotificationCompat.Builder(this).setAutoCancel(true).setContentTitle(title).setContentText(body).setSmallIcon(R.drawable.atlantic_applogo).setLargeIcon(icon).setDefaults(Notification.DEFAULT_ALL).setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(0, builder.build());
当应用程序从应用程序图标打开时,它工作完美,但当应用程序从通知打开时,如果应用程序在前台,则单击新通知时没有任何反应。
OP的解决方案:
i.setPackage(null);
i.setFlags(Intent.FLAG_FROM_BACKGROUND | Intent.FLAG_RECEIVER_NO_ABORT );
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, i,
PendingIntent.FLAG_UPDATE_CURRENT |
PendingIntent.FLAG_ONE_SHOT |
PendingIntent.FLAG_IMMUTABLE);
Intent i = new Intent(this, MainActivity.class);
i.addFlags(Intent.FLAG_FROM_BACKGROUND);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, i, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_ONE_SHOT| PendingIntent.FLAG_IMMUTABLE);
NotificationCompat.Builder builder = (NotificationCompat.Builder) new NotificationCompat.Builder(this).setAutoCancel(true).setContentTitle(title).setContentText(body).setSmallIcon(R.drawable.atlantic_applogo).setLargeIcon(icon).setDefaults(Notification.DEFAULT_ALL).setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(0, builder.build());
当应用程序从应用程序图标打开时,它工作完美,但当应用程序从通知打开时,如果应用程序在前台,则单击新通知时没有任何反应。
OP的解决方案:
i.setPackage(null);
i.setFlags(Intent.FLAG_FROM_BACKGROUND | Intent.FLAG_RECEIVER_NO_ABORT );
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, i,
PendingIntent.FLAG_UPDATE_CURRENT |
PendingIntent.FLAG_ONE_SHOT |
PendingIntent.FLAG_IMMUTABLE);