-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.ts
77 lines (63 loc) · 1.93 KB
/
server.ts
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
import fs from "fs";
import process from "process";
import apn from "@parse/node-apn";
import express from "express";
import morgan from "morgan";
const PORT = process.env.PORT || 5000;
const AUTH_KEY_PATH =
"/certs/" +
fs.readdirSync("/certs/").filter((file) => {
return file.endsWith(".p8");
})[0];
const apnProvider = new apn.Provider({
production: true,
token: {
key: AUTH_KEY_PATH,
keyId: process.env.KEY_ID as string,
teamId: process.env.TEAM_ID as string,
},
});
async function sendNotification(deviceId: string, alert: apn.NotificationAlertOptions, payload: Object) {
if (!alert) throw "missing alert";
const notification = new apn.Notification();
notification.alert = alert;
notification.payload = payload;
notification.topic = process.env.TOPIC as string;
notification.sound = "default";
const result = await apnProvider.send(notification, deviceId);
if (result.failed.length) {
const { error, response } = result.failed[0];
throw error || response;
}
}
//---- REST API ----
const app = express();
const router = express.Router();
app.use(morgan("tiny"));
app.use(express.json());
app.use("/", router);
function auth(req: express.Request, res: express.Response, next: express.NextFunction) {
const { authorization } = req.headers;
const secret = authorization && authorization.split(" ")[1];
if (secret && secret === process.env.SECRET) {
next();
} else {
res.sendStatus(401);
}
}
router.post("/notify", auth, async (req, res) => {
const { deviceId, alert, payload } = req.body;
(async () => {
try {
await sendNotification(deviceId, alert, payload || {});
if(process.env.DEBUG) {
console.info("Notification sent to", deviceId);
}
} catch (e: any) {
console.error(e);
}
})();
res.send({ success: true });
});
router.all("*", (_req, res) => res.sendStatus(404));
app.listen(PORT, () => console.log(`Server started on port ${PORT}`));