【问题标题】:IoT project, How to get a andorid notification when a value is UPDATED in the realtime databe?IoT 项目,如何在实时数据库中更新值时获取 android 通知?
【发布时间】:2021-08-13 05:37:20
【问题描述】:

我正在做一个物联网项目,我将数据从 7 个不同的 NodeMCU 传感器节点发送到 FIREBASE 实时数据库。我正在使用 Android APP 作为 HMI,用户可以使用 APP 在数据库中读写。

我想要做的是,当一个节点在数据库中更新一个值时,用户会收到通知,然后用户进入或退出 APP。我已经发送了大量的教程,其中用户使用 APP 在数据库中写入,然后接收他所写内容的通知,这里节点更新值,应用程序通知用户值已更新。

我试过了。

云功能,我设置了 onUpdate 打字稿功能,但我没有在控制台日志中显示更改。

我会向你展示我的 Firebase 结构。

如您所见,节点更新的值是真或假,我需要当 ACT/IN1 更新为真时,用户会收到通知。

希望a让自己足够清楚,以便您可以帮助我。

【问题讨论】:

    标签: android typescript firebase push-notification


    【解决方案1】:

    您可以使用 OnTrigger 事件从 Cloud Functions 触发此操作。

    exports.makeUppercase = functions.database.ref('/datos/ACT/{item}')
        .onUpdate((change, context) => {
          const newValue = change.after.val();
          const oldValue = change.before.val();
          // Send FCM here
        });
    

    从那里,您可以使用逻辑来确定是否要发送通知。

    const message = {
      data: {
        score: '850',
        time: '2:45'
      },
      token: registrationToken
    };
    
    // Send a message to the device corresponding to the provided
    // registration token.
    admin.messaging().send(message)
      .then((response) => {
        // Response is a message ID string.
        console.log('Successfully sent message:', response);
      })
      .catch((error) => {
        console.log('Error sending message:', error);
      });
    

    您只需要从设备中查找或存储注册令牌即可知道将通知发送到哪里,这可以根据文档找到

    【讨论】:

    • 嗨,我没有足够的 REP 来投票,它运行良好,非常感谢您的帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-26
    • 1970-01-01
    • 2019-12-02
    相关资源
    最近更新 更多