FCM Token for firebase cloud messaging sometimes not registered

Viewed 4086

I have a React Native app with React Native Firebase, which I use for push notifications / cloud messaging. So generally everything is fine, but the iOS version has one issue.

The problem is that the FCM Token I receive (const fcmToken = await messaging().getToken()) sometimes is valid and works and sometimes I get an error with NotRegistered as reason.

My test is very simple. I uploaded a version to TestFlight. When I'm now installing this version of the app a few times then the received FCM token is sometimes valid and sometimes it is not. The same code, the same keys/certificates, the same device.

Additionally checking the APNS token, that seems to be valid and working fine every time I checked.

  • I made sure I setup the Firebase app on launch with [FIRApp configure];
  • I checked if the App is registered at the APNS
  • I checked on another iPhone (The result was the same)

What can possible be wrong when the token sometimes works and sometimes doesn't?

Any hints are highly appreciated

System info:

System:
    OS: macOS 10.15.6
    CPU: (8) x64 Intel(R) Core(TM) i7-7700HQ CPU @ 2.80GHz
    Memory: 557.23 MB / 16.00 GB
    Shell: 3.0.2 - /usr/local/bin/fish
  Binaries:
    Node: 14.5.0 - /var/folders/bn/f6trf3q55vj_hjplf9vy9ls00000gn/T/yarn--1598005931689-0.20174739828176125/node
    Yarn: 1.22.4 - /var/folders/bn/f6trf3q55vj_hjplf9vy9ls00000gn/T/yarn--1598005931689-0.20174739828176125/yarn
    npm: 6.14.5 - ~/.nodenv/shims/npm
    Watchman: 4.9.0 - /usr/local/bin/watchman
  SDKs:
    iOS SDK:
      Platforms: iOS 13.6, DriverKit 19.0, macOS 10.15, tvOS 13.4, watchOS 6.2
    Android SDK:
      API Levels: 28, 29
      Build Tools: 28.0.3, 29.0.2
      System Images: android-28 | Intel x86 Atom, android-28 | Google APIs Intel x86 Atom, android-28 | Google Play Intel x86 Atom
      Android NDK: Not Found
  IDEs:
    Android Studio: 3.6 AI-192.7142.36.36.6392135
    Xcode: 11.6/11E708 - /usr/bin/xcodebuild
  Languages:
    Java: 1.8.0_232 - /usr/bin/javac
    Python: 2.7.16 - /usr/bin/python
  npmPackages:
    @react-native-community/cli: Not Found
    react: 16.13.1 => 16.13.1
    react-native: 0.63.2 => 0.63.2
  npmGlobalPackages:
    *react-native*: Not Found
1 Answers

fcm sends a NotRegistered state because if you turn off the app based on ios, the fcm and the app's state are disconnected.

So, when you get the token value by executing the getToken function, you get the old token value instead of getting a new refresh token.

So, if you execute the following code and perform post message again with the refresh token value, you can receive the message normally.

const getToken = async () => {
  const oldToken = await messaging().getToken();
  await messaging().deleteToken(String(${fcm_send_id}), '*');
  const newToken = await messaging().getToken();
  if (oldToken === newToken) {
     console.log('not refresh')
  } else {
    return newToken;
  }
};

Related