1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 | 'use strict';
|
8 |
|
9 | var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
|
10 |
|
11 | function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 | var exec = cordova.require('cordova/exec');
|
21 |
|
22 | var PushNotification = function () {
|
23 | |
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 | function PushNotification(options) {
|
30 | var _this = this;
|
31 |
|
32 | _classCallCheck(this, PushNotification);
|
33 |
|
34 | this.handlers = {
|
35 | registration: [],
|
36 | notification: [],
|
37 | error: []
|
38 | };
|
39 |
|
40 |
|
41 | if (typeof options === 'undefined') {
|
42 | throw new Error('The options argument is required.');
|
43 | }
|
44 |
|
45 |
|
46 | this.options = options;
|
47 |
|
48 |
|
49 | var success = function success(result) {
|
50 | if (result && typeof result.registrationId !== 'undefined') {
|
51 | _this.emit('registration', result);
|
52 | } else if (result && result.additionalData && typeof result.additionalData.actionCallback !== 'undefined') {
|
53 | _this.emit(result.additionalData.actionCallback, result);
|
54 | } else if (result) {
|
55 | _this.emit('notification', result);
|
56 | }
|
57 | };
|
58 |
|
59 |
|
60 | var fail = function fail(msg) {
|
61 | var e = typeof msg === 'string' ? new Error(msg) : msg;
|
62 | _this.emit('error', e);
|
63 | };
|
64 |
|
65 |
|
66 | setTimeout(function () {
|
67 | exec(success, fail, 'PushNotification', 'init', [options]);
|
68 | }, 10);
|
69 | }
|
70 |
|
71 | |
72 |
|
73 |
|
74 |
|
75 |
|
76 | _createClass(PushNotification, [{
|
77 | key: 'unregister',
|
78 | value: function unregister(successCallback) {
|
79 | var _this2 = this;
|
80 |
|
81 | var errorCallback = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : function () {};
|
82 | var options = arguments[2];
|
83 |
|
84 | if (typeof errorCallback !== 'function') {
|
85 | console.log('PushNotification.unregister failure: failure parameter not a function');
|
86 | return;
|
87 | }
|
88 |
|
89 | if (typeof successCallback !== 'function') {
|
90 | console.log('PushNotification.unregister failure: success callback parameter must be a function');
|
91 | return;
|
92 | }
|
93 |
|
94 | var cleanHandlersAndPassThrough = function cleanHandlersAndPassThrough() {
|
95 | if (!options) {
|
96 | _this2.handlers = {
|
97 | registration: [],
|
98 | notification: [],
|
99 | error: []
|
100 | };
|
101 | }
|
102 | successCallback();
|
103 | };
|
104 |
|
105 | exec(cleanHandlersAndPassThrough, errorCallback, 'PushNotification', 'unregister', [options]);
|
106 | }
|
107 |
|
108 | |
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 | }, {
|
117 | key: 'subscribe',
|
118 | value: function subscribe(topic, successCallback) {
|
119 | var errorCallback = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : function () {};
|
120 |
|
121 | if (typeof errorCallback !== 'function') {
|
122 | console.log('PushNotification.subscribe failure: failure parameter not a function');
|
123 | return;
|
124 | }
|
125 |
|
126 | if (typeof successCallback !== 'function') {
|
127 | console.log('PushNotification.subscribe failure: success callback parameter must be a function');
|
128 | return;
|
129 | }
|
130 |
|
131 | exec(successCallback, errorCallback, 'PushNotification', 'subscribe', [topic]);
|
132 | }
|
133 |
|
134 | |
135 |
|
136 |
|
137 |
|
138 |
|
139 |
|
140 |
|
141 |
|
142 | }, {
|
143 | key: 'unsubscribe',
|
144 | value: function unsubscribe(topic, successCallback) {
|
145 | var errorCallback = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : function () {};
|
146 |
|
147 | if (typeof errorCallback !== 'function') {
|
148 | console.log('PushNotification.unsubscribe failure: failure parameter not a function');
|
149 | return;
|
150 | }
|
151 |
|
152 | if (typeof successCallback !== 'function') {
|
153 | console.log('PushNotification.unsubscribe failure: success callback parameter must be a function');
|
154 | return;
|
155 | }
|
156 |
|
157 | exec(successCallback, errorCallback, 'PushNotification', 'unsubscribe', [topic]);
|
158 | }
|
159 |
|
160 | |
161 |
|
162 |
|
163 |
|
164 | }, {
|
165 | key: 'setApplicationIconBadgeNumber',
|
166 | value: function setApplicationIconBadgeNumber(successCallback) {
|
167 | var errorCallback = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : function () {};
|
168 | var badge = arguments[2];
|
169 |
|
170 | if (typeof errorCallback !== 'function') {
|
171 | console.log('PushNotification.setApplicationIconBadgeNumber failure: failure ' + 'parameter not a function');
|
172 | return;
|
173 | }
|
174 |
|
175 | if (typeof successCallback !== 'function') {
|
176 | console.log('PushNotification.setApplicationIconBadgeNumber failure: success ' + 'callback parameter must be a function');
|
177 | return;
|
178 | }
|
179 |
|
180 | exec(successCallback, errorCallback, 'PushNotification', 'setApplicationIconBadgeNumber', [{ badge: badge }]);
|
181 | }
|
182 |
|
183 | |
184 |
|
185 |
|
186 |
|
187 | }, {
|
188 | key: 'getApplicationIconBadgeNumber',
|
189 | value: function getApplicationIconBadgeNumber(successCallback) {
|
190 | var errorCallback = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : function () {};
|
191 |
|
192 | if (typeof errorCallback !== 'function') {
|
193 | console.log('PushNotification.getApplicationIconBadgeNumber failure: failure ' + 'parameter not a function');
|
194 | return;
|
195 | }
|
196 |
|
197 | if (typeof successCallback !== 'function') {
|
198 | console.log('PushNotification.getApplicationIconBadgeNumber failure: success ' + 'callback parameter must be a function');
|
199 | return;
|
200 | }
|
201 |
|
202 | exec(successCallback, errorCallback, 'PushNotification', 'getApplicationIconBadgeNumber', []);
|
203 | }
|
204 |
|
205 | |
206 |
|
207 |
|
208 |
|
209 | }, {
|
210 | key: 'clearAllNotifications',
|
211 | value: function clearAllNotifications() {
|
212 | var successCallback = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : function () {};
|
213 | var errorCallback = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : function () {};
|
214 |
|
215 | if (typeof errorCallback !== 'function') {
|
216 | console.log('PushNotification.clearAllNotifications failure: failure parameter not a function');
|
217 | return;
|
218 | }
|
219 |
|
220 | if (typeof successCallback !== 'function') {
|
221 | console.log('PushNotification.clearAllNotifications failure: success callback ' + 'parameter must be a function');
|
222 | return;
|
223 | }
|
224 |
|
225 | exec(successCallback, errorCallback, 'PushNotification', 'clearAllNotifications', []);
|
226 | }
|
227 | |
228 |
|
229 |
|
230 |
|
231 |
|
232 |
|
233 |
|
234 |
|
235 |
|
236 |
|
237 |
|
238 |
|
239 |
|
240 | }, {
|
241 | key: 'on',
|
242 | value: function on(eventName, callback) {
|
243 | if (!this.handlers.hasOwnProperty(eventName)) {
|
244 | this.handlers[eventName] = [];
|
245 | }
|
246 | this.handlers[eventName].push(callback);
|
247 | }
|
248 |
|
249 | |
250 |
|
251 |
|
252 |
|
253 |
|
254 |
|
255 |
|
256 | }, {
|
257 | key: 'off',
|
258 | value: function off(eventName, handle) {
|
259 | if (this.handlers.hasOwnProperty(eventName)) {
|
260 | var handleIndex = this.handlers[eventName].indexOf(handle);
|
261 | if (handleIndex >= 0) {
|
262 | this.handlers[eventName].splice(handleIndex, 1);
|
263 | }
|
264 | }
|
265 | }
|
266 |
|
267 | |
268 |
|
269 |
|
270 |
|
271 |
|
272 |
|
273 |
|
274 |
|
275 |
|
276 |
|
277 |
|
278 | }, {
|
279 | key: 'emit',
|
280 | value: function emit() {
|
281 | for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
|
282 | args[_key] = arguments[_key];
|
283 | }
|
284 |
|
285 | var eventName = args.shift();
|
286 |
|
287 | if (!this.handlers.hasOwnProperty(eventName)) {
|
288 | return false;
|
289 | }
|
290 |
|
291 | for (var i = 0, length = this.handlers[eventName].length; i < length; i++) {
|
292 | var callback = this.handlers[eventName][i];
|
293 | if (typeof callback === 'function') {
|
294 | callback.apply(undefined, args);
|
295 | } else {
|
296 | console.log('event handler: ' + eventName + ' must be a function');
|
297 | }
|
298 | }
|
299 |
|
300 | return true;
|
301 | }
|
302 | }, {
|
303 | key: 'finish',
|
304 | value: function finish() {
|
305 | var successCallback = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : function () {};
|
306 | var errorCallback = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : function () {};
|
307 | var id = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'handler';
|
308 |
|
309 | if (typeof successCallback !== 'function') {
|
310 | console.log('finish failure: success callback parameter must be a function');
|
311 | return;
|
312 | }
|
313 |
|
314 | if (typeof errorCallback !== 'function') {
|
315 | console.log('finish failure: failure parameter not a function');
|
316 | return;
|
317 | }
|
318 |
|
319 | exec(successCallback, errorCallback, 'PushNotification', 'finish', [id]);
|
320 | }
|
321 | }]);
|
322 |
|
323 | return PushNotification;
|
324 | }();
|
325 |
|
326 |
|
327 |
|
328 |
|
329 |
|
330 | module.exports = {
|
331 | |
332 |
|
333 |
|
334 |
|
335 |
|
336 |
|
337 |
|
338 |
|
339 |
|
340 |
|
341 | init: function init(options) {
|
342 | return new PushNotification(options);
|
343 | },
|
344 |
|
345 | hasPermission: function hasPermission(successCallback, errorCallback) {
|
346 | exec(successCallback, errorCallback, 'PushNotification', 'hasPermission', []);
|
347 | },
|
348 |
|
349 | createChannel: function createChannel(successCallback, errorCallback, channel) {
|
350 | exec(successCallback, errorCallback, 'PushNotification', 'createChannel', [channel]);
|
351 | },
|
352 |
|
353 | deleteChannel: function deleteChannel(successCallback, errorCallback, channelId) {
|
354 | exec(successCallback, errorCallback, 'PushNotification', 'deleteChannel', [channelId]);
|
355 | },
|
356 |
|
357 | listChannels: function listChannels(successCallback, errorCallback) {
|
358 | exec(successCallback, errorCallback, 'PushNotification', 'listChannels', []);
|
359 | },
|
360 |
|
361 | |
362 |
|
363 |
|
364 |
|
365 |
|
366 |
|
367 |
|
368 | PushNotification: PushNotification
|
369 | }; |
\ | No newline at end of file |