Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Undefined is not an object (evaluating '_PushTokenManager.default.getDevicePushTokenAsync [REACT NATIVE EXPO]

Can't get the token with the getExpoPushTokenAsync() function on expo-notifications api. The function as follow is just identical to Expo documentation:

import Constants from "expo-constants";
import * as Notifications from "expo-notifications";
import * as Permissions from "expo-permissions";

async function registerForPushNotificationsAsync() {
    let token;
    if (Constants.isDevice) {
    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;
    }
    token = (await Notifications.getExpoPushTokenAsync()).data;
    console.log(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 token;
}

Expo: ~37.0.3 App.json:

"expo": {
        "android": {
          "useNextNotificationsApi": true
        }
      }

Seems like when calling the function, got the next warning:

[Unhandled promise rejection: TypeError: undefined is not an object (evaluating '_PushTokenManager.default.getDevicePushTokenAsync')]
like image 775
Beto Montenegro Avatar asked Jul 06 '20 02:07

Beto Montenegro


2 Answers

Make sure you import {Notifications} from 'expo' and not the other way from 'expo-notifications'. Was having the same problem but then tried importing it from the 'expo' and it worked. Check out this image

like image 157
Shubham Palkar Avatar answered Oct 18 '22 13:10

Shubham Palkar


make sure you are in managed workflow, if you are in bare you should specify { experienceId}

code example from the docs:

    import Constants from 'expo-constants';
import * as Notifications from 'expo-notifications';

export async function registerForPushNotificationsAsync(userId: string) {
  let experienceId = undefined;
  if (!Constants.manifest) {
    // Absence of the manifest means we're in bare workflow
    experienceId = '@username/example';
  }
  const expoPushToken = await Notifications.getExpoPushTokenAsync({
    experienceId,
  });
  await fetch('https://example.com/', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
    },
    body: JSON.stringify({
      userId,
      expoPushToken,
    }),
  });
}

experienceId (string) -- The ID of the experience to which the token should be attributed. Defaults to Constants.manifest.id exposed by expo-constants. In the bare workflow, you must provide a value which takes the shape @username/projectSlug, where username is the Expo account that the project is associated with, and projectSlug is your slug from app.json

for more info: https://docs.expo.io/versions/latest/sdk/notifications/#dailytriggerinput

like image 32
kholood Avatar answered Oct 18 '22 14:10

kholood