forked from NativeScript/push-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpush-plugin.ios.js
170 lines (138 loc) · 6.59 KB
/
push-plugin.ios.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
module.exports = (function() {
var iosApp = require('application').ios;
var pushHandler;
var pushManager
(function() {
if (!pushHandler) {
pushHandler = Push.alloc().init();
pushManager = PushManager.alloc().init();
}
})();
var pushPluginObject = {
_init: function(settings) {
if (!!this.isInitialized) return;
var self = this;
// initialize the native push plugin
this.settings = settings;
this.notificationCallbackIOS = settings.notificationCallbackIOS;
// subscribe to the notification received event.
this._addObserver("notificationReceived", function(context) {
var userInfo = JSON.parse(context.userInfo.objectForKey('message'));
self.notificationCallbackIOS(userInfo);
});
this.isInitialized = true;
},
register: function(settings, success, error) {
this._init(settings);
var self = this;
if (!this.didRegisterObserver) { // make sure that the events are not attached more than once
this.didRegisterObserver = this._addObserver("didRegisterForRemoteNotificationsWithDeviceToken", function(result) {
self._removeObserver(self.didRegisterObserver, "didRegisterForRemoteNotificationsWithDeviceToken");
self.didRegisterObserver = undefined;
var token = result.userInfo.objectForKey('message');
success(token);
});
}
if (!this.didFailToRegisterObserver) {
this.didFailToRegisterObserver = this._addObserver("didFailToRegisterForRemoteNotificationsWithError", function(e) {
self._removeObserver(self.didFailToRegisterObserver, "didFailToRegisterForRemoteNotificationsWithError");
self.didFailToRegisterObserver = undefined;
error(e);
});
}
pushHandler.register(self.settings);
},
registerUserNotificationSettings: function(success,error) {
var self = this;
if (self.settings && self.settings.interactiveSettings) {
var interactiveSettings = self.settings.interactiveSettings;
var notificationTypes = [];
if (self.settings.alert) {
notificationTypes.push("alert");
}
if (self.settings.badge) {
notificationTypes.push("badge");
}
if (self.settings.sound) {
notificationTypes.push("sound");
}
if (!this.registerUserSettingsObserver) {
this.registerUserSettingsObserver = this._addObserver("didRegisterUserNotificationSettings", function() {
self._removeObserver(self.registerUserSettingsObserver, "didRegisterUserNotificationSettings");
self.registerUserSettingsObserver = undefined;
success();
});
}
if (!this.failToRegisterUserSettingsObserver) {
this.failToRegisterUserSettingsObserver = this._addObserver("failToRegisterUserNotificationSettings", function(error) {
self._removeObserver(self.didFailToRegisterObserver, "failToRegisterUserNotificationSettings");
self.failToRegisterUserSettingsObserver = undefined;
error(error);
});
}
pushHandler.registerUserNotificationSettings({
types: notificationTypes,
categories: self._mapCategories(interactiveSettings)
});
} else {
success();
}
},
unregister: function(done) {
var self = this;
if (!this.didUnregisterObserver) {
this.didUnregisterObserver = this._addObserver("didUnregister", function(context) {
self._removeObserver(self.didUnregisterObserver, "didUnregister");
self.didUnregisterObserver = undefined;
done(context);
});
}
pushHandler.unregister();
},
areNotificationsEnabled: function(done) {
var self = this;
if (!this.areNotificationsEnabledObserver) {
this.areNotificationsEnabledObserver = this._addObserver("areNotificationsEnabled", function(result) {
var areEnabledStr = result.userInfo.objectForKey('message');
var areEnabled;
if(areEnabledStr === "true"){
areEnabled = true;
}
self._removeObserver(self.areNotificationsEnabledObserver, "areNotificationsEnabled");
self.areNotificationsEnabledObserver = undefined;
done(areEnabled);
});
}
pushHandler.areNotificationsEnabled();
},
_mapCategories: function(interactiveSettings) {
var categories = [];
for (var i = 0; i < interactiveSettings.categories.length; i++) {
var currentCategory = interactiveSettings.categories[i];
var mappedCategory = {
identifier: currentCategory.identifier,
actionsForDefaultContext: [],
actionsForMinimalContext: []
}
for (var j = 0; j < interactiveSettings.actions.length; j++) {
var currentAction = interactiveSettings.actions[j];
if (currentCategory.actionsForMinimalContext.indexOf(currentAction.identifier) > -1) {
mappedCategory.actionsForMinimalContext.push(currentAction);
}
if (currentCategory.actionsForDefaultContext.indexOf(currentAction.identifier) > -1) {
mappedCategory.actionsForDefaultContext.push(currentAction);
}
}
categories.push(mappedCategory);
}
return categories;
},
_addObserver: function(eventName, callback) {
return iosApp.addNotificationObserver(eventName, callback);
},
_removeObserver: function(observer, eventName) {
iosApp.removeNotificationObserver(observer, eventName);
}
};
return pushPluginObject;
})();