Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use Firebase Functions to send FCM to user?

Firebase currently rolled out Firebase Functions to add server side code

Firebase Functions

I was wondering if there could be a way to call FCM notifications through those functions when there is some change in database.

like image 379
Sahaj Rana Avatar asked Mar 13 '17 17:03

Sahaj Rana


1 Answers

Firebase SDK for Cloud Functions includes the Firebase Admin SDK, you can find an example that we made here Send Firebase Cloud Messaging notifications for new followers

In brief:

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);

exports.foo = functions.database.ref('/bar').onWrite(event => {
  const tokens = ...;
  const payload = ...;
  return admin.messaging().sendToDevice(tokens, payload);  
})
like image 70
James Daniels Avatar answered Oct 22 '22 19:10

James Daniels