-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
59 lines (50 loc) · 1.56 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
58
59
const express = require('express');
const app = express();
const webpush = require('web-push');
var bodyParser = require('body-parser');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
var cors = require('cors');
app.use(cors({
origin: "https://anselmesdr.github.io"
}));
const vapidKeys = {
"publicKey": "BG8BD1RO8ClN6z_LraL6aF2s263rOwbIgEgSEDt1s59EQsHIOqSA46paHYN5iBEtv5CrVy7esYnGeSrkrDy5cBA",
"privateKey": "yHmaA2WSsm9LQM6jtqgxBl7mSm-kktRm5fSAh0bSW4E"
};
webpush.setVapidDetails(
'mailto:[email protected]',
vapidKeys.publicKey,
vapidKeys.privateKey
);
const subscribe = express.Router();
subscribe.post('/', (req, res) => {
const subscription = req.body;
const payload = JSON.stringify({
"notification": {
"title": "Angular News",
"body": "Newsletter Available!",
"icon": "assets/main-page-logo-small-hat.png",
"vibrate": [100, 50, 100],
"data": {
"dateOfArrival": Date.now(),
"primaryKey": 1
},
"actions": [{
"action": "explore",
"title": "Go to the site"
}]
}
});
console.log(subscription);
webpush.sendNotification(subscription, payload).then(result => {
console.log(result);
res.send(result);
}).catch(error => {
res.send(error.stack);
});
res.send(payload);
})
app.use('/subscribe', subscribe);
const port = process.env.PORT || 80;
app.listen(port, () => console.log('server started'));