Expo Push Notification not working on standalone app

Viewed 5512

I made the setup for push notifications using the expo push system and it works fine when using the app through ExpoGo. But in the installed app from the play store, it doesnt work. Debugging the app I found out that the

token = (await Notifications.getExpoPushTokenAsync()).data;

is returning nothing because the alert after it never runs.

async function registerForPushNotificationsAsync() {
    let token;
    const { status: existingStatus } = await Permissions.getAsync(Permissions.NOTIFICATIONS);
    let finalStatus = existingStatus;
    if (existingStatus !== 'granted') {
      const { status } = await Permissions.askAsync(Permissions.NOTIFICATIONS);
      finalStatus = status;
    }
    if (finalStatus !== 'granted') {
      alert('Failed to get push token for push notification!');
      return;
    }
    alert('1')
    token = (await Notifications.getExpoPushTokenAsync()).data;
    alert('2')
    if (Platform.OS === 'android') {
      Notifications.setNotificationChannelAsync('default', {
        name: 'default',
        importance: Notifications.AndroidImportance.MAX,
        vibrationPattern: [0, 250, 250, 250],
        lightColor: '#FF231F7C',
      });
    }

    //sending the token to my api
    api.put('xxx', {
      notification_token: token,
    });
  }

What should I do? Is anything I am missing? I just used the codes given in the docs https://docs.expo.io/push-notifications/overview/

4 Answers

The expo push notification documentation page is not super clear in mentioning that, there are few steps needed for push notification to work in a standalone android/ios app. Although it mentions in the documentation as "Next Steps" at the bottom of the page, but from the first paragraph it is not certainly clear that they are distinguishing expo app notification from standalone android/ios app notification. Rather it felt that Expo takes care of all the things as if no more steps require for standalone apps. It is certainly right only for Expo app. However, when we land into the Next Steps page, we find the most important information in the "Credentials" section.

So this is how it works for Android Stand alone app (which I have done). We need to setup our own Firebase project as it says here Using FCM for Push Notifications. If you are using Expo server to send Push notification, make sure to follow the steps "Upload Server Credentials". You will see the FCM Server Key added in your Expo Credentials.

It should not take too much of a time but for me it took 1 hour or so to setup these extra steps as I was learning these, for the first time.

Notifications on android haven't worked on our project. And in addition to other answers some of the following helped:

  • I've uploaded FCM service key to Expo
  • Put SHA certificate fingerprint to Firebase project
  • Set experienceId explicitly in getExpoPushTokenAsync

I had similar issues on android. Following were the steps which helped me resolve the issue.

  1. create firebase project and add sha key (not sure if it's necessary)

  2. enable cloud messaging API on firebase and upload the server key to expo servers as fcm key expo push:android:upload --api-key <server-key>

  3. add the 96x96px notification icon in directory and add the path in the

app.json file "notification": { "icon": "./src/assets/notificationIcon.png", "color": "#ffffff", "iosDisplayInForeground": true }

  1. create android file with expo prebuild --platform android

  2. add implementation platform('com.google.firebase:firebase-bom:30.2.0') in android/app/build.gradle file.

  3. add following permissions in manifest file.

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM"/>
  1. rebuilt the project.
Related