簡體   English   中英

將Firebase Cloud Messaging發送到Android,但iOS失敗。 如何為iOS構建有效負載?

[英]Firebase Cloud Messaging to Android works but iOS fails. How to structure payload for iOS?

我可以在iOS和Android上將測試通知從Firebase控制台發送到我的應用。 因此,我的應用程序設置正確,可以在兩個平台上接收推送通知。 但是,當我使用雲功能發送通知時。 僅在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;
}
});

我沒有收到任何錯誤消息。 功能成功完成,狀態為“確定”。

我測試使用兩種設備:一台android和一台iOS。 這兩個設備令牌都已正確保存在數據庫中,以供雲功能檢索和用於發送消息。

我在運行我的應用的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"}}}
};

現在,它可以在兩個平台上使用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM