Add getInitialNotification() on iOS #626
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
TL;DR
This PR adds the ability to call
getInitialNotification()
on react-native-onesignal.Description
The aim is to provide an API that is similar to React-Native's Linking and PushNotificationIOS modules.
Both of these stock modules allow to register event handlers, as well as providing the initial data that triggered the app launch.
From my point of view, triggering a
onOpened
event in the case of an app launch from a notification if flawed. You have to race to register your listener before the event is dispatched, and this means you're introducing a luck factor in your app initialization.I cannot rely on luck nor do I wish to race in an already complicated Javascript-timed environment.
So this PR is a neat way of doing things. You can call
getInitialNotification()
and get the data from the notification that opened the app. You can call it soon, late, it doesn't matter, it's available 24/7.It also comes in handy in case you need to store the initial notification or need to deal with it at a later point. No more redux needed to save your notification for later use.
This addresses #332, for real this time.
Android notice
I'm no Android developer, and I did not implement the function in the native Android module. If someone feels like Android could benefit from this as well, don't hesitate to contribute!
Trying to call
getInitialNotification()
on Android will simply console.log a message saying it's iOS only.Usage
Breaking changes
If the iOS app is opened by a notification, the native module will no longer trigger the
onOpened
callback.This choice has been made to prevent cases where the old way was working [insert random number here]% of the time. A working event trigger AND using
getInitialNotification()
could make you handle the initial notification twice. So we're avoiding this right of the bat.You can easily update your code by adding
getInitialNotification()
in addition to your listener, and basically give it the same callback. This ensures you handle the initial notification properly, and your listener handles any future notifications.This change is