通过单击通知打开应用时,FCM不会再次打开活动

时间:2018-10-11 11:15:21

标签: android android-intent firebase-cloud-messaging android-pendingintent

    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());

从应用程序图标打开应用程序但从通知中打开应用程序时,它工作正常,如果单击新通知,则如果应用程序处于前台,则什么也不会发生。

1 个答案:

答案 0 :(得分:0)

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);