【问题标题】:Firebase FCM doesn't send image notification after uploading to Firebase storageFirebase FCM 在上传到 Firebase 存储后不发送图像通知
【发布时间】:2020-01-04 17:49:35
【问题描述】:

我已将用户图库中的图片上传到 Firebase 存储并获得以下 URL -

https://firebasestorage.googleapis.com/v0/b/tnn-1mddev.appspot.com/o/notificationImages?alt=media&token=9a91bfe6-ae7a-4560-9f42-0adbd49de2e9

在谷歌浏览器上打开时是一个有效的图像 URL。

问题是,当我尝试通过以下方法将其发送到 Firebase 服务器时,我的来自 firebase 的 URL 不起作用 -


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 缺少什么以及如何将其添加到其中。

【问题讨论】:

    标签: javascript android firebase firebase-cloud-messaging firebase-storage


    【解决方案1】:

    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 函数。

    【讨论】:

    • 文档的链接似乎已过时
    【解决方案2】:

    每个通知可以发送的图片大小限制为几百 KB - 检查您发送的图片有多大

    【讨论】:

      猜你喜欢
      • 2022-08-22
      • 2017-09-25
      • 1970-01-01
      • 1970-01-01
      • 2022-08-15
      • 2020-09-08
      相关资源
      最近更新 更多