use this package :
https://www.npmjs.com/package/fcm-notification
const fcm = require('fcm-notification');
var fcm_key = require('your/fcm/token/from/firebase');
var FcM = new fcm(fcm_key);
module.exports.sendToSingleUser = async (message, token) => {
let message_body = {
notification: {
...message
},
token: token
};
FcM.send(message_body, function (err, response) {
if (err) {
return err
} else {
return response
}
})
}
hint: get fcm tokens from clients and store them in the database.
this question will guide you on how to access tokens on android devices or ios devices.
remember you can store the tokens inside your user model . like this:
sequilioze example :
'use strict';
module.exports = (sequelize, DataTypes) => {
const FCM_customer = sequelize.define('FCM_customer', {
platform: {
type: DataTypes.ENUM,
values: ['android', 'apn', 'webpush']
},
deviceId: DataTypes.STRING,
token: DataTypes.STRING
}, {});
FCM_customer.associate = function (models) {
FCM_customer.belongsTo(models.Customer)
};
return FCM_customer;
};
Firebase (FCM) how to get token
extra:
- register client app to firebase(native developer tasks: Andriod, ios)
- client app most provide the fcm token to the backend.(native developer tasks:you can search about getting fcm tokens in ios and Andriod platforms)
- backend will store the token in the user model.
- firebase only knows the token. then use fcm library for sending to that specific token you desire
hint: there is a better solution which is: create a collection about
devices of the clients. every client could have more than one device
on other platforms.