【问题标题】:iOS - Expo Notifications.getExpoPushTokenAsync() not getting tokeniOS - Expo Notifications.getExpoPushTokenAsync() 未获得令牌
【发布时间】:2019-08-06 19:56:22
【问题描述】:

我正在尝试从我的应用程序接收推送通知令牌,但我从未收到令牌。我尝试使用一些警报进行调试,我可以看到在接受通知时我得到了“授权”返回。我只测试了 iOS。

我在跑步

"expo": "^32.0.0",
"react-native": "https://github.com/expo/react-native/archive/sdk-32.0.0.tar.gz",

我尝试使用来自https://docs.expo.io/versions/latest/guides/push-notifications/的指南

由于它不起作用,我尝试了他们从 API 参考中提供的零食:https://docs.expo.io/versions/v32.0.0/sdk/notifications/

小吃:https://snack.expo.io/@documentation/pushnotifications

这是我当前的代码:

 static registerForPushNotificationsAsync = async kid => {
    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;
      }
      let token = await Notifications.getExpoPushTokenAsync();
      alert("finalstatus " + finalStatus);
      alert("existing status " + existingStatus);
      alert(token);
      // POST the token to your backend server from where you can retrieve it to send push notifications.
      return await fetch(`${Api.APIEndpoint}/app/notification`, {
        method: "POST",
        headers: {
          Accept: "application/json",
          "Content-Type": "application/json"
        },
        body: JSON.stringify({
          token: token,
          kid: kid
        })
      });
    } else {
      alert("Must use physical device for Push Notifications");
    }
  };

前两个警报按预期触发(并在我接受时返回“已授予”),但 alert(token) 似乎为空。

我还注意到我被要求提供两个权限。首先,它请求使用通知的权限,然后请求访问照片。我不需要照片的许可,我很好奇它为什么会要求这样做。

据我了解阅读文档,只有 android 设备才需要 FCM?我也需要它在 android 上工作,但我想先让它在一个平台上工作,然后继续。

我使用 Testflight 在我的 iPhone 上安装了该应用程序。令牌是否仅在应用被批准进入应用商店后“出现”?

也许我在文档中遗漏了一些其他内容。

我们将不胜感激任何帮助或指出正确的方向。

【问题讨论】:

    标签: ios react-native expo


    【解决方案1】:

    我终于明白了!

    将解决方案留在这里,以防其他人遇到同样的问题。

    显然我有一个旧版本的 expo-cli,即使我尝试使用 npm install -g expo-cli 多次更新它。该命令似乎运行良好,但 expo-cli 仍然是相同的版本。

    我尝试使用 npm 卸载,但似乎没有效果。 expo-cli 仍然可用。我想可能是因为我在开始使用 nvm 之前安装了它。

    然后我决定手动删除它。我在mac上运行,不知道是否有更正确的卸载方法。但这似乎奏效了!我删除了 /usr/bin/expo 和 /usr/bin/expo-cli。然后我跑了npm install -g expo-cli

    然后我更新了应用的推送证书:

    expo build:ios --clear-push-cert
    

    我在我的测试设备上使用 testflight 安装了应用程序,现在我确实按预期获得了令牌!

    我唯一仍然好奇的是为什么它要求访问照片的权限。

    我希望这可以帮助其他有同样问题的人。

    感谢那些花时间阅读我的问题的人。

    【讨论】:

      【解决方案2】:

      我在 v34 上,但仍然有很大的问题。以防以后对任何人有帮助,我发现使用 Permissions.USER_FACING_NOTIFICATIONS 首先会导致提示出现,然后我可以跟进 Permissions.NOTIFICATIONS,如下所示:

      const { status } = await Permissions.askAsync(Permissions.USER_FACING_NOTIFICATIONS);
      const { status2 } = await Permissions.askAsync(Permissions.NOTIFICATIONS);
      finalStatus = status2?status2:status;
      

      这对我有用。希望它可以帮助别人!

      【讨论】:

      • 这正是我所需要的。前台显示时(通过 Notifications.setNotificationHandler),我在我的应用程序中看到了通知数据,但它不会显示在我的手机上。请求 Permissions.USER_FACING_NOTIFICATIONS 是我所缺少的。谢谢@大卫穆迪!
      猜你喜欢
      • 1970-01-01
      • 2020-02-04
      • 1970-01-01
      • 1970-01-01
      • 2021-12-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多