在Firebase推送通知中发送图像

发布于 2025-02-06 17:22:24 字数 2657 浏览 2 评论 0原文

我希望能够在Firebase发送的推送通知上显示一个迷你图像。我遇到的问题是我使用一个有效负载,有时图像中的参数可以为null(未发送图像时),我正在阅读本文:在通知有效载荷中发送图像

但始终在我的控制台上出现错误:

export const sendChatNotification = functions.firestore.document('chatrooms/{chatroomId}/chats/{chatId}').onCreate(async (snap, ctx) => {

    //In Comment OLD Way to get info data for the notification
    const token = snap.get('sendToDeviceToken'); //snap.data().sendToDeviceToken;
    const sender = snap.get('fullSenderName'); //snap.data().fullSenderName;
    const body = snap.get('message'); //snap.data().message;
    //const platform = snap.get('sendToDevicePlatform');

    var tokens  = [];

    tokens.push(token);

    console.log('Tokens: ' + tokens);

    var imageInfo = body.substring(0, 38);

    var message;
    var image;

    if(imageInfo === 'https://firebasestorage.googleapis.com') {
        message = 'Image Received';
        image = body;
    } else {
        message = body;
        image = null;
    }

    const payload = {
        tokens: tokens,
        notification: {
            title: sender,
            body: message,
        },
        android: {
            notification: {
                channel_id: "roofdeck_default",
                title: sender,
                body: message,
                notification_priority: 'priority_max',
                color: '#DDE9F7',
                icon: '@mipmap/ic_launcher',
                image_url: image,
            },
        },  // El Codigo Comentariado aqui es para mostrar Badge en 1 con nuevos chats.
        apns: {
            payload: {
                aps: {
                    badge: 1,
                },
                fcm_options: {
                    image: image,
                  },
            },
            webpush: {
                headers: {
                  image: image,
                }
              },
        }, // Hasta Aqui el Codgio del Badge.
        // android: {
        //     notification: {
        //         notificationCount: 0,
        //         channelId: 'roofdeck_default',
        //     }
        // }
    };
    
    fcm.sendMulticast(payload).then((response) => {
        // Response is a message ID string.
        console.log('Successfully sent message:', response);
        return {success: true};
    }).catch((error) => {
        console.log('Error on sent message:', error);
        return {error: error.code};
    });

});

问题是有时是图像是图像字段将为null(用户仅发送文本时),并且不应发送任何图像。

关于如何解决行为的任何想法。

亲切的问候

I want to be able to display a mini image on the Push Notifications sent from Firebase. The problem that I'm having is that I use a single payload and sometimes the parameter in Image can be null (when no image is sent), I was reading this article: Send an image in the notification payload

But always got an error on my Console like this:

export const sendChatNotification = functions.firestore.document('chatrooms/{chatroomId}/chats/{chatId}').onCreate(async (snap, ctx) => {

    //In Comment OLD Way to get info data for the notification
    const token = snap.get('sendToDeviceToken'); //snap.data().sendToDeviceToken;
    const sender = snap.get('fullSenderName'); //snap.data().fullSenderName;
    const body = snap.get('message'); //snap.data().message;
    //const platform = snap.get('sendToDevicePlatform');

    var tokens  = [];

    tokens.push(token);

    console.log('Tokens: ' + tokens);

    var imageInfo = body.substring(0, 38);

    var message;
    var image;

    if(imageInfo === 'https://firebasestorage.googleapis.com') {
        message = 'Image Received';
        image = body;
    } else {
        message = body;
        image = null;
    }

    const payload = {
        tokens: tokens,
        notification: {
            title: sender,
            body: message,
        },
        android: {
            notification: {
                channel_id: "roofdeck_default",
                title: sender,
                body: message,
                notification_priority: 'priority_max',
                color: '#DDE9F7',
                icon: '@mipmap/ic_launcher',
                image_url: image,
            },
        },  // El Codigo Comentariado aqui es para mostrar Badge en 1 con nuevos chats.
        apns: {
            payload: {
                aps: {
                    badge: 1,
                },
                fcm_options: {
                    image: image,
                  },
            },
            webpush: {
                headers: {
                  image: image,
                }
              },
        }, // Hasta Aqui el Codgio del Badge.
        // android: {
        //     notification: {
        //         notificationCount: 0,
        //         channelId: 'roofdeck_default',
        //     }
        // }
    };
    
    fcm.sendMulticast(payload).then((response) => {
        // Response is a message ID string.
        console.log('Successfully sent message:', response);
        return {success: true};
    }).catch((error) => {
        console.log('Error on sent message:', error);
        return {error: error.code};
    });

});

The problem is that sometimes the image field would be null (when the user only sends text) and it should not send any images.

Any ideas on how to fix the behavior.

Kind Regards

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凡尘雨 2025-02-13 17:22:24

您可以验证image.isnotnull()检查服务是否使用图像响应。然后,如果不是零,则可以显示图像。

You could validate image.isNotNull() to check if the service respond with an image or not. Then, if it is not null, you can show the image.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文