【问题标题】:[Unhandled promise rejection: TypeError: undefined is not an object (evaluating '_expoPermissions.Permissions.getAsync')][Unhandled promise rejection: TypeError: undefined is not an object (evaluating '_expoPermissions.Permissions.getAsync')]
【发布时间】:2019-11-02 16:25:57
【问题描述】:

我无法获取推送通知的令牌并且权限不起作用:

未处理的承诺拒绝:TypeError: undefined is not an object(评估 '_expoPermissions.Permissions.getAsync')

这是代码:

registerForPushNotificationsAsync = async ()=> {
  const { status } = await Permissions.getAsync(Permissions.NOTIFICATIONS);
  let finalStatus = status;
  if (status !== 'granted') {
    const { status } = await Permissions.askAsync(Permissions.NOTIFICATIONS);
    finalStatus = status;
  }
  if (finalStatus !== 'granted') {
    return;
  }
  let token = await Notifications.getExpoPushTokenAsync();
  console.log(token);
}

【问题讨论】:

  • Permissions.getAsync 方法好像有问题。
  • 您可以发布您的例外情况
  • 你导入权限了吗:import { Permissions } from 'expo';
  • 你的 Expo 是什么版本?

标签: react-native expo


【解决方案1】:

权限从“expo”包移到“expo-permissions”

您必须先安装 expo-permissions 包,然后使用以下语法导入它

import * as Permissions from 'expo-permissions';

【讨论】:

  • 事实上,很多软件包在新版本中被移到了 expo 之外。它通过不包括世博会不需要的东西来减少应用程序的大小。
  • 我继续从 yarn 安装 expo-permissions,但它应该来自 expo。喜欢expo install expo-permissions
【解决方案2】:

我使用了正确的包,但我盲目地把我的导入写错了。 Rusty 的方式仍然是正确的,但对于其他做了正确事情但可能落入这个简单陷阱的人,请务必检查一下。

不要这样导入

import Permissions from 'expo-permissions';

像这样导入

import * as Permissions from 'expo-permissions';

...如果不是,即使使用 CORRECT 包,您也会看到相同的错误消息!

【讨论】:

    猜你喜欢
    • 2020-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-05
    • 2019-02-24
    • 2022-12-21
    • 2023-02-18
    • 2022-12-03
    相关资源
    最近更新 更多