Last active
June 12, 2019 14:38
-
-
Save ajurcevic/1a4696324b86ccb4fff27a79ab4b25c3 to your computer and use it in GitHub Desktop.
Cloud Function with Realtime Database Trigger
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
/* | |
# author: Adrian Jurcevic | |
# github: @ajurcevic | |
# sample to send remote notifications from realtime database updates. | |
# utilising latest firebase-functions v2.3.1 | |
# 10/06/2019 | |
*/ | |
const functions = require('firebase-functions'); | |
const admin = require('firebase-admin'); | |
admin.initializeApp(); | |
//change the region: https://firebase.google.com/docs/functions/locations | |
const region = "asia-northeast1"; | |
//change the topic name that your mobile/web devices will be subscribed to for messages | |
const topic = "custom-topic-name"; | |
/* | |
onWrite(change, context) = triggers when data is created, updated, or deleted in the Realtime Database. | |
onCreate(snap, context) = triggers when new data is created in the Realtime Database. | |
onUpdate(change, context) = triggers when data is updated in the Realtime Database. | |
onDelete(snap, context) = triggers when data is deleted from the Realtime Database. | |
The example we use below is watching for any newly created records to the database in the /notifications/ location. | |
*/ | |
exports.sendNotifications = functions | |
.region(region) | |
.database | |
.ref('notifications/{notiUid}') | |
.onCreate((snap, context) => { | |
const msg = snap.val(); | |
//uncomment below lines to see values in the Function logs | |
//console.log('id', context.params.notiUid); | |
//console.log('message', msg); | |
let payload = { | |
notification: { | |
title: msg.title, | |
body: msg.message, | |
sound: "default", | |
badge: "1" | |
} | |
}; | |
return admin.messaging().sendToTopic(topic, payload); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment