Trying to get iOS device token by using firebase message in React Native

2.5k Views Asked by At

Below is the steps implemented in React Native code - Do i have to add any steps or methods in iOS side? Certificate is APNS enable.

Step 1:

# Install & setup the app module
yarn add @react-native-firebase/app

# Install the messaging module
yarn add @react-native-firebase/messaging

# If you're developing your app using iOS, run this command
cd ios/ && pod install


const getDeviceToken = firebase.messaging().getAPNSToken();
  if (getDeviceToken) {
    console.log('getDeviceToken:', getDeviceToken);
  }

But the result i am getting in console is : { _U: 0, _V: 0, _W: null, _X: null } Note: I am running the project in Actual iOS device. Notification permission is already granted.

2

There are 2 best solutions below

0
Naveed Shah On BEST ANSWER

firebase.messaging().getAPNSToken() returns a promise. You can get your token when the promise is reloved like this:

getDeviceToken.then( async (token) => {
  console.log('promise token: ', token);
})
0
Bhavya Koshiya On

You need to make it async in order to wait for the token to be fetched from firebase

use async await and it will work

const getDeviceToken = await firebase.messaging().getAPNSToken();
  if (getDeviceToken) {
    console.log('getDeviceToken:', getDeviceToken);
  }