Where the mind is without fear..
As the figure says, we will send notification messages from our android set to our own server (say Node js web app). In the Node js web app we implemented the admin sdk of Firebase Cloud messaging (FCM) server. The Nodejs web app communicates with the firebase cloud server using its app credentials. The FCM (Firebase Cloud Messaging) server now serve the users of defined categories.
[This article should be read as the appended section of SEND YOUR FIRST ANDROID NOTIFICATION USING FCM (Firebase Cloud Messaging)]
1. In your Firebase console go to Project Settings>Service Accounts
2. In the Service Accounts window click Generate New Private Key. Then in the confirmation window click Generate Key.
3. The Google Credential json file will be downloaded to the downloads folder of the browser.
Keep this file safe.
4. We need to put this file in the project root of our Node js web app.
5. SETUP THE NODEJS WEB SERVER
Now install firebase admin sdk in our server.
In my case my server is an apache shared server. I have node js setup tool in it.
(i) first create a folder in my shared server root directory. Under it I will create my base directory for my nodejs web app.
(ii) it is better to create a demo app in our local WAMP or LAMP server with npm init.
(iii) the main app.js file along with package.json will be created there.
(iv) now upload this app.js along with package.json to our shared server base directory.
(v) Now go to nodejs setup.
(vi) specify Node js app root directory in Application root.
And put the application url.
(vii) Application startup file(app,js) & log file
(viii) click on Edit under Run NPM Install.
(ix) Here enter in the dependency section the firbase-admin as following:
"firebase-admin": ""
(x) Now install packages by clicking Run NPM Install.
(xi) After successful installation open app.js and add the following code in app.js file:
var express = require('express')
var admin = require("firebase-admin");
const port = 2388;
app.get('/', (req,res)=>{
res.status(200).send("Hello World")
})
app.listen(port, () =>{
console.log("listening to port"+port);
})
var admin = require("firebase-admin");
var serviceAccount = require("./myTopic-firebase-msg.json");
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: "htts://your-firebase-database.firebaseio.com"
})
app.post('/firebase/notification', (req, res)=>{
var topic;
var title;
var message;
var body = [];
req.on('error', (error)=>{
console.log(error);
}).on('data', (chunk)=>{
body.push(chunk);
}).on('end', ()=>{
body = Buffer.concat(body).toString();
res.on('error', (err)=>{
console.error('Error Occured :'+err);
});
var json = JSON.parse(body);
topic = json.topic
title = json.title
message = json.body
const messaging = admin.messaging();
var payload = {
notification: {
title: title,
body: message
}
};
messaging.sendToTopic(topic, payload)
.then(result=>{
Console.log('The response: '+result)
});
res.send('Its OK..');
res.end();
})
})
String topic = "urgent"; String title = "Test Notification"; String body = "Meeting called at 8:30 pm";
FirebaseMessaging.getInstance().subscribeToTopic(topicname);
FirebaseMessaging.getInstance().unsubscribeFromTopic(topicname);
