This repository was archived by the owner on Sep 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathnotify.js
44 lines (40 loc) · 1.44 KB
/
notify.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
const { Subscription } = require('./db');
const configuredWebPush = require('./configured-web-push');
const init = async function() {
let pushMessage = "Extreme weather! Come check it out!";
try {
const cursor = Subscription.find().cursor();
await cursor.eachAsync(function(sub) {
return configuredWebPush.webPush.sendNotification({
endpoint: sub.endpoint,
keys: {
auth: sub.keys.auth,
p256dh: sub.keys.p256dh
}
}, pushMessage, {contentEncoding: 'aes128gcm'})
.then(function(push) {
console.log(push);
})
.catch(function(e) {
// 404 for FCM AES128GCM
if (e.statusCode === 410 || e.statusCode === 404) {
// delete invalid registration
return Subscription.remove({endpoint: sub.endpoint}).exec()
.then(function(sub) {
console.log('Deleted: ' + sub.endpoint);
})
.catch(function(sub) {
console.error('Failed to delete: ' + sub.endpoint);
});
}
});
});
} catch (e) {
console.log(e);
}
console.log('Job executed correctly');
};
init().catch(function(err) {
console.error(err);
process.exit(1);
});