gpt4 book ai didi

ios - Firebase Cloud Messaging 到 Android 可以正常工作,但 iOS 却失败。如何构建 iOS 的有效负载?

转载 作者:行者123 更新时间:2023-11-29 05:35:34 27 4
gpt4 key购买 nike

我可以从 Firebase 控制台向我在 iOS 和 Android 上的应用发送测试通知。因此,我的应用程序已正确设置为在两个平台上接收推送通知。但是,当我使用云功能发送通知时。仅接收 Android 设备上的通知。 iOS 设备上不会显示任何通知。我怀疑这可能与我在云函数中创建有效负载的方式有关。也许我缺少 iOS 的一些东西。如果您能给我一些提示,那就太好了。

我检查了 iOS 设备的 deviceToken 是否正确,结果是正确的。

我使用 firebase 控制台向 iOS 设备的同一 deviceToken 发送了一条测试消息,并且通知已发送。

因此,我断定我的问题可能是由我编写的云函数引起的。因此,我将云功能分享如下:

exports.notifToApp = functions.database.
ref(`/memInfo/{memId}/notifChoice/`).onWrite((snap, context) => {

//send only if exists and new notification OR if doesn't exist
if ((snap.before.exists() && (snap.after.val() !== snap.before.val())) || !snap.before.exists()) {

//get notification body
const notificationTitle = snap.after.val().memName;
const notificationText = snap.after.val().notifText;

//get and loop over notification subscribers
return admin.database().ref(`/notifics/${context.params.memId}/notifSubs/`).once("value", subs => {
if (subs.exists()) {
return subs.forEach(sub => {

//payload for notification
const payload = {
"notification":{
"title": notificationTitle,
"body": notificationText,
"sound": "default",
"click-action": "FCM_PLUGIN_ACTIVITY",
"priority": "high"
}
}


//deliver notification
return admin.messaging().sendToDevice(sub.val().deviceToken, payload).catch(e => {console.log(e);});

});
} else { //end: if returned any value
return 0;
}
});// end: get and loop over notification subscribers
} else { //end: send only if exists and new notification OR if doesn't exist
return 0;
}
});

我没有收到任何错误消息。函数成功完成,状态为“OK”。

我使用两台设备进行测试:一台 Android 设备和一台 iOS 设备。两个设备 token 都正确保存在数据库中,以便云功能检索并用于发送消息。

我在运行我的应用程序的 Android 设备上看到通知。我希望通知显示在运行相同应用程序的 iOS 设备上。

从 Firebase 控制台发送的测试消息通知在两台设备上均正确显示。

最佳答案

我意识到 sendToDevice() 使用了旧版本的有效负载。我在函数中使用了 send() 来使用较新的版本。 (参见答案:stackoverflow)

admin.messaging().send(payload).catch(e => console.log(e));

我根据最新指南更改了有效负载以包含平台特定字段(请参阅 firebase docs )

const payload = {
"token": sub.val().deviceToken,
"notification":{"title": notificationTitle,"body": notificationText},
"android": {"notification": {"sound": "default"}},
"apns": {"payload": {"aps": {"sound": "default"}}}
};

现在它可以在两个平台上运行。

关于ios - Firebase Cloud Messaging 到 Android 可以正常工作,但 iOS 却失败。如何构建 iOS 的有效负载?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57066039/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com