firebase-android-sdk
firebase-android-sdk copied to clipboard
notification_open never reported if onCreate of MainActivity was not called
- Android Studio version: Android Studio Chipmunk | 2021.2.1
- Firebase Component: Firebase Cloud Messaging
- Component version: 23.0.5
Problems
My app has only one MainActivity
, and it's singleTask.
When I received the notification and tap it to bring my app to foreground, the MainActivity
was not recreated and its onCreate
method was not called.
So FcmLifecycleCallbacks
had no chance to report the notification_open event.
Steps to reproduce:
- Make the
MainActivity
singleTask, and don't callfinish
, just send it to background - Enable Firebase Analytics debug, and open the DebugView
- Push a notification to the device
- Make sure we can see the notification_receive event in the DebugView
- Tap the notification to open the App
- notification_open event won't show in the DebugView
Relevant Code:
FcmLifecycleCallbacks
code below will never be called
@Override
public void onActivityCreated(Activity createdActivity, Bundle instanceState) {
Intent startingIntent = createdActivity.getIntent();
if (startingIntent == null || !seenIntents.add(startingIntent)) {
// already seen (and logged) this, no need to go any further.
return;
}
if (VERSION.SDK_INT <= VERSION_CODES.N_MR1) {
// On Android 7.1 and lower Bundle unparceling is not thread safe. Wait to log notification
// open after Activity.onCreate() has completed to try to avoid race conditions with other
// code that may be trying to access the Intent extras Bundle in onCreate() on a different
// thread.
new Handler(Looper.getMainLooper()).post(() -> logNotificationOpen(startingIntent));
} else {
logNotificationOpen(startingIntent);
}
}
I found a few problems with this issue:
- I couldn't figure out how to label this issue, so I've labeled it for a human to triage. Hang tight.
- This issue does not seem to follow the issue template. Make sure you provide all the required information.
Thanks for reporting, @kevin-zqw. I used the Firebase quickstart, and simulated the issue with the steps you've provided, however I'm able to receive the notification_receive
, and notification_open
upon tapping the notification. Could you provide a minimal repro of your issue so I can investigate this further? Thanks!
Hi @argzdev
I just managed to reproduce this bug with firebase quick start
project, here is the repo:
https://github.com/kevin-zqw/messaging
- Remove all Kotlin codes
- Set
java/MainActivity
to the main activity - Setup with my firebase settings
Test result:
Received both notification_receive
and notification_open
- Add
android:launchMode="singleTask"
toMainActivity
Test result:
-
App is running in the background Received only the
notification_receive
, nonotification_open
-
Quit the app completely Received both
notification_receive
andnotification_open
The notification_open
will only be reported when MainActivity
was created by tapping the notification.
Last but not least, the singleTask
mode is very important to my game app, because it has only one single & big GameActivity, I can't recreate it when user tap the notification and the app is running in the background.
Thanks!
Thanks for the detailed explanation, @kevin-zqw! I overlooked the android:launchMode="singleTask"
part, that's why I wasn't able to reproduce the issue at the first try.
Looking at the Android documentations.
However, if the target task already has an existing instance of the activity at the top of its stack, that instance will receive the new intent (in an onNewIntent() call); a new instance is not created.
Thank you for reporting this, but I can't say though if this is a bug or intended behavior, since if a new instance is not created due to the behavior of singleTask
then onActivityCreated
would definitely not get called for new intents, and logNotificationOpen
won't be invoked.
However, I do think this is something that needs to be supported in the future. That being said, I'll notify our engineers and see what we can do here.
This appears to be a bug as it's intended that opening a notification should log notification_open, if enabled. Unfortunately ActivityLifecycleCallbacks doesn't have a callback for onNewIntent(), so the FCM SDK would not be able to log notification open in that way. There may be some other ways to try to handle this, but even if we decide on a fix, I don't know how long it might take to be included in a release.
In the meantime, I think that you should be able to work around this by copying logNotificationOpen() from FcmLifecycleCallbacks and calling it in your Activity's onNewIntent().
https://github.com/firebase/firebase-android-sdk/blob/a283019216f4987fd4fbb806141d3540f28f53c3/firebase-messaging/src/main/java/com/google/firebase/messaging/FcmLifecycleCallbacks.java#L79
@argzdev @gsakakihara
Thanks so much! Hope Firebase will fix this bug in the future.
In the mean time, as @gsakakihara said, I have solved this issue in my App by calling logNotificationOpen()
in my MainActivity's onNewIntent().
A quick update. We're looking into seeing if we can make notification logging more seamless in cases like this, but there's a good chance that it could take some time and may not work on existing versions, so for now the best choice is probably to continue calling logNotificationOpen() in onNewIntent().