首页 > 解决方案 > Firebase 功能 + Firestore + 向多个令牌发送通知

问题描述

我创建了以下函数来向特定fcmToken发送通知

/**
 * FOR ADMIN
 */
exports.adminNotification = functions.firestore.document('/pending_videos/{pushId}').onCreate(async (snap, context) => {

    const newValue = snap.data();
    const title = newValue.title;
    const category = newValue.category;
    const uploadedBy = newValue.uploadedBy;

    // Grab the current value of what was written to the Realtime Database.

    let adminRef = admin.firestore().collection('admin').doc('57MRzgiBwx2gO2JXZ4jo');
    let token;
    let getDoc = adminRef.get()
      .then(doc => {
        if (!doc.exists) {
          console.log('No such document!');
        } else {
            console.log('Document data:', doc.data().url);
            token = doc.data().token;

            // Create a notification
            const payload = {
                data: {
                    title: "Jain Stavan Video Status",
                    body:  title + " video uploaded in " + category + " category by " + uploadedBy + ".",
                    sound: "default"
                },
            };

            //Create an options object that contains the time to live for the notification and the priority
            const options = {
                priority: "high",
                timeToLive: 60 * 60 * 24
            };

            console.log('Sending notification');

            return admin.messaging().sendToDevice(token, payload, options);
        }
      })
      .catch(err => {
        console.log('Error getting document', err);
      });
});

我已经使用静态文档.doc('57MRzgiBwx2gO2JXZ4jo')完成了它,但我想将它发送给所有管理员,这意味着管理员集合中的所有文档。

我怎样才能做到这一点?

标签: firebasegoogle-cloud-firestorepush-notificationgoogle-cloud-functionsfirebase-cloud-messaging

解决方案


推荐阅读