I need assistance with sending push notifications to all devices through a Firebase function.
Here is the Firebase Function code:
const functions = require("firebase-functions");
const express = require("express");
const bodyParser = require("body-parser");
const cors = require('cors');
const request = require('request-promise');
const app = express();
const main = express();
main.use(cors())
main.use('/api/v1', app);
main.use(bodyParser.json());
main.use(bodyParser.urlencoded({ extended: false }));
app.post('/sendNotification', function(req, res) {
console.log(req.body)
let notification = {
"to" : req.body.token,
"collapse_key" : "type_a",
"notification" : {
"body" : "Body of Your Notification",
"title": "Title of Your Notification"
},
"data" : {
"body" : "Body of Your Notification in Data",
"title": "Title of Your Notification in Title",
"key_1" : "Value for key_1",
"key_2" : "Value for key_2"
}
};
const options = {
method: 'POST',
uri: 'https://fcm.googleapis.com/fcm/send',
body: notification,
json: true,
headers: {
'Content-Type': 'application/json',
'Authorization': 'key=MY KEY'
}
}
request(options).then(function (response){
res.status(200).json(response);
})
.catch(function (err) {
console.log(err);
})
});
exports.sendSms = functions.https.onRequest(main);
In my Angular Application,
sendNotification() {
alert('Called, ddd');
this.http.post(this.settings.sendNotification, { token: this.token }).subscribe(res => {
alert('API CALLED');
console.log(res)
});
}
Although the sendNotification
function is triggered on the emulator devices, the API subscribe alert does not execute as expected. Can anyone offer guidance?
Please provide assistance. Thank you!