Reputation: 762
I am creating a flutter application in which I have used cloud firestore as a backend. I want to trigger push notification along with data of multiple data types like array, maps , string and image which will display in notification when event is added to event collection.
I have written following function which is working fine for normal notification with out image and arrays and maps data types.
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());
}
}
);
Upvotes: 0
Views: 213
Reputation: 3789
Just for reference and as mentioned by @ranjit in their comment:
You can do this by converting objects and map data types into string using JSON.stringify() function.
Upvotes: 1