Firebase push notification when new data is added in Database


#1

How do I send notification to all users that have my app either on foreground or background?

for example, I have a seperate app that adds data on firebase, lets call it “post”. Every added post will be displayed in the users app with a listview. Could it be possible that whevener I add a post, all the users will recieve push notification either on foreground or background?

also i took reference with the following:

https://github.com/NativeScript/push-plugin
https://github.com/eddyverbruggen/nativescript-local-notifications

and have seen similar topics here.


#2

i got the same problem. the way we send notification through API is make all user register to same topics (for example ‘new data’. so when you want to send to all user you just send it to topics and all user subscribe to topics ‘new data’ will receive notifications. But the problem is i dont know how to register topic with push plugin.


#3

Hi @shafrudin, to subscribe the user to a topic you should following this:

https://github.com/EddyVerbruggen/nativescript-plugin-firebase/blob/master/docs/MESSAGING.md#send-messages-to-topics

once subscribed you can send them notifications through firebase console:

Firebase > Project Overview > GROW > Cloud Messaging

If you want to send them notifications through another app for convenience.
heres the code I used to notify them together with firebase push new data function.

delcare this first:
const httpModule = require("http");

httpModule.request({
                url: "https://fcm.googleapis.com/fcm/send",
                method: "POST",
                headers: {'Authorization': 'key=AIzaSyA95mMSnwSIbahgZ8ebnUxxxxxxxxxxxxxxx', 'Content-Type': 'application/json' },
                content: JSON.stringify({
                    "notification" : {
                        "body": "Notication description",
                        "title": "Notification Title",
                        "sound": "default",
                        "priority": "High",
                        "time_to_live": "172800", 

                    },
                    'to': "/topics/advisory" // its is mandatory to have /topics/ before the topic name
                })
            }).then(function(response) {
                //const result = response.content.toJSON();
                console.log(JSON.stringify(response));
            }, function (e) {
                  console.log("Error occurred " + JSON.stringify(e));
            }).then(
      function (result) {
        console.log("created key: " + result.key);
      }
);

here are some references:

https://stackoverflow.com/questions/42277861/using-ns-http-request-to-send-a-curl

https://stackoverflow.com/questions/37371990/how-can-i-send-a-firebase-cloud-messaging-notification-without-use-the-firebase?rq=1

for notification options:

https://firebase.google.com/docs/cloud-messaging/http-server-ref#send-downstream

GO CRAZY!


#4

Thanks broo. I did it.