forked from gokulkrishh/demo-progressive-web-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
53 lines (44 loc) · 1.44 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
'use strict';
var express = require('express');
var bodyParser = require('body-parser');
var gcm = require('node-gcm');
var app = express();
//Here we are configuring express to use body-parser as middle-ware.
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
//To server static assests in root dir
app.use(express.static(__dirname));
//To allow cross origin request
app.use(function(req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept');
next();
});
//To server index.html page
app.get('/', function (req, res) {
res.sendFile(__dirname + '/index.html');
});
//To receive push request from client
app.post('/send_notification', function (req, res) {
if (!req.body) {
res.status(400);
}
var message = new gcm.Message();
var temp = req.body.endpoint.split('/');
var regTokens = [temp[temp.length - 1]];
var sender = new gcm.Sender('AIzaSyCjrU5SqotSg2ybDLK_7rMMt9Rv0dMusvY'); //Replace with your GCM API key
// Now the sender can be used to send messages
sender.send(message, { registrationTokens: regTokens }, function (error, response) {
if (error) {
console.error(error);
res.status(400);
}
else {
console.log(response);
res.status(200);
}
});
});
app.listen(process.env.PORT || 3000, function() {
console.log('Local Server : http://localhost:3000');
});