I am making a mobile app with NativeScript technology and I have a PHP backend panel for sending push notifications , I understand that from the app I need to send the token to a DB (in this case I want to use Firebase), and then use the api from firebase to take the tokens to the PHP backend panel to be able to send push notifications through a form to all devices. I have the google-services files included in the Android and iOS directories.
I have the following code:
app.js
var applicationModule = require("application");
applicationModule.start({ moduleName: "main-page" });
var pushPlugin = require("nativescript-push-notifications");
var pushSettings = {
senderID: "XXXXXXXXXXXX", // Required: setting with the sender/project number
notificationCallbackAndroid: function (stringifiedData, fcmNotification) {
var notificationBody = fcmNotification && fcmNotification.getBody();
_this.updateMessage("Message received!\n" + notificationBody + "\n" + stringifiedData);
}
};
pushPlugin.register(pushSettings, function (token) {
alert(token);
}, function() { });
Package.json dependencies
"dependencies": {
"nativescript-push-notifications": "^1.1.4",
"nativescript-theme-core": "~1.0.4",
"tns-core-modules": "^3.4.1"
}
From my PHP backend the push notification is sent perfectly to the device, but only to one since I have to manually write the token for it in the code.
The goal is to save the generated tokens in firebase and then go through them in the PHP backend , but I don't know how to do it because I don't understand this whole concept very well, any help please?
I don't have a way to validate at the moment, but it would be best if you switch to
nativescript-plugin-firebase
, since it has access to the different Firebase products in the same plugin.You could get the token and send it to your database
Realtime Database
or toFirestore
, then in your backend get access to the list of tokens.For example, on your device:
In your backend you can then check in
Firebase Realtime Database
/tokensDeDispositivos/
, which should be an array with the tokens of all the devices that have been registered.You can do that with
firebase-php
. But that is another topic.Later you can do something to keep the token list up to date and clean of devices that have lost or changed their token. But that is another topic.