【问题标题】:Send Notification when Document is Created using Cloud Function使用 Cloud Function 创建文档时发送通知
【发布时间】:2020-06-23 06:21:04
【问题描述】:

我使用 Firebase 和 Firestore 作为数据库

我想在用户通知集合中创建文档时推送新通知。

应该是这样的,

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

// // Create and Deploy Your First Cloud Functions
// // https://firebase.google.com/docs/functions/write-firebase-functions
//
// exports.helloWorld = functions.https.onRequest((request, response) => {
//  response.send("Hello from Firebase!");
// });

exports.sendNotificationToTopic = functions.firestore.document('notification/{notificationId}').onWrite(async  (event) => {

  var message ={
      notification: {
          title: "This is Title",
          body: "This is the Body",
          user: "fA0eLup_TUKVzdBkQbd3Qb:APA91bHuSuZOjdJgXn6xBndhQQkfnzm1pdLl9x3B2FZUYcYQULVPJFRbERezPuLoXD4QCUU2yalLUvgRfEas4B0sKAcwOkcmGkudLFvQWqTT7uhG21pKffTNTz5GvWKcD2-hKkfPq9Gq"

      },

      "token": user


  };

  let response=await admin.messaging().send(message);
  console.log(response);

});

是的,我正在使用云功能

当我在通知集合中创建文档时,我没有收到通知

我在云功能日志中遇到了一些错误

sendNotificationToTopic 
Error: Exactly one of topic, token or condition is required at FirebaseMessagingError.FirebaseError [as constructor] (/srv/node_modules/firebase-admin/lib/utils/error.js:42:28) at FirebaseMessagingError.PrefixedFirebaseError [as constructor] (/srv/node_modules/firebase-admin/lib/utils/error.js:88:28) at new FirebaseMessagingError (/srv/node_modules/firebase-admin/lib/utils/error.js:254:16) at Object.validateMessage (/srv/node_modules/firebase-admin/lib/messaging/messaging-types.js:46:15) at Messaging.send (/srv/node_modules/firebase-admin/lib/messaging/messaging.js:207:27) at exports.sendNotificationToTopic.functions.firestore.document.onWrite (/srv/index.js:22:40) at cloudFunction (/srv/node_modules/firebase-functions/lib/cloud-functions.js:131:23) at /worker/worker.js:825:24 at <anonymous> at process._tickDomainCallback (internal/process/next_tick.js:229:7) 

【问题讨论】:

    标签: android push-notification google-cloud-firestore google-cloud-functions firebase-notifications


    【解决方案1】:

    将令牌添加到您的 var 消息变量。

    { "notification": { title: "This is Title", body: "This is the Body", }, "token": user_info.device_id } 您可以从 Tools --> Firebase --> Cloud Messaging --> Step 4 Firebase cloud messing step 4 implementation 中获取令牌,简要说明一下。

    【讨论】:

    • 如何生成令牌?
    • 我生成了令牌并添加到我的文件中,但出现错误 ReferenceError: user is not defined at exports.sendNotificationToTopic.functions.firestore.document.onWrite (/srv/index.js:22:16) at cloudFunction (/srv/node_modules/firebase-functions/lib/cloud-functions.js:131:23) at /worker/worker.js:825:24 at at process._tickDomainCallback (internal/process/next_tick.js :229:7)
    猜你喜欢
    • 2021-05-15
    • 2018-06-20
    • 1970-01-01
    • 1970-01-01
    • 2019-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多