Forked from ricardodantas/firebase-cloud-function-notification-to-topic.js
Created
January 18, 2021 00:28
-
-
Save kiratheone/2d93ff33a652ecd6617466cb648eeb82 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 hidden or 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