0

I'm able to send push notifications from console.firebase.google however, when I click on the notification that has been sent I want to be redirected to Main2Activity instead of MainActivity, I am able to achieve this when the app is in the foreground but not in the background, appreciate any help.

public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "FCM Service";
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    Intent detailIntentForToday = new Intent(this, Main2Activity.class);
    TaskStackBuilder taskStackBuilder = 
    TaskStackBuilder.create(getApplicationContext());
    taskStackBuilder.addNextIntentWithParentStack(detailIntentForToday);
    PendingIntent resultPendingIntent = taskStackBuilder
            .getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
    Notification notification = new Notification.Builder(this)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle(getString(R.string.app_name))
            .setAutoCancel(true)
            .setPriority(Notification.PRIORITY_MAX)
            .setDefaults(Notification.DEFAULT_VIBRATE)
            .setContentIntent(resultPendingIntent)
            .setContentText("Text")
            .build();
    NotificationManager notificationManager = (NotificationManager) 
    getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(0, notification);
    }
}
Omar Salah
  • 15
  • 7

1 Answers1

0

Try to set setContentIntent() in Notification Builder Your code will be like

public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "FCM Service";
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    Intent detailIntentForToday = new Intent(this, Main2Activity.class);
    TaskStackBuilder taskStackBuilder = 
    TaskStackBuilder.create(getApplicationContext());
    taskStackBuilder.addNextIntentWithParentStack(detailIntentForToday);
    PendingIntent resultPendingIntent = taskStackBuilder
            .getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
    Notification notification = new Notification.Builder(this)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle(getString(R.string.app_name))
            .setContentIntent(createContentIntent())
            .setAutoCancel(true)
            .setPriority(Notification.PRIORITY_MAX)
            .setDefaults(Notification.DEFAULT_VIBRATE)
            .setContentIntent(resultPendingIntent)
            .setContentText("Text")
            .build();
    NotificationManager notificationManager = (NotificationManager) 
    getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(0, notification);
    }
}

private PendingIntent createContentIntent() {
        Intent openUI = new Intent(this,Main2Activity.class);
        openUI.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        return PendingIntent.getActivity(
                this, REQUEST_CODE, openUI, PendingIntent.FLAG_CANCEL_CURRENT);
    }
Mohamed Saber
  • 832
  • 1
  • 10
  • 16
  • I'm sorry what do you mean by setContentInfo(createIntentToMainActivity), I don't get it, also what is mService, also createContentIntent methid, where should I call this method – Omar Salah Mar 04 '18 at 10:33