FCM Data message is not navigating to desired fragment when app is in background

岁酱吖の 提交于 2020-07-10 03:17:25

问题


I have an app with a somewhat complex flow. There is a launcher activity (SplashActivity), then a MainActivity, which has five fragments, then in the fifth fragment, there is the DesiredActivity which has three fragments, one of them being the DesiredFragment. When the app is in foreground and we click on the push notification (data message type), the DesiredFragment opens, no issues there, but when the app is in background, the MainActivity opens, instead of the DesiredFragment, on click of the push notification.

Also, when in debug mode, the DesiredFragment opens without any issues, when the app is in background. The issue arises in normal run and not in debug mode (According to my research, this generally happens in case of race conditions, but there are no threads, async tasks, etc involved in my code.

Can anyone help me with what am I doing wrong?

This is the manifest file entry for DesiredActivity:

<activity 
        android:name=".activities.DesiredActivity"
        android:parentActivityName=".activities.MainActivity"
        android:exported="true">
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value=".activities.MainActivity" />
</activity>

This is the code for onMessageRecieved in FirebaseMessagingServices. (There is no issue with NotificationBuilder aand NotificationManager)

        Intent intent = new Intent(this, Appointments.class);
        backIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
        intent.putExtra("comingFromPushFragOpen","RequestedAppointment");


        TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
        stackBuilder.addParentStack(Appointments.class);
        stackBuilder.addNextIntent(intent);

        PendingIntent contentIntent = stackBuilder.getPendingIntent(3,PendingIntent.FLAG_ONE_SHOT); 

        mBuilder.setContentIntent(contentIntent);
        notificationManager.notify(3, mBuilder.build());

Code in onCreate of DesiredActivity, we use a viewpager to load the DesiredFragment here:

 if (getIntent().hasExtra("comingFromPushFragOpen") && getIntent().getStringExtra("comingFromPushFragOpen").equalsIgnoreCase("RequestedAppointment"))
        appointmentsViewpager.setCurrentItem(1);

Again, the push notification is navigating to the DesiredFragment without any issue when the app is in foreground, the issue arises wheh the app is in background, it may have to do something with the way I am handling the intent and the pendingintent.

Any help would be appreciated.

来源:https://stackoverflow.com/questions/62446449/fcm-data-message-is-not-navigating-to-desired-fragment-when-app-is-in-background

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!