en-dee-es
en-dee-es

Reputation: 21

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

I'm using Expo's bare-workflow for my react-native project. I'm using expo's push notification service. I keep getting the following error whenever I try to get my expo push token:

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

I'm running the app directly on my device so should be able to get notifications.

I'm using basically the same registerForPushNotificationsAsync() that is provided in the documentation.

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 = '@{username}/{slug}';

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

Expo packages in package.json

    "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",

I can't see anything about setting a bearer token, so I'm unsure what it could be after or where to even set it assuming I was able to determine what bearer token it is after.

Does anyone know what might be causing the problem?

Upvotes: 1

Views: 2590

Answers (1)

en-dee-es
en-dee-es

Reputation: 21

So we figured it out.

We were using fetch-token-intercept which was adding our bearer token to calls going to expo, which meant validation was failing.

We modified the function to exclude calls to expo from including our bearer token and now the token is being retrieved successfully.

Upvotes: 0

Related Questions