-
Set up Node.js
Download Node.js from https://nodejs.org/en/
-
Set up Firebase CLI
$ npm install -g firebase-tools
-
Initialize Firebase SDK for Cloud Functions
$ firebase login $ firebase init functions
-
Paste the following code to functions/index.js
//import firebase functions modules const functions = require('firebase-functions'); //import admin module const admin = require('firebase-admin'); admin.initializeApp(functions.config().firebase); // Listens for new messages added to messages/:pushId exports.pushNotification = functions.database.ref('/messages/{pushId}').onWrite( event => { console.log('Push notification event triggered'); // Grab the current value of what was written to the Realtime Database. var valueObject = event.data.val(); if(valueObject.photoUrl != null) { valueObject.photoUrl= "Sent you a photo!"; } // Create a notification const payload = { notification: { title:valueObject.msgUser, body: valueObject.msgText || valueObject.photoUrl, sound: "default" }, }; //Create an options object that contains the time to live for the notification and the priority const options = { priority: "high", timeToLive: 60 * 60 * 24 }; return admin.messaging().sendToTopic("pushNotifications", payload, options); });
-
Deploy the function
$ firebase deploy
-
Notifications
You must be signed in to change notification settings - Fork 1
LeonardChin2017/firebase-functions-pushnotification
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
About
Source code for firebase to send notification to the users when there is data update in the database.
Topics
Resources
Stars
Watchers
Forks
Releases
No releases published
Packages 0
No packages published