question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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:

  1. Make the MainActivity singleTask, and don’t call finish, just send it to background
  2. Enable Firebase Analytics debug, and open the DebugView
  3. Push a notification to the device
  4. Make sure we can see the notification_receive event in the DebugView
  5. Tap the notification to open the App
  6. 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:open
  • Created a year ago
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
kevin-zqwcommented, Jun 13, 2022

@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().

1reaction
gsakakiharacommented, Jun 10, 2022

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

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found