-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
50 lines (49 loc) · 1.29 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
const express = require("express");
const FCM = require("fcm-node");
const fcm = new FCM("server key");
const port = process.env.port || 3000;
const app = express();
app.listen(port,()=>{console.log(`App is running at port ${port}`)});
const to = "key for user 1";
const toTwo = "key for user 2";
const tokens = [to, toTwo]; // array of users
const promises = [];
const browser = ["firefox","chrome"];
var count = tokens.length;
tokens.forEach(token=>{
var promise = sendNotification(token,browser[count-1]);
promises.push(promise);
count--;
});
/**
* checking if all the messages are delivered
*/
Promise.all(promises).then(results=>{
console.log(results);
}).catch(err=>console.error(err));
/**
*
* @param {*} user the token for users
* @param {*} browser any exta messase
*/
function sendNotification(user, browser){
const message = {
to : user,
notification : {
title: "This is Gopal's Notification- " + browser,
body: "The message is that i can send a message"
},
data: {
payload1 : "firstPayload",
payload2: "payload two"
}
};
fcm.send(message, (err, res)=>{
if(err){
console.log(err);
return err;
}else{
return res;
}
});
}