Skip to content

Instantly share code, notes, and snippets.

@Kingamattack
Last active December 5, 2019 10:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Kingamattack/757abec7ad2e3b80fe57bb26cb022424 to your computer and use it in GitHub Desktop.
Save Kingamattack/757abec7ad2e3b80fe57bb26cb022424 to your computer and use it in GitHub Desktop.
Firebase Cloud Function to send a push notification everytime you have a new entry in your Cloud Storage
// The Cloud Functions for Firebase SDK to create Cloud Functions and setup triggers.
const functions = require('firebase-functions');
// The Cloud Functions for Firebase SDK to access database
const admin = require('firebase-admin');
// Init
admin.initializeApp(functions.config().firebase);
/**
* Create a 'sendPushNotification' function triggered
* everytime a new item is create in storage.
*/
exports.sendPushNotification = functions.storage.object()
.onFinalize(event => {
const payload = {
notification: {
body : 'Hello world !',
badge : '1',
sound : 'default'
}
}
return admin.database().ref('REF_TO_FCM_TOKENS').once('value').then(tokens => {
if(!tokens.val()) {
return;
}
const token = Object.keys(tokens.val());
return admin.messaging().sendToDevice(token, payload)
.then(function(response) {
return 'Notification sent: ', response;
})
.then(function(error) {
throw('Notification sent: ', error);
})
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment