Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/firebase/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在Firebase函数上为颤振推送通知创建Firebase推送通知有效负载?_Javascript_Firebase_Google Cloud Functions_Firebase Cloud Messaging - Fatal编程技术网

Javascript 如何在Firebase函数上为颤振推送通知创建Firebase推送通知有效负载?

Javascript 如何在Firebase函数上为颤振推送通知创建Firebase推送通知有效负载?,javascript,firebase,google-cloud-functions,firebase-cloud-messaging,Javascript,Firebase,Google Cloud Functions,Firebase Cloud Messaging,我正在创建一个flutter应用程序,其中我使用CloudFireStore作为后端。我想触发推送通知以及多种数据类型的数据,如数组、映射、字符串和图像,当事件添加到事件集合时,这些数据将显示在通知中 我已经编写了以下函数,该函数在没有图像、数组和映射数据类型的正常通知中运行良好 exports.eventPushNotification = functions.firestore.document('events/{eventid}').onCreate(async (snaphot, con

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

我已经编写了以下函数,该函数在没有图像、数组和映射数据类型的正常通知中运行良好

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.stringify()函数将对象和映射数据类型转换为字符串来实现这一点


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