【问题标题】:How to make the notification triggered after mojify applied Firebase Cloud Function Realtime Database如何在 mojify 应用 Firebase Cloud Function 实时数据库后触发通知
【发布时间】:2019-12-24 20:55:13
【问题描述】:

我想在 mojify 文本后显示通知。它应该同时运行还是触发 mojify then 通知? 如何让它发生?谢谢。

我正在使用 Firebase Cloud Function 实时数据库。

使用onCreate的通知函数

exports.sendAdminGroupNotification = functions.database.ref('/adminChat/{adminChatUid}')
    .onCreate(async (snap, context) => {

使用onWriteMojify 函数

exports.emojify =
    functions.database.ref('/adminChat/{adminChatUid}/message')
        .onWrite(async (change, context) => {
            console.log("emojifying!");
            // Get the value from the 'text' key of the message
            const originalText = change.after.val();
            const emojifiedText = emojifyText(originalText);
        // Return a JavaScript Promise to update the database node
        return admin.database().ref("/adminChat/" + context.params.adminChatUid + "/").update({ message: emojifiedText });
    });

【问题讨论】:

    标签: firebase firebase-realtime-database google-cloud-functions


    【解决方案1】:

    最简单的方法是在update上使用回调

    admin.database().ref("/adminChat/" + context.params.adminChatUid + "/")
    .update({ message: emojifiedText }), function(error) {
      if (error) {
        // The write failed...
      } else {
        // Data saved successfully!
        //Send notification
      }
    })
    

    或在上述引用上使用onUpdate 触发器而不是onWrite

    【讨论】:

      【解决方案2】:

      您可以通过多种方式做到这一点。如果您希望仅在消息更新后通知用户,则可以使用 onUpdate() 触发器

      exports.sendAdminGroupNotification = functions.database.ref('/adminChat/{adminChatUid}')
          .onUpdate(async (snap, context) => { ... });
      

      或者在完成表情和更新消息后调用 sendNotification(...) 函数:

      exports.emojify =
          functions.database.ref('/adminChat/{adminChatUid}/message')
              .onWrite(async (change, context) => {
                  console.log("emojifying!");
                  // Get the value from the 'text' key of the message
                  const originalText = change.after.val();
                  const emojifiedText = emojifyText(originalText);
              // Return a JavaScript Promise to update the database node
              admin.database().ref("/adminChat/" + context.params.adminChatUid +"/")
              .update({ message: emojifiedText }).then(() => {
                      console.log('emojified!');
                      sendNotification(message);
              });
          });
      

      有关该主题的更多信息Sync, async, and promises

      【讨论】:

        【解决方案3】:

        我认为不可能让它工作。

        【讨论】:

          猜你喜欢
          • 2018-08-09
          • 2018-01-10
          • 2019-05-06
          • 1970-01-01
          • 2022-01-04
          • 2018-10-12
          • 2020-10-26
          • 2020-10-01
          • 2018-06-25
          相关资源
          最近更新 更多