-
Notifications
You must be signed in to change notification settings - Fork 8
/
server.js
52 lines (41 loc) · 1.14 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
'use strict';
const path = require('path');
const { sleep } = require('sleep');
const config = require('./config');
const Koa = require('koa');
const Router = require('koa-router');
const serve = require('koa-static');
const convert = require('koa-convert');
const FCM = require('fcm-node');
const koa = new Koa();
const app = new Router();
const fcm = new FCM(config.fcm.serverKey);
koa.use(convert(serve(path.join(__dirname, 'public'))));
app.get('/push', async (ctx) => {
const token = ctx.query.token;
const secs = +ctx.query.sleep || 0;
if (token) {
sleep(secs);
const message = {
to: token,
notification: {
title: 'Hey!',
body: 'You are awesome!',
icon: '/icon.png'
}
};
await fcm.send(message, (err, response) => {
if (err) {
console.log('Unable to send push notification: ', err);
} else {
console.log('Successfully sent push notification: ', response);
}
});
ctx.status = 200;
} else {
ctx.status = 500;
}
});
koa.use(app.routes());
console.log(`Starting server on port ${config.port}...`);
koa.listen(config.port);