SHARE
TWEET

Untitled

a guest Dec 7th, 2019 84 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const admin = require("firebase-admin");
  2. const axios = require("axios");
  3. const { User, PushToken, Group } = PG.models;
  4.  
  5. const fs = require('fs');
  6. const path = require('path');
  7.  
  8. const serviceAccountFile = fs.readFileSync(path.resolve(__dirname, '../flutter-app-firebase-adminsdk.json'), { encoding: 'utf8' });
  9. const serviceAccount = JSON.parse(serviceAccountFile || "{}");
  10.  
  11. const firebaseServerKey = process.env.FIREBASE_SERVER_KEY;
  12.  
  13. admin.initializeApp({
  14.   credential: admin.credential.cert(serviceAccount),
  15.   databaseURL: process.env.FIREBASE_DB_URL
  16.   // databaseURL: "https://push-notifications-test-a1443.firebaseio.com"
  17. });
  18.  
  19. module.exports = {
  20.   async checkToken(token){
  21.     try {
  22.       const result = await axios.get(`https://iid.googleapis.com/iid/info/${token}`, {
  23.         headers:{
  24.           Authorization: `key=${firebaseServerKey}`
  25.         }
  26.       });
  27.       return !result.data.error;
  28.     } catch (e) {
  29.       return false;
  30.     }
  31.   },
  32.  
  33.   sendToToken(token, { title, body, action, navigate }){
  34.     let skillary_obj = {};
  35.     if(action)
  36.       skillary_obj.skillary_action = action;
  37.     if(navigate)
  38.       skillary_obj.skillary_navigate = navigate;
  39.  
  40.  
  41.     return admin.messaging().send({
  42.       notification:{
  43.         title,
  44.         body
  45.       },
  46.       data:{
  47.           click_action: "FLUTTER_NOTIFICATION_CLICK",
  48.           ...skillary_obj
  49.       },
  50.       token
  51.     });
  52.   },
  53.  
  54.   async sendToUser(email, { title, body, action, navigate }) {
  55.     const user = await User.findOne({
  56.       where: {
  57.         email,
  58.       },
  59.       include:[
  60.         {
  61.           model: PushToken,
  62.           as: 'tokens',
  63.         }
  64.       ]
  65.     });
  66.  
  67.     if(!user)
  68.       throw new Error("No user found");
  69.  
  70.     let requests = [];
  71.     // Iterate every token of the user
  72.     user.tokens.map(_=>_.token).forEach( token => {
  73.       // Send notification to token
  74.       requests.push(
  75.         this.sendToToken(token, { title, body, action, navigate})
  76.       );
  77.     });
  78.  
  79.     return Promise.all(requests);
  80.   },
  81.  
  82.   async sendToGroup({ id, name }, { title, body }) {
  83.     // send to user where id = id if id && email
  84.     let paramName = id ? "id": name ? "name": null;
  85.     let param = id? id: name? name: null;
  86.  
  87.     // If neither email or id are setted
  88.     if(!paramName || !param)
  89.       throw new Error("Invalid parameters");
  90.  
  91.     const group = await Group.findOne({
  92.       where: {
  93.         [paramName]: param
  94.       },
  95.       include: [{
  96.         model: User,
  97.         as: 'users',
  98.         include: [{
  99.           model: PushToken,
  100.           as: 'tokens',
  101.         }]
  102.       }]
  103.     });
  104.  
  105.  
  106.     if(!group)
  107.       return null;
  108.  
  109.     const { users } = group;
  110.  
  111.     let requests = [];
  112.     //Iterate users
  113.     users.forEach( user => {
  114.       // Iterate tokens
  115.       user.tokens.map(_=>_.token).forEach( token => {
  116.         requests.push(this.sendToToken(token, { title, body }));
  117.       })
  118.     });
  119.  
  120.     return Promise.all(requests);
  121.  
  122.   }
  123. };
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top