Detect when the game is launched from a local notification?

Hey All,

How can we detect whether the game was launched from a local notification? I know it’s possible on iOS through Unity’s NotificationServices class, but it seems like we need to do this natively.

If I’m understanding this correctly, on Android we send a message via Intent’s putExtra function and we check for this message during the Activity’s onCreate function to determine if we were launched from a notification.

Is there a way to check for this message on UnityPlayerNativeActivity’s onCreate without subclassing UnityPlayerNativeActivity? Is there a better/easier way to determine if we were launched from a local notification?

Thanks,

On Android, you schedule a local notification using Android APIs. What happens as part of that notification is all up to you: you build the Notification object and set an Intent to it that will be invoked when clicking on the notification.

Since the whole thing is up to you to configure, there’s no “automatic” way of figuring out if the game was launched from the notification or not.

The easiest way, like you suggest, is to add a certain piece of data into the intent that will launch your game, and later on query that at runtime to understand if the game was launched from a notification or not.

A few other cases to note:

  • You should check that in the onCreate method, but if your game is already launched, clicking on the notification will not trigger the onCreate again (it is only called when your activity is created), so you should also handle cases where the game “returns to foreground” and not launched.
  • The intent that launched the game remains the same as long as the game is running (or, until it gets replaced by another intent). So, If you query the intent that launched the game every time returning to the game, you will get the same intent every time. In case you want to react to opening from a local notification only once, you should handle that case, otherwise every app resume will count as “opening from notification” (i hope my explanation was good enough).

Let me know if you were able to implement that or if you need any further help :slight_smile:

Thanks for the info! I’ve managed to add data to the intent and check on the data to see if we were launched from a notification, which covers most cases. However, I am trying to work around the first case you mentioned but can’t find a good solution.

Is it possible to know if the app is launched from a notification if the app is already running in the background without having the notification intent replace the existing?

Yes, it’s all up to you to implement a mechanism that willl catch that case as well.

For example, check if you were launched from a notification in the onResume method instead of onCreate (as it will be called every time the game returns to the foreground).

But, as i mentioned with issue #2, after launching from a notification, moving to the background and resuming will still count as launching from a notification (maybe that is what you want).

If that is not the required behavior, you will have to deal with that case as well.

1 Like

@liortal
I am new on unity. I put some extra data to my intent by using “putExtra”. On my intent mention “UnityPlayerNativeActivity.class” as the class in unity which will run by notification. My question is how could I check on UnityPlayerNativeActivity which my app run by tapping on notification or not?