This repository has been archived by the owner on Apr 11, 2022. It is now read-only.
/
push.js
102 lines (84 loc) · 2.2 KB
/
push.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/*!
* Module dependencies.
*/
var Command = require('./util/command'),
path = require('path'),
util = require('util'),
fs = require('fs'),
http = require('http'),
querystring = require('querystring'),
project = require('./util/project');
/**
* Server Default Settings
*/
var PushServerDefaults = {
port: 80,
host: 'push.api.phonegap.com'
};
/*!
* Command setup.
*/
module.exports = {
create: function(phonegap) {
return new PushCommand(phonegap);
},
default_settings: PushServerDefaults
};
function PushCommand(phonegap) {
return Command.apply(this, arguments);
}
util.inherits(PushCommand, Command);
/**
* Push.
*
*/
PushCommand.prototype.run = function(options, callback) {
// require options
if (!options) throw new Error('requires option parameter');
// optional parameters
options.port = options.port || PushServerDefaults.port;
options.host = options.host || PushServerDefaults.host;
options.phonegap = this.phonegap;
callback = callback || function() {};
// send push
this.execute(options, callback);
return this.phonegap;
};
/*!
* Execute command.
*/
PushCommand.prototype.execute = function(options, callback) {
var self = this;
if (options.file) {
var payload = fs.readFileSync(options.file, 'utf8');
options.payload = payload;
}
if (!options.payload) throw new Error('requires payload parameter');
var data = JSON.stringify({
'deviceID': options.deviceID,
'type': options.service,
'appID': 'com.adobe.phonegap.app',
'payload': JSON.parse(options.payload)
});
var postOptions = {
host: options.host,
port: options.port,
path: '/v1/push',
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': Buffer.byteLength(data)
}
};
var req = http.request(postOptions, function(res) {
//res.setEncoding('utf8');
res.on('data', function (chunk) {
console.log("body: " + chunk);
});
res.on('error', function (e) {
console.log("error: " + e);
});
});
req.write(data);
req.end();
};