1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 | var TEST_TITLE = 'Hi.';
|
17 | var TEST_MESSAGE = 'This is a test.';
|
18 |
|
19 | var mpns = require('../lib/mpns')
|
20 | , fs = require('fs');
|
21 |
|
22 | var args = process.argv;
|
23 | args.shift();
|
24 | var cmd = 'node ' + args.shift();
|
25 |
|
26 | function help() {
|
27 | console.log('Node.js MPNS Module Test Toaster');
|
28 | console.log('Please provide the pushUri to a Microsoft Push Notification Service (MPNS) endpoint to a Windows Phone device.');
|
29 | console.log();
|
30 | console.log('Parameters:');
|
31 | console.log(' ' + cmd + ' pushUri [Message]');
|
32 | console.log();
|
33 |
|
34 |
|
35 | console.log('Authenticated push notification channels are supported with the appropriate environment variables:');
|
36 | console.log(' MPNS_CERT: Point to a certificate file.');
|
37 | console.log(' MPNS_CA: Point to a certificate authority or intermediate chain file.');
|
38 | console.log(' MPNS_KEY: Point to a private key file.');
|
39 | console.log();
|
40 | console.log('Or for authenticated push with a PKCS12 package format:');
|
41 | console.log(' MPNS_PFX: The PKCS12 file.');
|
42 | console.log(' MPNS_PASSPHRASE: The optional password for the PKCS12 file.')
|
43 | }
|
44 |
|
45 | if (args.length == 0) {
|
46 | return help();
|
47 | }
|
48 |
|
49 | var uri = args.shift();
|
50 |
|
51 | if (uri.indexOf('http') !== 0) {
|
52 | console.log('The first parameter must be a URI.');
|
53 | return help();
|
54 | }
|
55 |
|
56 | var options = {
|
57 | text1: args.shift() || TEST_TITLE,
|
58 | text2: args.length > 0 ? args.join(' ') : TEST_MESSAGE
|
59 | };
|
60 |
|
61 | var authenticationReady = false;
|
62 | var fileEncoding = undefined;
|
63 | if (process.env.MPNS_CERT && process.env.MPNS_KEY) {
|
64 | options.cert = fs.readFileSync(process.env.MPNS_CERT, fileEncoding);
|
65 | options.key = fs.readFileSync(process.env.MPNS_KEY, fileEncoding);
|
66 | var ca = process.env.MPNS_CA;
|
67 | if (ca !== undefined) {
|
68 | options.ca = fs.readFileSync(ca, fileEncoding);
|
69 | }
|
70 | authenticationReady = true;
|
71 | } else if (process.env.MPNS_PFX) {
|
72 | options.pfx = fs.readFileSync(process.env.MPNS_PFX, fileEncoding);
|
73 | var passphrase = process.env.MPNS_PASSPHRASE;
|
74 | if (passphrase !== undefined) {
|
75 | options.passphrase = passphrase;
|
76 | }
|
77 | authenticationReady = true;
|
78 | }
|
79 |
|
80 | if (uri.indexOf('https') == 0) {
|
81 | if (!authenticationReady) {
|
82 | throw new Error('Authenticated push channels are not currently supported by this test application unless environment variables are set properly.');
|
83 | } else {
|
84 | var keys = [];
|
85 | for (var k in mpns.Properties.ssl) {
|
86 | var key = mpns.Properties.ssl[k];
|
87 | if (options[key]) {
|
88 | keys.push(key);
|
89 | }
|
90 | }
|
91 | console.log('Authenticated push notification channel: ' + keys.join(', '));
|
92 | }
|
93 | }
|
94 |
|
95 | console.log('Sending a toast...');
|
96 |
|
97 | mpns.sendToast(uri, options, function (err, result) {
|
98 | console.log(err ? 'Error' : 'OK');
|
99 | console.dir(err || result);
|
100 | });
|