getToken resolves to null if called too early #1286
Labels
platform: ios
plugin: messaging
FCM only - ( messaging() ) - do not use for Notifications
Workflow: Waiting for User Response
Blocked waiting for user response.
Milestone
Background
messaging.getToken
returns aPromise<string>
, which should resolve to a valid FCM token OR reject with an error (eg. timeout).getToken
on iOS looks something like this:Issue
When
getToken
gets called very early during the first launch of the app, it may resolve with null-ish values, sinceinstanceID.token
wasn't retrieved by FCM yet. This may lead to unexpected behaviour, since the promise resolves (not rejects) with a null value.Expected behaviour
Reproduction
I'm experiencing this issue on a production app, where our usage something like this (using redux-saga)
This bug is paticularly difficult to spot since it only happens in release builds, perhaps due to the fact that dev mode builds take longer to boot, delaying the call to
getToken
?Dirty & Temporary Fixes Until Resolution
await sleep(T)
) the call togetToken
in the first launch makes the problem go away, and getToken resolves as it should.Suggested Solution
Use
instanceIDWithHandler
from the Firebase SDK instead of accessing the token directly.Quoting the FCM docs:
Environment
Application Target Platform:
Verified on iOS, didn't check on Android
Development Operating System:
macOS
Build Tools:
Xcode 9.4
React Native
version:0.55.4
React Native Firebase
Version:4.2.x
Firebase
Module:messaging
Are you using
typescript
?Yes
I'd be happy to submit a pull request with the suggested solution if a contributor would review this issue and confirm this is really the reason for the bug.
The text was updated successfully, but these errors were encountered: