-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
80 lines (73 loc) · 1.92 KB
/
index.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
const express = require('express')
const app = express()
const port = 3000
const { buildMessage, sendPostNotification } = require('./expo');
const multer = require('multer');
const upload = multer();
app.use(express.json());
// fake db
let messages = [];
const user = {
expoPushToken: null,
shouldSendNotifications: true,
shouldSendSound: true,
}
/*
example:
post: http://localhost:3000/save
{
"expoPushToken": "<YOUR_PUSH_TOKEN_HERE>",
"shouldSendNotifications": true,
"shouldSendSound": true
}
*/
app.post('/save', upload.any(), (req, res) => {
console.log('saving user data');
const body = req.body;
const expoPushToken = body.expoPushToken;
if (expoPushToken !== undefined) {
user.expoPushToken = expoPushToken;
}
const shouldSendNotifications = body.shouldSendNotifications;
if (shouldSendNotifications !== undefined) {
user.shouldSendNotifications = shouldSendNotifications;
}
const shouldSendSound = body.shouldSendSound;
if (shouldSendSound !== undefined) {
user.shouldSendSound = shouldSendSound;
}
console.log('user updated to:', user)
res.sendStatus(200);
})
/*
example:
post http://localhost:3000/send
{
"title": "New invoice",
"body": "You have new invoice",
"data": { "invoiceId": "<SOME_INVOICE_ID>" }
}
or
{
"title": "New order",
"body": "You have new order, check it out!",
"data": { orderId: "<SOME_ORDER_ID>" }
}
*/
app.post('/send', (req, res) => {
console.log('sending push to user: ', user);
const pushBody = req.body;
console.log('push body: ', pushBody);
messages.push(buildMessage(user, pushBody));
if (user.shouldSendNotifications && user.expoPushToken) {
console.log('messages: ', messages);
sendPostNotification(messages);
} else {
console.log('user disabled notifications or doesn\'t have push token');
}
messages = [];
res.sendStatus(200);
})
app.listen(port, () => {
console.log(`Example app listening on port ${port}`)
})