我正在试用 Expo 推送通知服务。我通过点击“在小吃上试试这个例子”来运行 code in the overview,然后将我的 iPad 指向二维码,它在 Expo Go 中加载了这个例子。当我点按“按下以发送通知”时,我确实看到了一条通知,一切都很好。
使用印在 iPad 应用程序上的相同令牌(“您的博览会推送令牌:ExponentPushToken[token-goes-here]”)我尝试通过在我的 PC 上运行 curl 命令来调用 Expo API 以触发使用 HTTPS POST
远程推送通知,同时将应用程序保持在前台:
curl -H "Content-Type: application/json" -X POST "https://exp.host/--/api/v2/push/send" -d '{
"to": "ExponentPushToken[token-goes-here]",
"title":"hallo",
"body": "world"
}'
但我明白了:
{"data":{"status":"error","message":"\"ExponentPushToken[token-goes-here]\" is not a registered push notification recipient","details":{"error":"DeviceNotRegistered"}}}
查看 sendPushNotification(expoPushToken)
的代码,我发现它使用与 fetch
相同的 API:
// Can use this function below, OR use Expo's Push Notification Tool-> https://expo.io/notifications
async function sendPushNotification(expoPushToken) {
const message = {
to: expoPushToken,
sound: 'default',
title: 'Original Title',
body: 'And here is the body!',
data: { someData: 'goes here' },
};
await fetch('https://exp.host/--/api/v2/push/send', {
method: 'POST',
headers: {
Accept: 'application/json',
'Accept-encoding': 'gzip, deflate',
'Content-Type': 'application/json',
},
body: JSON.stringify(message),
});
}
那么为什么它可以在 iPad 上运行,而不能在 PC 上使用完全相同的令牌呢?顺便说一句,我仔细检查了令牌并尝试了基于网络的 push notifications tool 并收到了“这不是有效的 Expo 推送令牌”错误。我错过了一步吗?我还没有做任何实际的构建,但认为它会起作用。
答案 0 :(得分:0)
问题中显示的方法后来起作用了。相同的 curl
命令后来返回了 "status":"ok"
,我在移动设备上收到了通知。这可能是世博会方面的临时问题。
如果再次发生这种情况,并且 API 没有改变,建议稍后再试或联系 Expo。