我已将用户图库中的图片上传到 Firebase 存储并获得以下 URL -
在谷歌浏览器中打开时是一个有效的图像 URL。
问题是,当我尝试通过以下方法将它发送到 Firebase 服务器时,我的 URL 来自 firebase 不起作用 -
exports.helperPromiseNotify = function (profileId, notificationKey, body, payloadData, image) {
// admin
const fcm = admin.messaging();
const db = admin.database();
// prepare obj
const notificationDBObj = {
created_at: internalGetUnixEpoch(),
is_read: false,
key: notificationKey,
body: body,
data: payloadData,
};
console.log("utils.js helperPromiseNotify " + image)
var payload = {
notification: {
body: body,
image: image
//this is the place -> //image: "https://1mdtalent.com/wp-content/uploads/2017/12/cropped-1-2.png" // <- this is the place
},
data: payloadData
};
// find
const prFetchDeviceTokens = db.ref(config.ENTITY_NAME_DEVICES).child(profileId).child("tokens")
.once("value")
.then(snapshot => {
const tokensArr = new Array();
if (snapshot.exists()) {
// extract tokens
const obj = snapshot.val();
Object.keys(obj).forEach(key => {
tokensArr.push(obj[key]);
})
}
return tokensArr;
})
const prSendFCM = tokensArr => {
if (tokensArr !== undefined && tokensArr !== null && tokensArr.length > 0 && tokensArr.length <=1000) {
fcm.sendToDevice(tokensArr, payload)
.catch((error) => {
console.log(`Failed send notification to device: ${error}`);
});
}
};
// chains
const prDB = db.ref(config.ENTITY_NAME_NOTIFICATIONS).child(profileId).push(notificationDBObj);
return prDB
.then(() => prFetchDeviceTokens)
.then(tokensArr => prSendFCM(tokensArr));
}
我有另一张从谷歌上截取的图片,它有一个更清晰的 URL 和一个扩展名,所以我想这就是问题所在。当将“图像”变量从当前正在运行的变量切换到评论中的变量时,它起作用了,这意味着它不是逻辑问题而是 Firebase 服务器无法处理的 URL 问题
我试图弄清楚 firebase API 真正期望的是什么,我从 Firebase 获取的 URL 丢失了什么以及如何将其添加到其中。
最佳答案
2020 年 10 月更新
admin.messaging.Notification
API 已更新为 imageURL
property .
原始答案
Admin SDK 无法发送图像。
负载中没有image
属性。只有 body
和 title
:
https://firebase.google.com/docs/reference/admin/node/firebase-admin.messaging.notification.md#notification_interface
可能的解决方案:尝试为 FCM 调用 HTTP API 而不是调用 sendToDevices
函数。
关于javascript - 上传到 Firebase 存储后,Firebase FCM 不发送图像通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57744268/