我正在尝试从我的应用程序接收推送通知令牌,但我从未获得令牌。我尝试使用一些警报进行调试,并且可以看到接受通知时得到了“已授予”的返回。我只测试过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(令牌)似乎为空。
我还注意到我被要求提供两个权限。首先,它要求使用通知的许可,然后,它要求访问照片。我不需要对照片的许可,我很好奇为什么要这么做。
据我了解的阅读文档,仅Android设备需要FCM吗?我也需要它在android上运行,但我认为首先要使其在一个平台上运行,然后继续前进。
我使用Testflight在iPhone上安装了该应用程序。一旦应用程序被批准进入应用商店,令牌是否只会“出现”?
也许我在文档中还缺少其他内容。
任何帮助或指出正确的方向将不胜感激。
答案 0 :(得分:1)
我终于明白了!
将解决方案留在这里,以防其他人遇到同样的问题。
显然,即使我尝试使用npm install -g expo-cli
对其进行了多次更新,我也拥有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安装了该应用程序,现在我确实得到了预期的令牌!
我唯一好奇的是为什么它要求获得访问照片的权限。
我希望这可以帮助其他遇到相同问题的人。
感谢那些花时间阅读我的问题的人。
答案 1 :(得分:0)
我使用的是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;
这帮了我大忙。希望对别人有帮助!