Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save ricardodantas/91d2817338832e67c14c06de069a499f to your computer and use it in GitHub Desktop.
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.
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