【问题标题】:How to create Firebase push Notification Payload on firebase function for flutter push Notification?如何在firebase功能上为flutter push Notification创建Firebase push Notification Payload?
【发布时间】:2020-11-30 13:46:17
【问题描述】:

我正在创建一个使用 Cloud Firestore 作为后端的 Flutter 应用程序。我想触发推送通知以及数组、地图、字符串和图像等多种数据类型的数据,这些数据将在事件添加到事件集合时显示在通知中。

我编写了以下函数,该函数在没有图像和数组以及地图数据类型的情况下正常通知。

exports.eventPushNotification = functions.firestore.document('events/{eventid}').onCreate(async (snaphot, context) => {
    if(snaphot.empty){
        console.log("No data");
        return;
    }

    const eventData = snaphot.data();
    const dateavailable = eventData.available_dates;
    const ticketavailable = eventData.availbale_tickets;
    const coverImage = eventData.availbale_tickets;
    const created_at = eventData.created_at;
    const description = eventData.description;
    const title = eventData.title;
    const updated_at = eventData.updated_at;
    const venue = eventData.venue;
    var tokens =[];
    const deviceTokens =  await db.collection('DeviceTokens').get();
    console.log("Device tokens: ", deviceTokens.toString);
    for(var token of deviceTokens.docs)
    {
        tokens.push(token.data().token);
    }
    

    var payload = {
        notification : {title: "New Event Created", body: "Click here to see the event" },
        data: {
            click_action: "FLUTTER_NOTIFICATION_CLICK",
            message:"Sample Push Message",
            availableDatess : "afsdfa",
            availbale_tickets: "sdfsfa",
            cover: "sfsadf",
            created_at: "dsafsafs",
            description : "safasfs",
            title:"afdasfa",
            updated_at: "afasdfa",
            venue : "afdasf",
                    }
    }

    try{
        const respnse = await admin.messaging().sendToDevice(tokens,payload);
        console.log("Notification sent successfully");
    }
    catch(e){console.log("error while sending push notification", e.toString());
    }
}
);

【问题讨论】:

  • 到目前为止,您对其他数据类型进行了哪些尝试?
  • @YeriPelona 谢谢你的提问。最后,我可以通过使用 JSON.stringify() 函数将对象转换为字符串来传递对象和映射数据类型。

标签: javascript firebase google-cloud-functions firebase-cloud-messaging


【解决方案1】:

仅供参考,正如@ranjit 在他们的评论中提到的那样:

您可以通过使用 JSON.stringify() 函数将对象和映射数据类型转换为字符串来做到这一点。

【讨论】:

    猜你喜欢
    • 2022-12-27
    • 1970-01-01
    • 1970-01-01
    • 2020-08-07
    • 2021-02-10
    • 1970-01-01
    • 2021-07-04
    • 2018-08-03
    • 2014-07-25
    相关资源
    最近更新 更多