notification_open never reported if onCreate of MainActivity was not called
See original GitHub issue- 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);
}
}
Issue Analytics
- State:
- Created a year ago
- Comments:7 (5 by maintainers)
Top Results From Across the Web
Go to an Activity After Opening Push Notification
It is called from onCreate under MainActivity . See my edited answer for the code. – jumpingmaniac. Oct 26, 2018 at 18:10.
Read more >Application's onCreate is never being called, if the app is re ...
During normal circumstance, Application's onCreate will always be called before entry point Activity's onCreate. public class MainActivity extends ...
Read more >App lifecycle - .NET MAUI | Microsoft Learn
NET MAUI raises cross-platform lifecycle events when an app transitions between its different execution states.
Read more >Activity - Android Developers
onCreate (Bundle) is where you initialize your activity. ... and unregister it in onStop() when the user no longer sees what you are...
Read more >Understanding the Android Application Class
@Override public void onCreate() { super.onCreate(); // Required initialization logic here! } // Called by the system when the device configuration changes ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@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().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