-
Notifications
You must be signed in to change notification settings - Fork 0
/
ionic-push.js
41 lines (37 loc) · 973 Bytes
/
ionic-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
'use strict';
var https = require('https');
var ionic = require('./config').ionic;
var options = {
hostname: 'api.ionic.io',
port: '443',
path: '/push/notifications',
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + ionic.jwt
}
};
exports.post = function(message, payload, devices) {
return new Promise((resolve, reject) => {
var req = https.request(options, (response) => {
if (response.statusCode < 200 || response.statusCode > 299) {
reject(new Error('HTTP : ' + response.statusCode ));
}
const body = [];
response.on('data', (chunk) => body.push(chunk));
response.on('end', () => resolve(body.join('')));
});
req.on('error', (err) => reject(err))
var body = {
"send_to_all": true,
"profile": ionic.profile,
"notification": {
"message": message,
"payload": payload
}
};
req.write(JSON.stringify(body));
req.end();
console.log('Request sent');
});
};