Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can not get expo push token

Tags:

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],
      });
    }
  }
like image 943
adil stifi Avatar asked Jul 02 '20 22:07

adil stifi


People also ask

How do I get push notifications from Expo?

When you're ready to send a push notification, take the Expo push token from your user record and send it to the Expo API using a plain old HTTPS POST request.

How do you get Expo tokens?

To use tokens, you need to define an environment variable, like EXPO_TOKEN="token" , before running commands. If you are using GitHub Actions, you can configure the expo-token property to include this environment variable in all of the job steps.

Do expo push tokens expire?

The ExpoPushToken will never "expire" but if one of your users uninstalls the app, you'll receive a DeviceNotRegistered error back from Expo's servers, meaning you should stop sending notifications to this app.


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.

like image 62
Biskrem Muhammad Avatar answered Sep 20 '22 13:09

Biskrem Muhammad