如何在Firebase功能上为flutter推送通知创建Firebase推送通知有效负载



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

我已经编写了以下函数,该函数适用于没有图像、数组和映射数据类型的正常通知。

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());
}
}
);

仅供参考,正如@ranjit在其评论中所提到的:

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

相关内容

  • 没有找到相关文章

最新更新