Can not get expo push token

Viewed 4722

I am building a React native app which is based on Expo , i use Expo's push notification . when i test app with expo cli i get the expo token . and after i generate a .aab and i post it on play store . i can't get the expo token from any device . i don't know why .

registerForPushNotification = async() => {
    // Check for existing permissions
    const {status} = await Permissions.getAsync(Permissions.NOTIFICATIONS);
    let finalStatus = status;

    // if no existing permission, ask user for permission
    if (status !== 'granted') {
        const {status} = await Permissions.askAsync(Permissions.NOTIFICATIONS);
        finalStatus = status;
    }

    // if no permission, exit the function.
    if (finalStatus !== 'granted') {
      alert('Failed to get push token for push notification!')
      return;}

    // get push notification token.
    let token = await Notifications.getExpoPushTokenAsync();
    alert(token)
    firebase.database().ref('/users/usersInfo/'+user).update({
      expoToken:token
    })

    if (Platform.OS === 'android') {
      Notifications.createChannelAndroidAsync('default', {
        name: 'default',
        sound: true ,
        priority: 'max',
        vibrate: [0, 250, 250, 250],
      });
    }
  }
1 Answers

In order to use push notifications for your app, your should register the app for firebase.

If you have not already created a Firebase project for your app, do so now by clicking on Add project in the Firebase Console.

In your new project console, click Add Firebase to your Android app and follow the setup steps. Make sure that the Android package name you enter is the same as the value of android.package in your app.json.

Download the google-services.json file and place it in your Expo app's root directory.

In your app.json, add an android.googleServicesFile field with the relative path to the google-services.json file you just downloaded. If you placed it in the root directory, this will probably look like:

{
  ...
  "android": {
    "googleServicesFile": "./google-services.json",
    ...
  }
}

Now you can build your standalone app by using expo build:android -t apk or the app bundle expo build:android -t app-bundle

from the documentation.

Related