Skip to content

Commit f179587

Browse files
arcturusFrancisco Jordano
authored andcommitted
Adding executable to use web-push from command line
1 parent fd95929 commit f179587

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

bin/web-push.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#! /usr/bin/env node
2+
const webPush = require('web-push');
3+
webPush.setGCMAPIKey(process.env.GCM_API_KEY);
4+
5+
const argv = require('minimist')(process.argv.slice(2));
6+
7+
const usage = 'Use: web-push --endpoint=<url> --key=<browser key> [--auth=<auth secret>] [--ttl=<seconds>] [--payload=<message>]';
8+
9+
if (!argv['endpoint'] || !argv['key']) {
10+
console.log(usage);
11+
process.exit(1);
12+
}
13+
14+
const endpoint = argv['endpoint'];
15+
const key = argv['key'];
16+
const ttl = argv['ttl'] || 0;
17+
const payload = argv['payload'] || '';
18+
const auth = argv['auth'] || null;
19+
20+
// Uses old sendMessage API
21+
var pushResult = null;
22+
if (!auth) {
23+
pushResult = webPush.sendNotification(endpoint, ttl, key, payload)
24+
} else {
25+
const vapidKeys = webPush.generateVAPIDKeys();
26+
const vapid = {
27+
audience: 'https://www.mozilla.org/',
28+
subject: 'mailto:[email protected]',
29+
privateKey: vapidKeys.privateKey,
30+
publicKey: vapidKeys.publicKey,
31+
};
32+
pushResult = webPush.sendNotification(endpoint, {
33+
TTL: ttl,
34+
payload,
35+
userPublicKey: key,
36+
userAuth: auth,
37+
vapid
38+
});
39+
}
40+
pushResult.then(() => {
41+
console.log('Push message sent.');
42+
}, (err) => {
43+
console.log('Error sending push message: ', err);
44+
}).then(() => {
45+
process.exit(0);
46+
})

package.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
"colors": "^1.1.2",
3030
"http_ece": "^0.4.5",
3131
"jws": "^3.1.3",
32+
"minimist": "^1.2.0",
3233
"urlsafe-base64": "^1.0.0"
3334
},
3435
"devDependencies": {
@@ -42,5 +43,11 @@
4243
"selenium-webdriver": "^2.53.1",
4344
"semver": "^5.1.0",
4445
"temp": "^0.8.3"
46+
},
47+
"bin": {
48+
"web-push": "bin/web-push.js"
49+
},
50+
"engines": {
51+
"node": ">= v4.0.0"
4552
}
4653
}

0 commit comments

Comments
 (0)