[expo-notifications] Error encountered while updating the device push token with the server

Hi,

I’m trying to set up notifications with bare-work-flow.

I initially was able to retrieve my expo push token but when I returned to the project the next day I was met the following set of error:

[expo-notifications] Error encountered while updating the device push token with the server: {"error":"invalid_token","error_description":"The bearer token is invalid"}

I can’t find anything regarding the issue. Does anyone know what it might be?

– package.json snippet

{ 
  "dependencies": {
    "expo": "~40.0.0",
    "expo-analytics": "^1.0.16",
    "expo-app-loading": "^1.0.1",
    "expo-font": "~8.4.0",
    "expo-image-manipulator": "~8.4.0",
    "expo-image-picker": "~9.2.0",
    "expo-intent-launcher": "~8.4.0",
    "expo-notifications": "~0.8.2",
    "expo-splash-screen": "~0.8.0",
    "expo-updates": "~0.4.0",
  }
}

This is the registerForPushNotificationsAsync() function I’m using:

import Constants from 'expo-constants';
import * as Notifications from 'expo-notifications';
import { Platform } from 'react-native';

export const registerForPushNotificationsAsync = async () => {
  try {
    if (Constants.isDevice) {
      const experienceId = '@ndasilva/{APP-SLUG}'; // this has the actual slug in my application.

      const {
        status: existingStatus
      } = await Notifications.getPermissionsAsync();
      let finalStatus = existingStatus;
      if (existingStatus !== 'granted') {
        const { status } = await Notifications.requestPermissionsAsync();
        finalStatus = status;
      }
      if (finalStatus !== 'granted') {
        alert('Failed to get push token for push notification!');
        return;
      }
      const token = (
        await Notifications.getExpoPushTokenAsync({ experienceId })
      ).data;
      console.log('  🏷  🏷   Token :', token);
      return token;
    } else {
      alert('Must use physical device for Push Notifications');
    }

    if (Platform.OS === 'android') {
      Notifications.setNotificationChannelAsync('default', {
        name: 'default',
        importance: Notifications.AndroidImportance.MAX,
        vibrationPattern: [0, 250, 250, 250],
        lightColor: '#FF231F7C'
      });
    }
    return undefined;
  } catch (error) {
    console.error('Error in registerForPushNotificationsAsync()', error);
    return undefined;
  }
};

Hi!

I haven’t seen this error before… is that happening on just one particular device? What happens if you uninstall the app and reinstall it again? Sorry for the very basic suggestion, just trying to understand if this was intermittent or not

Hi Charliecruzan,

No, it happens on more than one of our iPhone devices.

The same thing happens if I uninstall and reinstall the app. We’ve also had a different developer pull down the project on his machine before my changes of introducing notifications, once he set it all up he was also met with the same issue.

We were able to establish that if we post the body directly to expo on this url then we were able to successfully retrieve the expo push token.

Have you checked what the difference is between the body you are sending manually, and the one being sent by expo-notifications when you call getExpoPushTokenAsync?

The body we posted was taken when we logged out the value of body. As far as we are aware, we posted the exact same value.

hm, that would be very odd if the same request consistently resulted in two different responses :thinking:

Hi @charliecruzan,

We finally figured it out. We were using ‘fetch-token-intercept’ to add the bearer token to calls by default for our APIs. For whatever reason at some point the bearer token was not added, which meant I was able to retrieve the token successfully.

That meant calls to Expo from our device had our bearer token attached to it, which was failing Expo’s auth.

I was mislead by having it work at one point (I assume I had no bearer token for some reason) and then when I reauthenticated, the bearer token was present and therefore I could no longer call to Expo.

1 Like