Skip to content

Instantly share code, notes, and snippets.

@ismummy
Last active November 25, 2020 11:55
Show Gist options
  • Save ismummy/dcd0203aebd352934ccfe61e8f873388 to your computer and use it in GitHub Desktop.
Save ismummy/dcd0203aebd352934ccfe61e8f873388 to your computer and use it in GitHub Desktop.
Firbase push notification helper class
require('dotenv').config();
const FCM = require('fcm-node');
const serverKey = process.env.GOOGLE_API_KEY;
const fcm = new FCM(serverKey);
class FCMHandler {
static sentToTopic(topic, flag, message) {
const msg = {
to: `/topics/${topic}`,
notification: {
title: topic,
body: topic
},
data: {
title: topic,
flag: flag,
data: message
}
};
this.send(msg);
}
static send(message) {
fcm.send(message, function (err, response) {
if (err) {
console.log("Something has gone wrong!");
} else {
console.log("Successfully sent with response: ", response);
}
});
}
}
module.exports = FCMHandler;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment