React-native-firebase: Messaging getToken() always returns initialToken

Created on 20 Sep 2018  路  30Comments  路  Source: invertase/react-native-firebase

Issue

messaging.getToken() keeps returning the first token, even if the firebaseId has been deleted and a new token generated.

To Reproduce. In a onTokenRefresh callback, log out the parameter token and a messaging.getToken and compare. No matter how many times you delete the instanceId generating new tokens you'll always receive the first token.

Environment

  1. Application Target Platform:

Both

  1. Development Operating System:

mac High Sierra

  1. Build Tools:

xcode 9.4, Android Studio 28

  1. React Native version:

    "react-native": "0.55.4",

  2. React Native Firebase Version:

    "react-native-firebase": "^4.0.3",

  3. Firebase Module:

firebase-core:12.0.1
firebase-analytics:12.0.1
firebase-messaging:12.0.1

  1. Are you using typescript?

no

Needs Tests Bug iOS Messaging Needs Review

Most helpful comment

This can be really easily reproduced. On iOS:

const oldToken = await firebase.messaging().getToken();
await firebase.messaging().deleteToken();
const newToken = await firebase.messaging().getToken();
if (oldToken === newToken) {
    console.error('Token has not been refreshed');
} 

All 30 comments

@britianoates If I read this correctly it would only impact the case where the token changed during a single app instance, correct? Like if you manually delete the firebaseId for example. Is that something you do when a user signs out?

We are trying to chase down an issue on iOS where an FCMToken seems to stop working over the course of a couple days and I'm trying to determine if token refresh has anything to do with it. According to the Firebase docs a token refresh on it's own should be a rare event.

We had many users not seeing the notifications we were sending. It was difficult to track down what percentage or when notifications stopped arriving. What you are seeing sounds similar to what we just dealt with in prod. To fix our users we've released a version of the app that calls RequestPermission every app opening to make sure the APNS and fcmToken remain synced.

Calling to delete the firebase instanceId has been the only way we were able to recreate the failure to receive notifications. We do not delete the token as a part of normal app usage.

Interesting. Does it seem like that fixed the issue for you guys?

That sounds very similar to what we're seeing. We didn't notice this problem before our last release and in that release we added code to try and capture analytics events for how many users were denying notification permissions that had an early return if they had already enabled permissions. We also only request permission after a user signs in for the first time. Previously we had been calling requestPermission on every authenticated app opening.

We have not been in production long enough to be confident our fix covers all use cases. It did cover all scenarios we were able to fake using deleteToken on a dev screen, and the one device from prod we had access to.

Sounds like your solution was the opposite of ours. We only registered during the first run, and then the APNS and fcmToken seems to grow stale and we didn't have a mechanism to resync them. Our fix calls requestPermission on app start (Does not open modal, but does trigger a registration method. See my other ticket.)

I'm seeing the same problem. Users that have been receiving notifications for weeks suddenly stop receiving them. I can have them open the app, get the token returned by firebase, put that token into the firebase console and send a message. The console reports success but the user never receives the push. It appears that somehow the token is invalid yet firebase doesn't see it as invalid. This is a very serious problem as we have no way of knowing which users simply never receive their push notifications.

@jcharbo Are you also attempting to only call Firebase.requestPermissions() on a limited basis in your app?

I have the same issue as britianoates. messenging().getToken() only returns the initialToken. It only happens on iOS. On Android, it works fine, when the token change, messaging().getToken() return the new token.

This can be really easily reproduced. On iOS:

const oldToken = await firebase.messaging().getToken();
await firebase.messaging().deleteToken();
const newToken = await firebase.messaging().getToken();
if (oldToken === newToken) {
    console.error('Token has not been refreshed');
} 

So, guys, what is the solution of the problem ? Just remove
https://github.com/invertase/react-native-firebase/blob/a506d1e5b5c8293630ecd0c8bcdc9e42dcf7e7c2/ios/RNFirebase/messaging/RNFirebaseMessaging.m#L111
as @britianoates noted above? Is it right ?

Hello 馃憢, this issue has been automatically marked as stale because it has not had activity for quite some time. Has the issue been fixed, or does it still require the community's attention? This issue may be closed if no further activity occurs. Thank you for your contributions.

