-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.ts
76 lines (60 loc) · 2.22 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
import * as express from 'express';
import {Application} from "express";
const webpush = require('web-push');
const vapidKeys = {
"publicKey":"BEf3AAasjVowk2heZKL_QLSM9AkUrEiiCaxdZNrA96Ffe3lPs66r7mguXTUAvzdmvBT44dcA-JjdTyzBXdUUKfM",
"privateKey":"m3_xmk5uy_JR2oNf_bX7Q7ejzJdxBqi2pWlA5-S6WYU"
};
webpush.setVapidDetails(
'mailto:[email protected]',
vapidKeys.publicKey,
vapidKeys.privateKey
);
const app: Application = express();
const firebase = require("firebase");
firebase.initializeApp({
apiKey: "AIzaSyDDqoCe1KVK-UBZoJrBHm8xQoQD4HtYug8",
authDomain: "vokabeldojo.firebaseapp.com",
databaseURL: "https://vokabeldojo.firebaseio.com",
projectId: "vokabeldojo",
storageBucket: "vokabeldojo.appspot.com",
messagingSenderId: "818974977616"
});
var db = firebase.firestore();
var subscriptions = db.collection('subscriptions');
app.route('/api/newsletter').get(sendNewsletter);
export function sendNewsletter(req, res) {
subscriptions.get().then(collection => {
const allSubscriptions = [];//... get subscriptions from database
collection.forEach(sub => { allSubscriptions.push(sub.data())});
console.log('Total subscriptions', allSubscriptions.length);
// console.log(allSubscriptions[0].data());
const notificationPayload = {
"notification": {
"title": "Angular News",
"body": "Newsletter Available!",
"icon": "assets/main-page-logo-small-hat.png",
"vibrate": [100, 50, 100],
"data": {
"dateOfArrival": Date.now(),
"primaryKey": 1
},
"actions": [{
"action": "explore",
"title": "Go to the site"
}]
}
};
Promise.all(allSubscriptions.map(sub => webpush.sendNotification(
sub, JSON.stringify(notificationPayload) )))
.then(() => res.status(200).json({message: 'Newsletter sent successfully.'}))
.catch(err => {
console.error("Error sending notification, reason: ", err);
res.sendStatus(500);
});
});
}
// launch an HTTP Server
const httpServer = app.listen(9000, () => {
console.log("HTTP Server running at http://localhost:" + httpServer.address().port);
});