-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.ts
82 lines (66 loc) · 1.88 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
78
79
80
81
82
import process from "process";
import express from "express";
import morgan from "morgan";
const PORT = process.env.PORT || 5000;
import admin from "firebase-admin"
const serviceAccount = require("./firebase-service-account.json")
admin.initializeApp({
credential: admin.credential.cert(serviceAccount)
});
async function sendNotification(deviceId: string, alert: {title: string, body: string}, payload: { [key: string]: string; }) {
if (!alert) throw "missing alert";
const message = {
token: deviceId,
notification: {
title: alert.title,
body: alert.body
},
android: {
priority: "high" as 'high'
},
apns: {
headers: {
'apns-priority': '10'
}
},
data: payload,
};
admin.messaging().send(message)
.then((response) => {
console.log('Successfully sent message:', response);
})
.catch((error) => {
console.log('Error sending message:', error);
});
}
//---- 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}`));