繁体   English   中英

未定义不是 object(评估 '_PushTokenManager.default.getDevicePushTokenAsync [REACT NATIVE EXPO]

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

无法在博览会通知 api 上使用 getExpoPushTokenAsync() function 获取令牌。 如下 function 与 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; }

Expo:~37.0.3 App.json:

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

好像在调用 function 时,得到了下一个警告:

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

确保从“expo”导入 {Notifications},而不是从“expo-notifications”导入。 遇到了同样的问题,但后来尝试从“世博会”导入它,它起作用了。 看看这张图片

确保您处于托管工作流程中,如果您处于裸露状态,则应指定 { 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 (string) -- 应归属于令牌的体验的 ID。 默认为由 expo-constants 公开的 Constants.manifest.id。 在裸工作流中,您必须提供一个形如 @username/projectSlug 的值,其中 username 是与项目关联的 Expo 帐户,projectSlug 是来自 app.json 的 slug

更多信息: https://docs.expo.io/versions/latest/sdk/notifications/#dailytriggerinput

我收到此错误是因为我没有登录到 Expo(使用 expo 客户端)。

以上答案都没有帮助我。 我不得不将我的导入声明从

  • import { Notifications } from 'expo';

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM