Created
October 24, 2017 16:34
-
-
Save ricardodantas/91d2817338832e67c14c06de069a499f to your computer and use it in GitHub Desktop.
Sample showing how to use Firebase Cloud Function to send push notification for a topic.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const functions = require('firebase-functions'); | |
const admin = require('firebase-admin'); | |
admin.initializeApp(functions.config().firebase); | |
exports.sendNotification = functions.database.ref('/messages/{pushId}/text').onWrite((event) => { | |
const data = event.data; | |
console.log('Message received'); | |
if(!data.changed()){ | |
console.log('Nothing changed'); | |
return; | |
}else{ | |
console.log(data.val()); | |
} | |
const payLoad = { | |
notification:{ | |
title: 'Message received', | |
body: 'You received a new message', | |
sound: "default" | |
} | |
}; | |
const options = { | |
priority: "high", | |
timeToLive: 60*60*2 | |
}; | |
return admin.messaging().sendToTopic("Message_Notifications", payLoad, options); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment