Search code examples
androidandroid-intentfirebase-cloud-messagingandroid-pendingintent

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

It is working flawless when app is opened from app icon but when app is opened from notification, on clicking new notification nothing happens if app is in foreground.


Solution

  • OP's solution:

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