【问题标题】:Undefined is not an object (evaluating '_PushTokenManager.default.getDevicePushTokenAsync [REACT NATIVE EXPO]未定义不是对象(评估 '_PushTokenManager.default.getDevicePushTokenAsync [REACT NATIVE EXPO]
【发布时间】:2020-07-06 02:11:31
【问题描述】:

无法使用 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 App.json:

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

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

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

【问题讨论】:

  • 你是在真机还是模拟器上做这个?
  • 真机与安卓系统中的Expo App
  • 谁能解决这个问题?为什么我们需要使用“expo”模块而不是文档所述的“expo-notifications”。
  • 嗯,经过长时间的研究,终于找到了解决方案。实际上,世博会文档说 import * as Notifications from 'expo-notifications';但实际上与 import { Notifications } from "expo" 一起使用;此外,您必须登录 expo cli。检查这个:github.com/expo/expo/issues/7831#issuecomment-613647709
  • 这里有同样的问题。你找到解决方案了吗? (托管工作流)

标签: android react-native push-notification expo


【解决方案1】:

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

【讨论】:

【解决方案2】:

确保您处于托管工作流程中,如果您处于裸露状态,则应指定 {体验ID}

文档中的代码示例:

    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

【讨论】:

    【解决方案3】:

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

    【讨论】:

      【解决方案4】:

      以上答案都没有帮助我。我不得不从

      更改我的导入语句
      • 从'expo'导入{通知}; 到
      • import * as Notifications from 'expo-notifications';

      【讨论】:

      猜你喜欢
      • 2020-09-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-29
      相关资源
      最近更新 更多