OneSignalPushAdapter now correctly sends APNS and GCM notifications and handles errors

This commit is contained in:
George Deglin
2016-02-13 18:26:17 -08:00
parent a1b24da3e7
commit 5a628516a6

View File

@@ -30,6 +30,7 @@ OneSignalPushAdapter.prototype.getValidPushTypes = function() {
} }
OneSignalPushAdapter.prototype.send = function(data, installations) { OneSignalPushAdapter.prototype.send = function(data, installations) {
console.log("Sending notification to "+installations.length+" devices.")
let deviceMap = classifyInstallation(installations, this.validPushTypes); let deviceMap = classifyInstallation(installations, this.validPushTypes);
let sendPromises = []; let sendPromises = [];
@@ -50,7 +51,9 @@ OneSignalPushAdapter.prototype.send = function(data, installations) {
OneSignalPushAdapter.prototype.sendToAPNS = function(data,tokens) { OneSignalPushAdapter.prototype.sendToAPNS = function(data,tokens) {
let post = {}; data= data['data']
var post = {};
if(data['badge']) { if(data['badge']) {
if(data['badge'] == "Increment") { if(data['badge'] == "Increment") {
post['ios_badgeType'] = 'Increase'; post['ios_badgeType'] = 'Increase';
@@ -81,12 +84,12 @@ OneSignalPushAdapter.prototype.sendToAPNS = function(data,tokens) {
var tokenlength=tokens.length; var tokenlength=tokens.length;
var offset = 0 var offset = 0
// handle onesignal response. Start next batch if there's not an error. // handle onesignal response. Start next batch if there's not an error.
let handleResponse = function(err) { let handleResponse = function(wasSuccessful) {
if (err) { if (!wasSuccessful) {
return promise.reject(err, tokens.slice(i, tokens.length())); return promise.reject("OneSignal Error");
} }
if(offset => tokenlength) { if(offset >= tokenlength) {
promise.resolve() promise.resolve()
} else { } else {
this.sendNext(); this.sendNext();
@@ -94,7 +97,10 @@ OneSignalPushAdapter.prototype.sendToAPNS = function(data,tokens) {
}.bind(this) }.bind(this)
this.sendNext = function() { this.sendNext = function() {
post['include_android_reg_ids'] = tokens.slice(offset,offset+chunk); post['include_ios_tokens'] = [];
tokens.slice(offset,offset+chunk).forEach(function(i) {
post['include_ios_tokens'].push(i['deviceToken'])
})
offset+=chunk; offset+=chunk;
this.sendToOneSignal(post, handleResponse); this.sendToOneSignal(post, handleResponse);
}.bind(this) }.bind(this)
@@ -105,7 +111,9 @@ OneSignalPushAdapter.prototype.sendToAPNS = function(data,tokens) {
} }
OneSignalPushAdapter.prototype.sendToGCM = function(data,tokens) { OneSignalPushAdapter.prototype.sendToGCM = function(data,tokens) {
let post = {}; data= data['data']
var post = {};
if(data['alert']) { if(data['alert']) {
post['contents'] = {en: data['alert']}; post['contents'] = {en: data['alert']};
@@ -127,12 +135,12 @@ OneSignalPushAdapter.prototype.sendToGCM = function(data,tokens) {
var tokenlength=tokens.length; var tokenlength=tokens.length;
var offset = 0 var offset = 0
// handle onesignal response. Start next batch if there's not an error. // handle onesignal response. Start next batch if there's not an error.
let handleResponse = function(err) { let handleResponse = function(wasSuccessful) {
if (err) { if (!wasSuccessful) {
return promise.reject(err, tokens.slice(i, tokens.length())); return promise.reject("OneSIgnal Error");
} }
if(offset => tokenlength) { if(offset >= tokenlength) {
promise.resolve() promise.resolve()
} else { } else {
this.sendNext(); this.sendNext();
@@ -140,10 +148,14 @@ OneSignalPushAdapter.prototype.sendToGCM = function(data,tokens) {
}.bind(this); }.bind(this);
this.sendNext = function() { this.sendNext = function() {
post['include_android_reg_ids'] = tokens.slice(offset,offset+chunk);; post['include_android_reg_ids'] = [];
tokens.slice(offset,offset+chunk).forEach(function(i) {
post['include_android_reg_ids'].push(i['deviceToken'])
})
offset+=chunk; offset+=chunk;
this.sendToOneSignal(post, handleResponse); this.sendToOneSignal(post, handleResponse);
}.bind(this); }.bind(this)
this.sendNext(); this.sendNext();
return promise; return promise;
@@ -165,12 +177,21 @@ OneSignalPushAdapter.prototype.sendToOneSignal = function(data, cb) {
data['app_id'] = this.OneSignalConfig['appId']; data['app_id'] = this.OneSignalConfig['appId'];
let request = this.https.request(options, function(res) { let request = this.https.request(options, function(res) {
cb(null); if(res.statusCode < 299) {
cb(true);
} else {
console.log('OneSignal Error');
res.on('data', function(chunk) {
console.log(chunk.toString())
});
cb(false)
}
}); });
request.on('error', function(e) { request.on('error', function(e) {
cb(e); console.log("Error connecting to OneSignal")
console.log(e);
cb(false);
}); });
console.log(data);
request.write(JSON.stringify(data)) request.write(JSON.stringify(data))
request.end(); request.end();
} }