-
Notifications
You must be signed in to change notification settings - Fork 88
/
urbanairport.js
384 lines (273 loc) · 8.13 KB
/
urbanairport.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
// Classic Titanium compatibility
var _ = _ || require((typeof ENV_TEST === 'boolean') ? 'alloy' : 'underscore')._,
OS_IOS = Ti.Platform.name === 'iPhone OS',
OS_ANDROID = (!OS_IOS && Ti.Platform.name === 'android');
// Actual module we're going to extend
var urbanairship = require('ti.urbanairship');
var callback,
sound = true,
debug = (typeof ENV_PRODUCTION === 'boolean' ? !ENV_PRODUCTION : false);
// Android-only vars
if (OS_ANDROID) {
var pendingTags = null,
pendingAlias = null,
compatibility = false,
compatibilityStack = [],
vibrate = true;
}
// iOS-only vars
if (OS_IOS) {
var alert = true,
badge = true;
}
urbanairship.register = function(config) {
if (config) {
this.config(config);
}
this.enable();
return this;
};
urbanairship.config = function(config) {
if (debug || config.debug !== false) {
logger('INIT', config);
}
var that = this;
_.each(config, function(val, key) {
switch (key) {
// callback
case 'callback':
callback = val;
break;
// debug
case 'debug':
debug = !! val;
break;
// compatibility
case 'compatibility':
if (OS_ANDROID) {
compatibility = !! val;
if (typeof config.showOnAppClick === 'undefined') {
that.showOnAppClick = compatibility;
}
}
break;
// iOS-only so maybe better always use config file
case 'options':
if (OS_IOS) {
that.options = val;
}
break;
// Smart methods
case 'tags':
that.resetTags(val);
break;
case 'alias':
that.resetAlias(val);
break;
// Used for enabling push options
case 'sound':
sound = !! val;
break;
case 'vibrate':
if (OS_ANDROID) {
vibrate = !! val;
}
break;
case 'badge':
if (OS_IOS) {
badge = !! val;
}
break;
case 'alert':
if (OS_IOS) {
alert = !! val;
}
break;
// Allows to set pretty much any property or method
default:
if (typeof that[key] !== 'undefined') {
if (_.isFunction(that[key])) {
that[key](val);
logger('calling ' + key, val);
} else {
that[key] = val;
logger('setting ' + key, val);
}
}
break;
}
});
};
urbanairship.resetTags = function(tags) {
// We accept a single string as well
if (typeof tags === 'string') {
tags = [tags];
}
// We need to be flying on Android
if (OS_ANDROID && !this.isFlying) {
pendingTags = tags;
return;
}
this.tags = tags;
return this;
};
urbanairship.addTags = function(tags) {
// We accept a single string as well
if (typeof tags === 'string') {
tags = [tags];
}
// We need to be flying on Android
if (OS_ANDROID && !this.isFlying) {
pendingTags = _.union(pendingTags || [], tags);
return;
}
this.tags = _.union(this.tags || [], tags);
return this;
};
urbanairship.removeTags = function(tags) {
// We accept a single string as well
if (typeof tags === 'string') {
tags = [tags];
}
// We need to be flying on Android
if (OS_ANDROID && !this.isFlying) {
pendingTags = _.difference(pendingTags || [], tags);
return;
}
this.tags = _.difference(this.tags || [], tags);
return this;
};
urbanairship.resetAlias = function(alias) {
// We need to be flying on Android
if (OS_ANDROID && !this.isFlying) {
pendingAlias = alias;
return;
}
this.alias = alias;
return this;
};
urbanairship.enable = function() {
var that = this;
if (OS_IOS) {
var types = [];
if (badge) {
types.push(Ti.Network.NOTIFICATION_TYPE_BADGE);
}
if (alert) {
types.push(Ti.Network.NOTIFICATION_TYPE_ALERT);
}
if (sound) {
types.push(Ti.Network.NOTIFICATION_TYPE_SOUND);
}
Ti.Network.registerForPushNotifications({
types: types,
success: function(e) {
logger('SUCCESS', e);
that.registerDevice(e.deviceToken);
e.type = 'success';
// Android-like args
e.valid = e.success;
caller(e);
},
error: function(e) {
logger('ERROR', e);
e.type = 'error';
// Android-like args
e.valid = e.success;
caller(e);
},
callback: function(e) {
logger('CALLBACK', e);
that.handleNotification(e.data);
e.type = 'callback';
// Android-like args
e.clicked = e.inBackground;
e.message = e.data.aps.alert;
e.payload = e.data.aps;
caller(e);
}
});
}
if (OS_ANDROID) {
that.pushEnabled = true;
}
return this;
};
urbanairship.disable = function() {
if (OS_IOS) {
this.unregisterDevice();
}
if (OS_ANDROID) {
this.pushEnabled = false;
}
return this;
};
if (OS_ANDROID) {
urbanairship.addEventListener(urbanairship.EVENT_URBAN_AIRSHIP_SUCCESS, function(e) {
logger('SUCCESS', e);
if (pendingAlias !== null) {
urbanairship.alias = pendingAlias;
pendingAlias = null;
}
if (pendingTags !== null) {
urbanairship.tags = pendingTags;
pendingTags = null;
}
e.type = 'success';
// iOS-like args
e.success = e.valid;
caller(e);
});
urbanairship.addEventListener(urbanairship.EVENT_URBAN_AIRSHIP_ERROR, function(e) {
logger('ERROR', e);
e.type = 'error';
// iOS-like args
e.success = e.valid;
caller(e);
});
urbanairship.addEventListener(urbanairship.EVENT_URBAN_AIRSHIP_CALLBACK, function(e) {
logger('CALLBACK', e);
// Make sure we don't call the same notification twice
if (compatibility) {
var hash = Ti.Utils.sha1(JSON.stringify([e.payload, e.message]));
if (compatibilityStack.indexOf(hash) !== -1) {
return;
}
compatibilityStack.push(hash);
}
e.type = 'callback';
// Convert payload via JSON to object
if (e.payload) {
try {
var json = e.payload.replace(/\{/g, '{"').replace(/=/g, '":"').replace(/, /g, '","').replace(/\}/g, '"}');
e.payload = JSON.parse(json);
} catch (err) {
logger('CALLBACK PAYLOAD', err, 'error');
e.payload = {};
}
}
// iOS-like args
e.inBackground = null;
e.data = e.payload || {};
e.data.alert = e.message;
e.data.aps = e.data;
caller(e);
});
}
function logger(label, data, level) {
if (!level) {
level = 'debug';
}
// No DRY code due to https://github.com/dbankier/TiShadow/issues/147
if (level === 'debug') {
Ti.API.debug('[URBANAIRPORT] ' + label + (data ? ': ' + JSON.stringify(data, null, '\t') : ''));
} else {
Ti.API.error('[URBANAIRPORT] ' + label + (data ? ': ' + JSON.stringify(data, null, '\t') : ''));
}
}
function caller(e) {
if (_.isFunction(callback)) {
callback(e);
}
}
module.exports = urbanairship;