简体   繁体   English

在实时数据库中存储Firebase云消息

[英]Store Firebase Cloud Message in Realtime Database

When you send a cloud message via the Firebase console, is it possible to store the text of that message as a value and a timestamp as the key in the same project's realtime database? 当您通过Firebase控制台发送云消息时,是否可以将该消息的文本存储为同一项目的实时数据库中的值和时间戳作为密钥? If so, how? 如果是这样,怎么办? My end goal is to have a history of notifications and the time at which they were sent visible to users in my app. 我的最终目标是在我的应用程序中看到通知的历史记录以及向用户发送通知的时间。 Thank you! 谢谢!

Thanks to urgentx pointing me in the right direction, I found a solution in a Firebase HTTP Cloud Function. 多亏了Emergencyx为我指明了正确的方向,所以我在Firebase HTTP Cloud Function中找到了一个解决方案。 The URL to call it looked like this (spaces and punctuation are allowed as notification text): 调用它的URL如下所示(允许使用空格和标点符号作为通知文本):

https://[REGION]-[MY-APP-ID].cloudfunctions.net/notification?password=[PASSWORD]&notification=[NOTIFICATION]

Below is the index.js file. 以下是index.js文件。 I had to write code in Android Studio to subscribe each device to the all-devices topic. 我必须在Android Studio中编写代码,以使每个设备都订阅“所有设备”主题。

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

function format(number) {
  if (number.toString().length < 2) {
    return "0" + number;
  }
  return number;
}

exports.notification = functions.https.onRequest((request, response) => {
    if (request.query.password == "[PASSWORD]") {
        var message = {
            "notification": {
              "body": request.query.notification
            },
            "topic": "all-devices"
        };
        admin.messaging().send(message);
        var nowUTC = new Date();
        var nowEDT = new Date(nowUTC.getFullYear(), nowUTC.getMonth(), nowUTC.getDate(), nowUTC.getHours() - 4, nowUTC.getMinutes(), nowUTC.getSeconds());
        var timestamp = nowEDT.getFullYear() + ":" + format(nowEDT.getMonth()) + ":" + format(nowEDT.getDate()) + ":" + format(nowEDT.getHours()) + ":" + format(nowEDT.getMinutes()) + ":" + format(nowEDT.getSeconds());
        var JSONString = "{\"" + timestamp + "\":\"" + request.query.notification + "\"}";
        admin.database().ref("/notifications").update(JSON.parse(JSONString));
        response.send("Request to server sent to send message \"" + request.query.notification + "\" at timestamp " + timestamp + " and store in database. Await notification and check database if confirmation is needed.");
    } else {
        response.send("Password incorrect. Access denied.");
    }
});

Below is the code that subscribed the device to the all-devices topic. 以下是将设备订阅到所有设备主题的代码。

FirebaseMessaging.getInstance().subscribeToTopic("all-devices")
                .addOnCompleteListener(new OnCompleteListener<Void>() {
                    @Override
                    public void onComplete(@NonNull Task<Void> task) {
                    }
                });

You will be sending the notifications via either your own server or a Google cloud function. 您将通过自己的服务器或Google云功能发送通知。 You can store the text when you send a notification using those. 当您使用文本发送通知时,可以存储文本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM