-
Notifications
You must be signed in to change notification settings - Fork 4
/
server.js
57 lines (49 loc) · 1.5 KB
/
server.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
51
52
53
54
55
56
57
/**
* Created by vishwas3 on 3/8/17.
*/
var express = require('express');
var request = require('request');
var server = express();
server.get('/api/notify/:fcm', function (req, res) {
var to = req.params.fcm;
return notify(res, to);
});
server.get('/api/notify-with-delay/:fcm', function (req, res) {
setTimeout((function () {
var to = req.params.fcm;
return notify(res, to);
}), 3000);
})
server.use('/', express.static(__dirname + '/build'));
function notify(res, to) {
var key = process.env.FCM_SERVER_KEY;
var notification = {
'title': 'Wow! This works like a charm.',
'body': 'What sorcery is this?',
'icon': 'https://cdn.dextra.art/website/assets/1501776716680-image.jpg',
'click_action': 'http://localhost:8080'
};
request({
url: 'https://fcm.googleapis.com/fcm/send',
method: 'POST',
headers: {
'Authorization': 'key=' + key,
'Content-Type': 'application/json'
},
body: JSON.stringify({
'notification': notification,
'to': to
})
}, function (error, response, body) {
if (error) {
console.log("Error in post request!", error);
res.send('Send notification response: ', error);
res.status(500).send(error);
} else {
console.log("No error, body.", body);
res.status(200).send(body);
}
});
}
var port = process.env.PORT || 8080;
server.listen(port);