Instantly share code, notes, and snippets.

What would you like to do?
Firebase Cloud Messaging + Cloud Functions
const functions = require('firebase-functions');
const admin = require('firebase-admin');
exports.sendLeaderboardMesssage = functions
.onUpdate((change) => {
const oldPlayer = change.before.val();
const newPlayer = change.after.val();
// Exit if change is caused by old player moving up in leaderboard
if (newPlayer.score <= oldPlayer.score) { return; }
const payload = {
notification: {
title: `${oldPlayer.handle}, ${newPlayer.handle} just beat your score!`,
body: 'Play now to get back on the leaderboard',
const databaseRoot = change.before.ref.root;
return databaseRoot.child('users/' + oldPlayer.uId).once('value')
.then(snapshot => {
const fcmToken = snapshot.val().fcmToken;
return admin.messaging().sendToDevice(fcmToken, payload);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment