使用Firebase功能发送FCM

时间:2020-01-10 07:01:43

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

当我更新Notes应用程序时,我试图将通知发送到测试设备,日志显示成功,但是我没有在设备上收到通知。

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);

exports.onUpdate =
  functions.firestore.document('/NoteBook/{userID}').onUpdate((change: any, context: any) => {

      const notes2 = change.after.data();
      console.log('New Notes Added ' + notes2);

      //Get the notification token of all other users to notify.
      return admin.firestore().collection('fcm-tokens').get().then((snapshotsToken: { empty: any; docs: any; }) => {
        const fcmTokens = [];
        if (snapshotsToken.empty) {
          console.log('No snapshot avaliable');
        } else {
          for (const token of snapshotsToken.docs) {
            fcmTokens.push(token.data().tokens)
          }

          // build notification
          const message = {
            notification: {
              title: "Note Updated",
              body: notes2.title + " " + notes2.details + " " + " added",
              icon: "default",
              sound: "default"
            },
          };

          // Send a message to the device corresponding to the provided
          //Send the notification.
          return admin.messaging().sendToDevice(fcmTokens, message)
            .then(function (result: any) {
              console.log("Notification sent successfully! Note Updated");
              return null;
            })
            .catch(function (error: any) {
              console.log('Notification sent failed', error);
              return null;
            });
        }

      });
    });

这是日志文件 下午4:51:24.649 outlined_flag onUpdate 函数执行开始

4:51:24.649 PM outlined_flag onUpdate 未配置结算帐户。无法访问外部网络,并且配额受到严格限制。配置结算帐户以删除这些限制

4:51:27.561 PM 信息 onUpdate 添加了新注释[object Object]

4:51:29.496 PM 信息 onUpdate 通知发送成功!注意已更新

4:51:29.500 PM outlined_flag onUpdate 函数执行耗时4851毫秒,状态为:“确定”

0 个答案:

没有答案