4

I wrote some code to send a notification to all my user that register to an event, I use http trigger that will be called when event i starting, I can not send the notification to device group because the event maybe different even though the starting time is same.

My firebase node looks something like this:

registration
    |-- userid1
    |       |-- registration1
    |       |       |-- FirebaseToken
    |       |       |-- EventName
    |       |-- registration2
    |               |-- FirebaseToken
    |               |-- EventName
    |-- userid2

and this is my scriptso far:

exports.sendNotification = functions.https.onRequest((request, result) => {
    admin.database().ref('/registration').once('value').then(snapshot => {
        snapshot.forEach(childSnapshot => {
            let list = childSnapshot.val()
            for (let key in list) {
                let p = list[key];
                let token = p.FirebaseToken;
                console.log("Device token:", token);

                if (token != null && token != "") {

                    let payload = {
                        notification: {
                            title: `The ${p.EventName}`,
                            body: `The ${p.EventName} will be started soon.`,
                            sound: "default"
                        }
                    };

                    // Set the message as high priority and have it expire after 1 hours.
                    let options = {
                        priority: "high",
                        timeToLive: 60 * 60
                    };

                    admin.messaging().sendToDevice(token, payload, options)
                        .then(response => {
                            console.log("Successfully sent message:", response);
                        })
                        .catch(error => {
                            console.log("Error sending message:", error);
                        });
                }
            };
        });

        result.status(200).send("ok");
    });
});

with this script, I managed to send the notification to user, but I got a log something like this Function execution took 1389 ms, finished with status code: 304

Is there any correction or suggestion to do this.

SIRS
  • 624
  • 6
  • 20

0 Answers0