Has anyone experimented with adjusting the implementation of RNFirebaseMessaging.m?

Is this issue on the roadmap anytime soon? Is some more reproduction instructions or experimenting needed from the library users?

This can be really easily reproduced. On iOS:

const oldToken = await firebase.messaging().getToken();
await firebase.messaging().deleteToken();
const newToken = await firebase.messaging().getToken();
if (oldToken === newToken) {
    console.error('Token has not been refreshed');
} 

How does the firebase.messaging().deleteToken() work? I'm getting the error of firebase.messaging().deleteToken() is unsupported by the native Firebase SDKs

Can confirm that we're seeing this type of behaviour described above:

We are trying to chase down an issue on iOS where an FCMToken seems to stop working over the course of a couple days and I'm trying to determine if token refresh has anything to do with it.

We had many users not seeing the notifications we were sending. It was difficult to track down what percentage or when notifications stopped arriving.

I can have them open the app, get the token returned by firebase, put that token into the firebase console and send a message. The console reports success but the user never receives the push. It appears that somehow the token is invalid yet firebase doesn't see it as invalid. This is a very serious problem as we have no way of knowing which users simply never receive their push notifications.

I'm checking that tokens are part of topics using this API call: https://iid.googleapis.com/iid/info/?details=true&Authorizaton=

And it seems they are, we're looking into the solution that @britianoates described

To fix our users we've released a version of the app that calls RequestPermission every app opening to make sure the APNS and fcmToken remain synced.

@smathson did you solve this issue?

Any news on this? I am facing similar issue on iOS where a freshly retrieved FCM token is claimed to be not registered from FCM server when sending notification.

@liokm Are you get this error only after removing token and getting new one ?
I also have the same problem right now in production.

@liokm this bug report was for "react-native-firebase": "^4.0.3", when I joined the project it was on 4.3.x I didn't realise how behind the dependencies of the project I'm on were so trying out the latest 5.x.x release now. Will report back (we've moved crucial notifications to SMS in the meantime using Twilio).

@kholiavko-roman Actually my issue was a mix of two problems, one was that my app was transferred to another account and thus the team id and APNS key were invalid in FCM console and needed to be updated, otherwise "not registered" error is reported. Another problem is still waiting for feedback of logs. Have you tried to always requestPermission at start time anyway?

@liokm Yes, I do requestPermission on each time when app starting. And also I have onTokenRefreshListener in my app.
With my FCM key also all is okay, I rechecked this things.
And one more thing - I have the same problem with unregistered tokens on android.

@kholiavko-roman, how did you implement onTokenRefreshListener in you App? I did not find any API for this in react-native-firebase package. Thanks

@lisichka999
It calls onTokenRefresh

componentDidMount() {
 this.onRefreshToken = firebase
      .messaging()
      .onTokenRefresh(this.props.setDeviceId);
}

componentWillUnmount() {
    if (this.onRefreshToken) {
      this.onRefreshToken();
    }
  }

I'm having the same issue here. firebase 5.4.0. platform: iOS
I managed to get a new token by deleting the old one. I called firebase.messaging().deleteToken() before firebase.messaging().getToken();. And it did return me a fresh token. But that token turns out to be invalid and give me error NotRegistered during pushing notification. Any solution?

I am having the same issue
on RNFB 6.2.0 iOS
I can get the first token, but after deleting the token I keep getting the initial token.

What happened at the end???
I still can't get a new token on ios.

It's been awhile. I ended up working around this by saving the token after an earlier call on my side rather than calling for it since this is broken.

@britianoates

Would you please explain how you achieved what you're describing with some code.

I found the solution.

        messaging()
            .deleteToken(undefined,'*')
            .then(() => {
                RNRestart.Restart();

Use this to delete the token then restart the app with https://www.npmjs.com/package/react-native-restart now you're sure you get a new token :) I haven't tested this on android so I don't know if android also needs this if android doesn't have this problem just put a little if statement to do this only for ios.

It was enough for me to solve the problem (works on iOS & Android):

messaging().deleteToken(undefined,'*');

I'm going to reopen this as I think we have either a documentation issue or we need to alter the code to specify the asterisk as the default. Either way I appreciate the reports of what's working posted here, thanks

Was this page helpful?
0 / 5 - 0 ratings