-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
43 lines (38 loc) · 1.24 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
require('dotenv').config();
const server = require('server');
const { get, post } = server.router;
const { json, render } = server.reply;
const webpush = require('web-push');
webpush.setVapidDetails(
'https://arnellebalane.com',
process.env.VAPID_PUBLIC_KEY,
process.env.VAPID_PRIVATE_KEY
);
server([
get('/', ctx => render('index.hbs', {
vapidPublicKey: process.env.VAPID_PUBLIC_KEY
})),
post('/subscribe', async ctx => {
const subscription = ctx.body;
const payload = JSON.stringify({
title: 'Hello there!',
body: 'This is a test notification from the server.',
icon: '/assets/logo.png',
badge: '/assets/badge.png',
tag: 'main-notification',
actions: [ {
action: 'select-left',
title: 'Choose Left',
icon: '/assets/left.png'
}, {
action: 'select-right',
title: 'Choose Right',
icon: '/assets/right.png'
} ]
});
const response = await webpush.sendNotification(subscription, payload);
return json(response);
})
]).then(ctx => {
ctx.log.info(`Server is listening on port ${ctx.options.port}`);
});