wix / react-native-notifications

React Native Notifications
MIT License
3.23k stars 763 forks source link

Invalid FCM token received on iOS #946

Closed seco35 closed 1 year ago

seco35 commented 1 year ago

Everything works as expected on android and ios, sending the notification from the firebase console to all devices (ios and android app).

Trying to address a single device using its token fail due to invalidity: { results: [ { error: [FirebaseMessagingError] } ], canonicalRegistrationTokenCount: 0, failureCount: 1, successCount: 0, multicastId: 172160598... } ERROR: FirebaseMessagingError: Invalid registration token provided. Make sure it matches the registration token the client app receives from registering with FCM.

Sending to android devices works as expected (both using firebase or my own backend). However sending to iOS devices from my backend throws the error above.

Also I observed, that the device tokens are different. The length of tokens in android is 163, however ios token sizes are 64 characters (this seems to be correct according to other google results).

Edit: I've changed my backend from using the firebase admin-sdk with legacy api to v1 API, with same result =>

{
code: 400,
  errors: [
    {
      message: 'The registration token is not a valid FCM registration token',
      domain: 'global',
      reason: 'badRequest'
    }
  ]
}

react-native: 0.71.2 react-native-notifications: ^4.3.2 Device: iPhone 13 Pro (iOS 16.1.1)

couchounou commented 1 year ago

how do translate IOS APNS token loaded by iOs device to FCM token format ?

seco35 commented 1 year ago

@couchounou The token that you retrieve from FCM is translated to the APNS token in Firebase itself. So you don't need to handle any translations from the FCM token to APNS token. Just sending your notification should do the trick.

couchounou commented 1 year ago

@seco35 ios devices registration from react-native-notifications get a apns token not a fcm ?

seco35 commented 1 year ago

@seco35 ios devices registration from react-native-notifications get a apns token not a fcm ?

Is it so? Haven't read that in the docs?

couchounou commented 1 year ago

@seco35 ios devices registration from react-native-notifications get a apns token not a fcm ?

Is it so? Haven't read that in the docs?

@seco35 Yes i can send push notif trough APNS directly with the token as it is retreived by the IOS device.

stale[bot] commented 1 year ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.

seco35 commented 1 year ago

Was trying to use the APNS token for FCM token. Didn't knew that I have to convert it by myself.

Here is the code I use in my node.js backend to retrieve the FCM token for a given APNS token:

convertAPNSToFCMToken(token) { return new Promise((resolve, reject) => { let data = { "application": "com.your.package.name", "sandbox":true, "apns_tokens":[ token ] } axios.post("https://iid.googleapis.com/iid/v1:batchImport", JSON.stringify(data), {"headers": {"Content-Type": "application/json", "Authorization" : "key={YOURKEYGOESHERE}"} }).then(r => { if(r.data?.results?.length > 0 && typeof r.data.results[0].registration_token !== "undefined" && r.data.results[0].registration_token.length > 0){ console.log("FCM Token found: ",r.data.results[0].registration_token); resolve(r.data.results[0].registration_token) }else{ console.log("Error while determining FCM token for APNS token!"); reject(); } }).catch(e => { console.log("Something went wrong while APNS Token conversion: ",e); reject(); }); }) }