【问题标题】:Firebase Cloud Functions send notification weird outputFirebase Cloud Functions 发送通知奇怪的输出
【发布时间】:2019-03-25 04:44:11
【问题描述】:

我正在创建一个这样的函数来向新用户发送通知。

exports.sendCreateSuccessNotification = functions.firestore
  .document("users/{userId}")
  .onCreate((snap, context) => {
    const newVal = snap.data();
    const payload = {
      notification: {
        title: "Create Account Success",
        body: "Hello " + newVal.emailUser
      }
    };
    admin.messaging().sendToDevice(newVal.notiToken, payload);
  });

输出有错误行 但我的设备收到了通知。

当我将代码更改为下面的返回时

exports.sendCreateSuccessNotification = functions.firestore
  .document("users/{userId}")
  .onCreate((snap, context) => {
    const newVal = snap.data();
    const payload = {
      notification: {
        title: "Create Account Success",
        body: "Hello " + newVal.emailUser
      }
    };
    return admin.messaging().sendToDevice(newVal.notiToken, payload);
  });

输出没有错误,但我的设备没有收到任何通知。 谁能给我解释一下?谢谢!!

【问题讨论】:

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


    【解决方案1】:

    这是意料之中的事情,您可以在documentation 中了解这一点。

    对于 Firestore(和所有后台)函数,您必须返回一个仅在函数中的所有异步工作完成后才解析的 Promise。发送通知是异步的,因为sendToDevice 返回一个承诺。通过返回该承诺,您向 Cloud Function 发出信号,表明它的所有工作都已完成,并且可以安全清理。

    【讨论】:

    • 我不知道。谢谢你的信息。我们怎样才能等待sendToDevice。我尝试了 async 和 await 但不能。
    • 这听起来应该是一个全新的问题。
    • 我用 Async 和 Await 解决了这个问题。问题是我以前不能使用它。添加“引擎:节点8”后,它就可以工作了
    猜你喜欢
    • 2017-11-16
    • 1970-01-01
    • 2018-01-06
    • 2020-05-11
    • 1970-01-01
    • 2020-09-03
    • 2017-10-29
    • 1970-01-01
    • 2017-09-12
    相关资源
    最近更新 更多