Skip to content

Commit 5a62851

Browse files
committed
OneSignalPushAdapter now correctly sends APNS and GCM notifications and handles errors
1 parent a1b24da commit 5a62851

File tree

1 file changed

+38
-17
lines changed

1 file changed

+38
-17
lines changed

src/Adapters/Push/OneSignalPushAdapter.js

Lines changed: 38 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ OneSignalPushAdapter.prototype.getValidPushTypes = function() {
3030
}
3131

3232
OneSignalPushAdapter.prototype.send = function(data, installations) {
33+
console.log("Sending notification to "+installations.length+" devices.")
3334
let deviceMap = classifyInstallation(installations, this.validPushTypes);
3435

3536
let sendPromises = [];
@@ -50,7 +51,9 @@ OneSignalPushAdapter.prototype.send = function(data, installations) {
5051

5152
OneSignalPushAdapter.prototype.sendToAPNS = function(data,tokens) {
5253

53-
let post = {};
54+
data= data['data']
55+
56+
var post = {};
5457
if(data['badge']) {
5558
if(data['badge'] == "Increment") {
5659
post['ios_badgeType'] = 'Increase';
@@ -81,20 +84,23 @@ OneSignalPushAdapter.prototype.sendToAPNS = function(data,tokens) {
8184
var tokenlength=tokens.length;
8285
var offset = 0
8386
// handle onesignal response. Start next batch if there's not an error.
84-
let handleResponse = function(err) {
85-
if (err) {
86-
return promise.reject(err, tokens.slice(i, tokens.length()));
87+
let handleResponse = function(wasSuccessful) {
88+
if (!wasSuccessful) {
89+
return promise.reject("OneSignal Error");
8790
}
8891

89-
if(offset => tokenlength) {
92+
if(offset >= tokenlength) {
9093
promise.resolve()
9194
} else {
9295
this.sendNext();
9396
}
9497
}.bind(this)
9598

9699
this.sendNext = function() {
97-
post['include_android_reg_ids'] = tokens.slice(offset,offset+chunk);
100+
post['include_ios_tokens'] = [];
101+
tokens.slice(offset,offset+chunk).forEach(function(i) {
102+
post['include_ios_tokens'].push(i['deviceToken'])
103+
})
98104
offset+=chunk;
99105
this.sendToOneSignal(post, handleResponse);
100106
}.bind(this)
@@ -105,7 +111,9 @@ OneSignalPushAdapter.prototype.sendToAPNS = function(data,tokens) {
105111
}
106112

107113
OneSignalPushAdapter.prototype.sendToGCM = function(data,tokens) {
108-
let post = {};
114+
data= data['data']
115+
116+
var post = {};
109117

110118
if(data['alert']) {
111119
post['contents'] = {en: data['alert']};
@@ -127,23 +135,27 @@ OneSignalPushAdapter.prototype.sendToGCM = function(data,tokens) {
127135
var tokenlength=tokens.length;
128136
var offset = 0
129137
// handle onesignal response. Start next batch if there's not an error.
130-
let handleResponse = function(err) {
131-
if (err) {
132-
return promise.reject(err, tokens.slice(i, tokens.length()));
138+
let handleResponse = function(wasSuccessful) {
139+
if (!wasSuccessful) {
140+
return promise.reject("OneSIgnal Error");
133141
}
134142

135-
if(offset => tokenlength) {
143+
if(offset >= tokenlength) {
136144
promise.resolve()
137145
} else {
138146
this.sendNext();
139147
}
140148
}.bind(this);
141149

142-
this.sendNext = function() {
143-
post['include_android_reg_ids'] = tokens.slice(offset,offset+chunk);;
150+
this.sendNext = function() {
151+
post['include_android_reg_ids'] = [];
152+
tokens.slice(offset,offset+chunk).forEach(function(i) {
153+
post['include_android_reg_ids'].push(i['deviceToken'])
154+
})
144155
offset+=chunk;
145156
this.sendToOneSignal(post, handleResponse);
146-
}.bind(this);
157+
}.bind(this)
158+
147159

148160
this.sendNext();
149161
return promise;
@@ -165,12 +177,21 @@ OneSignalPushAdapter.prototype.sendToOneSignal = function(data, cb) {
165177
data['app_id'] = this.OneSignalConfig['appId'];
166178

167179
let request = this.https.request(options, function(res) {
168-
cb(null);
180+
if(res.statusCode < 299) {
181+
cb(true);
182+
} else {
183+
console.log('OneSignal Error');
184+
res.on('data', function(chunk) {
185+
console.log(chunk.toString())
186+
});
187+
cb(false)
188+
}
169189
});
170190
request.on('error', function(e) {
171-
cb(e);
191+
console.log("Error connecting to OneSignal")
192+
console.log(e);
193+
cb(false);
172194
});
173-
console.log(data);
174195
request.write(JSON.stringify(data))
175196
request.end();
176197
}

0 commit comments

Comments
 (0)