Undefined不是对象(评估'_PushTokenManager.default.getDevicePushTokenAsync [REACT NATIVE EXPO]

lsmepo6l  于 2022-12-14  发布在  React
关注(0)|答案(5)|浏览(172)

无法在Expo-notifications API上使用getExpoPushTokenAsync()函数获取令牌。以下函数与Expo文档完全相同:

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

博览会:~37.0.3应用程序:

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

似乎在调用该函数时,得到了下一个警告:

[Unhandled promise rejection: TypeError: undefined is not an object (evaluating '_PushTokenManager.default.getDevicePushTokenAsync')]
wi3ka0sx

wi3ka0sx1#

请确定您是从“expo”汇入{Notifications},而不是从“expo-notifications”汇入{Notifications}。之前也有相同的问题,但尝试从“expo”汇入,结果成功了。Check out this image

kiz8lqtg

kiz8lqtg2#

请确保您处于托管工作流中,如果您处于裸工作流中,则应指定{ experienceId}
文档中的代码示例:

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(字符串)--令牌应归属的体验ID。默认为Constants.manifest.idexpo-constants公开的www.example.com。在bare工作流中,您必须提供一个采用@username/projectSlug形式的值,其中username是与项目关联的Expo帐户,projectSlug是app.json中的slug
更多信息:https://docs.expo.io/versions/latest/sdk/notifications/#dailytriggerinput

ftf50wuq

ftf50wuq3#

我得到这个错误,因为我没有登录到博览会(使用博览会客户端).

5lhxktic

5lhxktic4#

上面的答案对我都没有帮助。我不得不将我的import语句从

  • import { Notifications } from 'expo';

  • import * as Notifications from 'expo-notifications';
mpbci0fu

mpbci0fu5#

您应该使用

import * as Notifications from 'expo-notifications';

这应该可以

相关问题