React-native-firebase: `onTokenRefresh` callback gives token as object not string

Created on 18 Nov 2019  路  8Comments  路  Source: invertase/react-native-firebase


Issue




Project Files






iOS

Click To Expand

#### `ios/Podfile`: - [ ] I'm not using Pods - [x] I'm using Pods and my Podfile looks like:

# N/A
#### `AppDelegate.m`:
// N/A


Android

Click To Expand

#### Have you converted to AndroidX? - [ ] my application is an AndroidX application? - [ ] I am using `android/gradle.settings` `jetifier=true` for Android compatibility? - [ ] I am using the NPM package `jetifier` for react-native compatibility? #### `android/build.gradle`:

// N/A
#### `android/app/build.gradle`:
// N/A
#### `android/settings.gradle`:
// N/A
#### `MainApplication.java`:
// N/A
#### `AndroidManifest.xml`:
<!-- N/A -->


Environment

Click To Expand

**`react-native info` output:**

 OUTPUT GOES HERE
- **Platform that you're experiencing the issue on**: - [ ] iOS - [ ] Android - [ ] **iOS** but have not tested behavior on Android - [ ] **Android** but have not tested behavior on iOS - [ ] Both - **`react-native-firebase` version you're using that has this issue:** - `6.0.3` - **`Firebase` module(s) you're using that has the issue:** - `messaging` - **Are you using `TypeScript`?** - `N`




Think react-native-firebase is great? Please consider supporting all of the project maintainers and contributors by donating via our Open Collective where all contributors can submit expenses. [Learn More]

Messaging

Most helpful comment

A little note on https://github.com/invertase/react-native-firebase/commit/1940d6c8fbab64ccf739186cea9633a605237942 : it changes the type of the value to "object", which might affect the way it is interpreted by other libraries. Make sure to cast it as a string using toStringor + "" before passing it to other libraries.

messaging().onTokenRefresh((token) => {
  token += "";
  // Now Token is a string
})

All 8 comments

Corresponding web sdk method does not return anything I think? https://firebase.google.com/docs/reference/js/firebase.messaging.Messaging.html#on-token-refresh

But getToken returns a string (presumably just the token? @ghsdh3409 do you know what the web sdk returns?) https://firebase.google.com/docs/reference/js/firebase.messaging.Messaging.html#get-token

Raw return from Obj-C is just the token https://firebase.google.com/docs/reference/ios/firebasemessaging/api/reference/Protocols/FIRMessagingDelegate#-messaging:didreceiveregistrationtoken:

Same from Java https://firebase.google.com/docs/reference/android/com/google/firebase/messaging/FirebaseMessagingService.html#public-void-onnewtoken-string-token ?

So probably should just be the raw token but that might be a breaking change for people

I tried to find how Java was handling it but got lost here https://github.com/invertase/react-native-firebase/blob/master/packages/messaging/android/src/main/java/io/invertase/firebase/messaging/ReactNativeFirebaseMessagingSerializer.java#L55 - I can't find the common package to see how it deserializes

@mikehardy
As the code, you commented, onTokenRefresh gives an object containing token on the Android, too.
https://github.com/invertase/react-native-firebase/blob/1f622039ccde227cf8dbf9f32d997d3a95a021f8/packages/messaging/android/src/main/java/io/invertase/firebase/messaging/ReactNativeFirebaseMessagingSerializer.java#L52-L56

I also think that it would be better if onTokenRefresh gives raw token as string since each firebase sdk returns raw token as you commented. (I hope that react-native-firebase has a consistency with native sdk references)

Is there any reason why v6 changed what onTokenRefresh gives? (In the previous v5, onTokenRefresh gives raw fcm token as string (https://rnfirebase.io/docs/v5.x.x/messaging/device-token#2)-Monitor-token-generation))

cc. @Salakar

Pushed a backwards compatible change for this up that supports onTokenRefresh(event => event.token) & onTokenRefresh(token => token)

onTokenRefresh(event => event.token) will log a deprecation warning and will be removed in the next major version

That still seems to happen, using @react-native-firebase/[email protected]. I suppose the next major version hasn't came out yet right?

Great library btw!

A little note on https://github.com/invertase/react-native-firebase/commit/1940d6c8fbab64ccf739186cea9633a605237942 : it changes the type of the value to "object", which might affect the way it is interpreted by other libraries. Make sure to cast it as a string using toStringor + "" before passing it to other libraries.

messaging().onTokenRefresh((token) => {
  token += "";
  // Now Token is a string
})

@Salakar When calling onrefreshToken i am receiving the token like this: {"0": "f", "1": "w", "10": "4", "100": "p", "101": "k", "102": "P", "103": "l", "104": "q", "105": "-", "106": "s", "107": "O", "108": "D", "109": "y", "11": ":", "110": "q", "111": "M", "112": "2", "113": "9", "114": "d", "115": "Y", "116": "5", "117": "V", etc etc......} iam not really sure why this is happening

@RickWoltheus That's excactly exactly what I was saying :wink:
It's caused by the fact that the string is converted to a String instance to be able to add the token property, which means it is not a "real" string. Just call toString() or force its cast with + "" and you're good to go.

Was this page helpful?
0 / 5 - 0 ratings