为什么此Cloud Function无法触发通知?

时间:2018-12-09 18:10:35

标签: node.js firebase-cloud-messaging google-cloud-functions

我正在Firebase中编写一个Cloud Function,该功能将通知发送给带有令牌的设备,令牌存储为文档中的集合:

const functions = require('firebase-functions');
const admin = require('firebase-admin');

admin.initializeApp();

exports.sendDMNotification = functions.firestore.document('/dm_threads/{thread_id}/messages/{message_id}')
    .onCreate((snapshot, context) => {


        const newMessage = snapshot.data();

        const senderName = newMessage.authorName;
        const senderID = newMessage.authorUID;
        const messageText = newMessage.message;
        const recipientName = newMessage.recipientName;
        const recipientID = newMessage.recipientUID;
        const timestamp = newMessage.timestamp;

        console.log(senderName + " " + senderID + " " + messageText + " " + recipientName + " " + recipientID + " " + timestamp.toString());

        let deviceTokenQuery = admin.firestore().collection(`/Users/${recipientID}/device_tokens/`);

        return deviceTokenQuery.get().then(querySnapshot => {
            console.log('deviceTokenQuery returned');

            let tokens = querySnapshot.docs;

            console.log('1');


            const notificationPromises = tokens.map(token => {
                console.log('2');

                let token_id = token.tokenID.toString();

                console.log('3');

                const payload = {
                    notification: {
                        title: senderName,
                        body: messageText,
                        icon: "default"
                    }
                };


                return admin.messaging().sendToDevice(token_id, payload)

            });
            console.log('4');
            return Promise.all(notificationPromises);

        });

    });

仅显示“返回的deviceTokenQuery”,“ 1”和“ 4”日志语句。我是否有语法问题,或者我没有正确使用这些方法?

0 个答案:

没有答案