Skip to content

notification_open never reported if onCreate of MainActivity was not called #3799

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
kevin-zqw opened this issue Jun 9, 2022 · 9 comments
Labels
api: messaging type: bug Something isn't working

Comments

@kevin-zqw
Copy link

  • 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);
    }
  }
@google-oss-bot
Copy link
Contributor

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.

@argzdev
Copy link
Contributor

argzdev commented Jun 9, 2022

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!

@kevin-zqw
Copy link
Author

kevin-zqw commented Jun 10, 2022

Hi @argzdev

I just managed to reproduce this bug with firebase quick start project, here is the repo:
https://github.com/kevin-zqw/messaging

Commit 1

  1. Remove all Kotlin codes
  2. Set java/MainActivity to the main activity
  3. Setup with my firebase settings

Test result:
Received both notification_receive and notification_open

Commit 2

  1. Add android:launchMode="singleTask" to MainActivity

Test result:

  1. App is running in the background
    Received only the notification_receive, no notification_open

  2. Quit the app completely
    Received both notification_receive and notification_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!

@argzdev
Copy link
Contributor

argzdev commented Jun 10, 2022

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.

@gsakakihara
Copy link
Contributor

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

@argzdev argzdev added the type: bug Something isn't working label Jun 10, 2022
@kevin-zqw
Copy link
Author

kevin-zqw commented 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().

@gsakakihara
Copy link
Contributor

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

@SteveBurkert
Copy link

SteveBurkert commented Jul 10, 2024

@gsakakihara

Hey there,

I don't want to open a new ticket for this, since my problem is kind of coming from the same source, the
FcmLifecycleCallbacks.

Just to add, the callbacks do not only not recognize onNewIntent calls, but they also do not recognize
if the same intent was already used for logging.

Use Case Example:

  1. LaunchActivity (which is called by fcm notification with data in Intent)
  2. SomeOtherActivity which is called from LaunchActivity

If the LaunchActivity blindly forwards it's Intent data to the "SomeOtherActivity" for any reasons,
the notification_open event will be tracked twice, because the FcmLifecycleCallbacks is invoked
two times.

This can easily be avoided, but was not easy to find. So this is more an additional info for you.

Maybe it's a good idea to check if the intent from onCreate is coming from an activity
with category.LAUNCHER Intent-filter first, before pushing the fcm event. But thats not
to me to decide.

Have a great day,
Cheers

@lehcar09
Copy link
Contributor

Hi @SteveBurkert, I opened a new ticket (#6091) referencing the issue you raised for better tracking. Moving forward, you can check on the issue #6091 for updates. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: messaging type: bug Something isn't working
Projects
None yet
Development

No branches or pull requests

6 participants