0

I'm currently working on a project in which I want to send push-notifications to a set of devices. In my backend in node.js/express.js I have the code defining which device to send notifications to. Theses devices are identified by their id's stored in a mongoDB database.

I made a lot of researches and it seems like the only option available is to use Google's Firebase API. I made all the steps required to integrate the API in my app but there's one thing that I still don't understand.

When I have my list of mongoDB id's device, how am I supposed to link them to the token used by Firebase to identify a device?

My frontend app is developped in Vue.js and Ionic. It's a mobile app designed to be cross-platform but at first I'm only focusing on Android

Thanks a lot for your help

Doug Stevenson
  • 236,239
  • 27
  • 275
  • 302
Eva
  • 23
  • 5

1 Answers1

1

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:

  1. register client app to firebase(native developer tasks: Andriod, ios)
  2. 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)
  3. backend will store the token in the user model.
  4. 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.

Babak Abadkheir
  • 1,868
  • 1
  • 10
  • 27
  • Thank you for your answer. I don't understand clearly how to get the token from some specific devices. I would have to store the token of a device when it makes a request to the API? I'm not sure to understand the link between the things stored by Firebase and my data stored in MongoDB with the library Mongoose – Eva Dec 06 '20 at 14:06
  • @Eva the client-side most provide the token for you by sending it to the server. then when the client installs the app you can now post a request to the firebase for sending notification. remember that you have to register your client app in the firebase dashboard panel.I will correct my answer to your desire. – Babak Abadkheir Dec 06 '20 at 14:13