React-native-firebase: getToken does not return

Created on 8 Jun 2018  路  7Comments  路  Source: invertase/react-native-firebase

Issue

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

Environment

  1. Application Target Platform:android
  1. Development Operating System:Ubuntu (16.04)
  1. Build Tools:
  1. React Native version:0.48
  1. RNFirebase Version:4.2.3
  1. Firebase Module:messaging

Most helpful comment

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.

All 7 comments

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:

  • 'Firebase/Core', I had 6.9.0 instead of 6.13.0
  • 'Firebase/Messaging', I had 6.9.0 instead of 6.13.0
  • 'GoogleAppMeasurement', I had 6.1.2 instead of 6.1.6

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

romreed picture romreed  路  3Comments

Draccan picture Draccan  路  3Comments

dgruseck picture dgruseck  路  3Comments

ODelibalta picture ODelibalta  路  3Comments

n-scope picture n-scope  路  3Comments