javascript - 上传到 Firebase 存储后,Firebase FCM 不发送图像通知

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

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

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

在谷歌浏览器中打开时是一个有效的图像 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 属性。只有 bodytitle: 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/

相关文章:

android - FileOutputStream 类型中的方法 write(byte[]) 不适用于参数 (Drawable)

swift - 在 Firebase 中创建一个实时观察器,始终使用 Swift 更新更改

Node.js 将图像上传到 firebase 存储

node.js - 对公共(public)类使用共享 Node 模块

javascript - 快速获取文件路径的正则表达式

javascript - 遍历 AST 时区分运算符优先级

javascript - Express.js 与 Node.js,多个客户端

javascript - TITANIUM 如何获取 ListView 的值?

java - Android Speech to text API 从 onActivityResult 方法中获取变量值

android - 路径曲线周围的区域