-
Notifications
You must be signed in to change notification settings - Fork 0
/
apn.js
52 lines (47 loc) · 1.49 KB
/
apn.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
if (typeof module !== 'undefined' && module.exports) {
} else {
var exports = Coltan.APN = {};
}
Coltan.APN.register = function(fn){
Coltan.info('Attempting to register for push notifications');
Titanium.Network.registerForPushNotifications({
types: [
Titanium.Network.NOTIFICATION_TYPE_BADGE,
Titanium.Network.NOTIFICATION_TYPE_ALERT,
Titanium.Network.NOTIFICATION_TYPE_SOUND
],
success:function(event) {
Coltan.info("Device registered for APN. Device token: \n\n"+event.deviceToken);
Ti.App.Properties.setString('pushToken',event.deviceToken);
if(fn) fn(null,event.deviceToken);
},
error:function(event) {
Coltan.warn("Error during APN registration: "+event.error);
if(fn) fn(event.error);
},
callback:function(event) {
Coltan.info("Received a push notification");
Coltan.info("event: "+JSON.stringify(event.data))
Ti.App.fireEvent('push_event',event.data);
}
});
};
Coltan.APN.asyncEnsureSaved = function(fn){
if(Coltan.APN.hasToken()){
fn(null,Coltan.APN.getToken())
} else {
Coltan.APN.register(fn);
}
}
Coltan.APN.registerIfNotSaved = function(){
if(!Coltan.APN.hasToken()) Coltan.APN.register();
};
Coltan.APN.hasToken = function(){
return Ti.App.Properties.hasProperty('pushToken') && !_.isEmpty(Ti.App.Properties.getString('pushToken'));
};
Coltan.APN.getToken = function(){
return Ti.App.Properties.getString('pushToken');
};
Coltan.APN.clearToken = function(){
Ti.App.Properties.removeProperty('pushToken');
};