UNPKG

11.9 kBJavaScriptView Raw
1/**
2* This file has been generated by Babel.
3*
4* DO NOT EDIT IT DIRECTLY
5*
6* Edit the JS source file src/js/push.js
7**/'use strict';
8
9var _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
11function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
12
13/* global cordova:false */
14/* globals window */
15
16/*!
17 * Module dependencies.
18 */
19
20var exec = cordova.require('cordova/exec');
21
22var PushNotification = function () {
23 /**
24 * PushNotification constructor.
25 *
26 * @param {Object} options to initiate Push Notifications.
27 * @return {PushNotification} instance that can be monitored and cancelled.
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 // require options parameter
41 if (typeof options === 'undefined') {
42 throw new Error('The options argument is required.');
43 }
44
45 // store the options to this object instance
46 this.options = options;
47
48 // triggered on registration and notification
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 // triggered on error
60 var fail = function fail(msg) {
61 var e = typeof msg === 'string' ? new Error(msg) : msg;
62 _this.emit('error', e);
63 };
64
65 // wait at least one process tick to allow event subscriptions
66 setTimeout(function () {
67 exec(success, fail, 'PushNotification', 'init', [options]);
68 }, 10);
69 }
70
71 /**
72 * Unregister from push notifications
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 * subscribe to a topic
110 * @param {String} topic topic to subscribe
111 * @param {Function} successCallback success callback
112 * @param {Function} errorCallback error callback
113 * @return {void}
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 * unsubscribe to a topic
136 * @param {String} topic topic to unsubscribe
137 * @param {Function} successCallback success callback
138 * @param {Function} errorCallback error callback
139 * @return {void}
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 * Call this to set the application icon badge
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 * Get the application icon badge
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 * Clear all notifications
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 * Listen for an event.
229 *
230 * The following events are supported:
231 *
232 * - registration
233 * - notification
234 * - error
235 *
236 * @param {String} eventName to subscribe to.
237 * @param {Function} callback triggered on the event.
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 * Remove event listener.
251 *
252 * @param {String} eventName to match subscription.
253 * @param {Function} handle function associated with event.
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 * Emit an event.
269 *
270 * This is intended for internal use only.
271 *
272 * @param {String} eventName is the event to trigger.
273 * @param {*} all arguments are passed to the event listeners.
274 *
275 * @return {Boolean} is true when the event is triggered otherwise false.
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 * Push Notification Plugin.
328 */
329
330module.exports = {
331 /**
332 * Register for Push Notifications.
333 *
334 * This method will instantiate a new copy of the PushNotification object
335 * and start the registration process.
336 *
337 * @param {Object} options
338 * @return {PushNotification} instance
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 * PushNotification Object.
363 *
364 * Expose the PushNotification object for direct use
365 * and testing. Typically, you should use the
366 * .init helper method.
367 */
368 PushNotification: PushNotification
369};
\No newline at end of file