├── README.md ├── firebase-config.js ├── index.js └── package.json /README.md: -------------------------------------------------------------------------------- 1 | # firebase-nodejs 2 | A tutorial to show how to send Firebase push notifications to mobile devices 3 | To run this code, clone the repository then run npm install to install all necessary dependencies 4 | then run the command node index.js to start the server file 5 | -------------------------------------------------------------------------------- /firebase-config.js: -------------------------------------------------------------------------------- 1 | var admin = require("firebase-admin"); 2 | 3 | var serviceAccount = require("./julla-tutorial.json"); 4 | 5 | 6 | admin.initializeApp({ 7 | credential: admin.credential.cert(serviceAccount), 8 | databaseURL: "https://sample-project-e1a84.firebaseio.com" 9 | }) 10 | 11 | module.exports.admin = admin -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | const express = require('express') 2 | const bodyparser = require('body-parser') 3 | const admin = require('./firebase-config') 4 | 5 | const app = express() 6 | app.use(bodyparser.json()) 7 | 8 | const port = 3000 9 | 10 | const notification_options = { 11 | priority: "high", 12 | timeToLive: 60 * 60 * 24 13 | }; 14 | 15 | 16 | app.post('/firebase/notification', (req, res)=>{ 17 | const registrationToken = req.body.registrationToken 18 | const message = req.body.message 19 | const options = notification_options 20 | 21 | admin.messaging().sendToDevice(registrationToken, message, options) 22 | .then( response => { 23 | 24 | res.status(200).send("Notification sent successfully"+response) 25 | 26 | }) 27 | .catch( error => { 28 | console.log(error); 29 | }); 30 | 31 | }) 32 | 33 | app.listen(port, () =>{ 34 | console.log("listening to port"+port) 35 | }) 36 | 37 | 38 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "firebase", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "keywords": [], 10 | "author": "", 11 | "license": "ISC", 12 | "dependencies": { 13 | "firebase-admin": "^8.4.0" 14 | } 15 | } 16 | --------------------------------------------------------------------------------