forked from thihara/web_push_notifications
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
103 lines (81 loc) · 3.17 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/**
* Created by thihara on 12/9/16.
*/
let express = require("express");
let webPush = require("web-push");
let atob = require('atob');
let bodyParser = require('body-parser');
let util = require('util');
let app = express();
let subscribers = [];
let VAPID_SUBJECT = process.env.VAPID_SUBJECT;
let VAPID_PUBLIC_KEY = process.env.VAPID_PUBLIC_KEY;
let VAPID_PRIVATE_KEY = process.env.VAPID_PRIVATE_KEY;
//Auth secret used to authentication notification requests.
let AUTH_SECRET = process.env.AUTH_SECRET;
if (!VAPID_SUBJECT) {
return console.error('VAPID_SUBJECT environment variable not found.')
} else if (!VAPID_PUBLIC_KEY) {
return console.error('VAPID_PUBLIC_KEY environment variable not found.')
} else if (!VAPID_PRIVATE_KEY) {
return console.error('VAPID_PRIVATE_KEY environment variable not found.')
} else if (!AUTH_SECRET) {
return console.error('AUTH_SECRET environment variable not found.')
}
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
webPush.setVapidDetails(
VAPID_SUBJECT,
VAPID_PUBLIC_KEY,
VAPID_PRIVATE_KEY
);
app.use(express.static('static'));
app.get('/status', function (req, res) {
res.send('Server Running!')
});
app.get('/notify/all', function (req, res) {
if(req.get('auth-secret') != AUTH_SECRET) {
console.log("Missing or incorrect auth-secret header. Rejecting request.");
return res.sendStatus(401);
}
let message = req.query.message || `Willy Wonka's chocolate is the best!`;
let clickTarget = req.query.clickTarget || `http://www.favoritemedium.com`;
let title = req.query.title || `Push notification received!`;
subscribers.forEach(pushSubscription => {
//Can be anything you want. No specific structure necessary.
let payload = JSON.stringify({message : message, clickTarget: clickTarget, title: title});
webPush.sendNotification(pushSubscription, payload, {}).then((response) =>{
console.log("Status : "+util.inspect(response.statusCode));
console.log("Headers : "+JSON.stringify(response.headers));
console.log("Body : "+JSON.stringify(response.body));
}).catch((error) =>{
console.log("Status : "+util.inspect(error.statusCode));
console.log("Headers : "+JSON.stringify(error.headers));
console.log("Body : "+JSON.stringify(error.body));
});
});
res.send('Notification sent!');
});
app.post('/subscribe', function (req, res) {
let endpoint = req.body['notificationEndPoint'];
let publicKey = req.body['publicKey'];
let auth = req.body['auth'];
let pushSubscription = {
endpoint: endpoint,
keys: {
p256dh: publicKey,
auth: auth
}
};
subscribers.push(pushSubscription);
res.send('Subscription accepted!');
});
app.post('/unsubscribe', function (req, res) {
let endpoint = req.body['notificationEndPoint'];
subscribers = subscribers.filter(subscriber => { endpoint == subscriber.endpoint });
res.send('Subscription removed!');
});
let PORT = process.env.PORT || 8080;
app.listen(PORT, function () {
console.log(`push_server listening on port ${PORT}!`)
});