Skip to content

Instantly share code, notes, and snippets.

@may-andro
Created October 25, 2018 08:43
Show Gist options
  • Save may-andro/618ba0ad109c62c46d5ad67d34b986a0 to your computer and use it in GitHub Desktop.
Save may-andro/618ba0ad109c62c46d5ad67d34b986a0 to your computer and use it in GitHub Desktop.
Firebase function for sending notification in flutter app.
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
exports.sendNotification = functions.https.onRequest((req, res) => {
const to = req.query.to;
const fromId = req.query.fromId;
const fromPushId = req.query.fromPushId;
const fromName = req.query.fromName;
const fromMessage = req.query.fromMessage;
const isApproved = req.query.isApproved;
const type = req.query.type;
var payload = {
data: {
click_action: "FLUTTER_NOTIFICATION_CLICK",
fromId: fromId,
fromPushId: fromPushId,
fromName: fromName,
type: type,
isApproved: isApproved,
fromMessage: fromMessage
}
};
if (type === 'leave_request') {
payload.notification = {
title: 'Leave Request',
body: `${fromName} has request for a leave!`
};
} else if (type === 'leave_approved') {
payload.notification = {
title: 'Leave Approved',
body: `${fromName} has approved your leave!`
};
}
var options = {
priority: "high",
timeToLive: 60 * 60 * 24
};
admin.messaging().sendToDevice(to, payload, options)
.then(function (response) {
return res.send(200, 'ok');
})
.catch(function (error) {
res.send(200, 'failed');
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment