IOSDev
IOSDev

Reputation: 229

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

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.

Upvotes: 0

Views: 3612

Answers (2)

Naveed Shah
Naveed Shah

Reputation: 326

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);
})

Upvotes: 1

Bhavya Koshiya
Bhavya Koshiya

Reputation: 1806

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);
  }

Upvotes: 1

Related Questions