firebase.messaging().getToken
never returns. This is how i am calling it I added a setTimeout incase the token was not available right away
firebase.messaging().hasPermission().then(enabled=>{
if(enabled){
console.log('enabled');
setTimeout(()=>{
firebase.messaging().getToken(token=>{
if(token)
console.log('token',token);
else
console.log('no token yet');
})
},60000);
}else{
firebase.messaging().requestPermission().then(()=>{
console.log('got permission');
}).catch(err=>{
console.log('did not get persmisis');
});
}
});
However I see the token in android logs using adb logcat
:
06-08 18:19:20.933 9073 9250 D RNFirebaseMessaging: Firebase token: eR-BHM7-SS0:APA91bESl4rYYglx5tTzDN8dkTWqZQozCSbJGWmXmGAxPla13KlJV9QM6fADaOGyt-NmoApjy0ToRwmo33P0S7RtNu_Z7R6QvHTMjPKgL6MBqmFmSZ6eZ8mjxhCjOQKTG4MNyu3teT3l
As the docs say getToken() returns Promise containing String;
Try:
import firebase from 'react-native-firebase';
const messaging = firebase.messaging();
messaging.hasPermission()
then((enabled) => {
if (enabled) {
messaging.getToken()
.then(token => { /* do stuff */ })
.catch(error => { /* handle error */ });
} else {
messaging.requestPermission()
.then(() => { /* got permission */ })
.catch(error => { /* handle error */ });
}
})
.catch(error => { /* handle error */ });
Hope it solves it.
@husscode is correct. You need to be awaiting a promise to be returned by getToken
I'm seeing a similar issue with react-native-firebase/messaging:@6.3.4
. I am awaiting the Promise as the docs suggest, but it never resolves. In the code below, the second log never occurs:
import FBMessaging from '@react-native-firebase/messaging'
// ...
console.log( 'pre getToken' )
const fcmToken = await FBMessaging.getToken()
console.log( 'post getToken', fcmToken )
I face same issue. I do have permissions enabled, but getToken never resolves, in ios simulator. I use react-native-firebase: 5.5.6
I don't believe testing anything about FCM in a simulator is useful. Use a real device
I found the solution to this:
The problem was that i was using older version of the following pods:
6.27.0 is out and works fine, react-native-firebase test project is already updated with them #3815 - I'd get current, 6.13.0 is pretty old at this point even.
Most helpful comment
As the docs say
getToken() returns Promise containing String;
Try:Hope it solves it.