Q: Creating a notification history with Firebase Realtime Database In my app a user can join a group and post something. Others can comment or like that post.
My database is separated in the group ID (I called it here place ID) and the users.
The structure in the realtime database looks like this:
-String placeID
    -the variable place ID
        -autogenerated message ID (first posted message in group)
            -Username (variable)
            -UserComment (variable)
            -UserTime (variable) …
                -String Subcomment
                    -autogenerated subcommand ID (first comment of someone)
                        -Username (variable)
                        -UserComment (variable)
                        -UserTime (variable)
                    -autogenerated subcommand ID (second comment of someone)
                        -Username (variable)
                        -UserComment (variable)
                        -UserTime (variable)
                    -…
-String User
    -autogenerated user ID
        -User first name
        -User last name 
        -…
    -autogenerated user ID
        -User first name
        -User last name 
        -…



If a user posts something and someone else comments it then everyone who follows this topic gets notified via push notification on his device. Here a I used the subscribe method.
I used the following code in javascript to notify everyone who follows the topic.
const functions = require("firebase-functions");
const admin = require("firebase-admin");
admin.initializeApp();
 
exports.Push = functions.database
    .ref("/placeID/{groupID}/{messageID}/subcomment/{subMessageID}")
    .onCreate((snapshot, context) => {
 
        testen().then(
            function (value) {
                pushsenden(); // Success!
            },
            function (reason) {}
        );
 
        function testen() {
            return new Promise(function (resolve, reject) {
                groupID = context.params.groupID;
                messageID = context.params.messageID;
               subMessageID = context.params.subMessageID;
                var db = admin.database();
                var ref = db.ref("/placeID/" + groupID + "/" + messageID + "/subcomment/" + subMessageID );
                ref.on(
                    "value",
                    function (snapshot) {
                        nameUser = snapshot.val().userName;
                        console.log("Username: " + nameUser);
                        userID = snapshot.val().userID
                        pushPlaceiD = groupID
                        pushCommentiD = messageID
                        commentUser = snapshot.val().userComment;
                        console.log("Usecomment: " + commentUser);
                        userTime = snapshot.val().userTime;
                        console.log(userTime);

                        resolve();
                    },
                    function (errorObject) {
                        reject(errorObject);
                    }
                );
            });
        }
 
        function pushsenden() {
            console.log("jetzt kommt pushsenden");
            var topic = messageID;
            const payload = {
                notification: {
                    title: 'Neuer Beitrag von ' + nameUser,
                    body: commentUser,
                    badge: "1",
                    sound: "default",
                },
                data: {
                    placeID: pushPlaceiD,
                    commentID: pushCommentiD,
                    
                  }
            };
            console.log("sending push notification");
            admin.messaging().sendToTopic(topic, payload);
        console.log("sent notification");

        }
    });

Till here everything works perfect.
But here comes the big question.
I also want to have a tab or a window in the app where the user can see the last 10 notification he received. Like a history. Something like the notification window in Facebook. How can I do that? I probably have to save the notification and the content on the user side and show it as a list or should I save it one the group ID. What is the best approach?
Added code:
 var ref = db.ref('/user/' + userID + '/notification/'+ subMessageID + '/subMessageID/').set(subMessageID);
            var ref = db.ref('/user/' + userID + '/notification/'+ subMessageID + '/userTime/').set(userTime);
            var ref = db.ref('/user/' + userID + '/notification/'+ subMessageID + '/userName/').set(nameUser);
            var ref = db.ref('/user/' + userID + '/notification/'+ subMessageID + '/userComment/').set(commentUser);
            var ref = db.ref('/user/' + userID + '/notification/'+ subMessageID + '/placeID/').set(groupID);
            var ref = db.ref('/user/' + userID + '/notification/'+ subMessageID + '/messageID/').set(messageID);
            var ref = db.ref('/user/' + userID + '/notification/'+ subMessageID + '/read/').set("false");


A: I always recommend to model your database to reflect the data that you show on the screens of your app. So if you have a place where you want to show the 10 most recent notifications for each user, I'd also create exactly that data structure in the database.
The easiest way to do that seems to me to store each new notification for each user as you also send the corresponding push notification.
Since you want to show only the 10 most recent notifications, you can either prune the list of notifications to the last 10 whenever you add a new notification to it, do this periodically, or you can never remove any notifications and instead only query for the 10 most recent ones.
