-
Notifications
You must be signed in to change notification settings - Fork 0
/
local-notification.js
356 lines (309 loc) · 10.3 KB
/
local-notification.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
/*
Copyright 2013-2014 appPlant UG
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
*/
var LocalNotification = function () {
this._defaults = {
message: '',
title: '',
autoCancel: false,
badge: 0,
id: '0',
json: '',
repeat: ''
};
};
LocalNotification.prototype = {
/**
* Returns the default settings
*
* @return {Object}
*/
getDefaults: function () {
return this._defaults;
},
/**
* Overwrite default settings
*
* @param {Object} defaults
*/
setDefaults: function (newDefaults) {
var defaults = this.getDefaults();
for (var key in defaults) {
if (newDefaults[key] !== undefined) {
defaults[key] = newDefaults[key];
}
}
},
/**
* @private
*
* Merges custom properties with the default values.
*
* @param {Object} options
* Set of custom values
*
* @retrun {Object}
* The merged property list
*/
mergeWithDefaults: function (options) {
var defaults = this.getDefaults();
for (var key in defaults) {
if (options[key] === undefined) {
options[key] = defaults[key];
}
}
return options;
},
/**
* @private
*
* Merges the platform specific properties into the default properties.
*
* @return {Object}
* The default properties for the platform
*/
applyPlatformSpecificOptions: function () {
var defaults = this._defaults;
switch (device.platform) {
case 'Android':
defaults.icon = 'icon';
defaults.smallIcon = null;
defaults.ongoing = false;
defaults.led = 'FFFFFF'; /*RRGGBB*/
defaults.sound = 'TYPE_NOTIFICATION'; break;
case 'iOS':
defaults.sound = ''; break;
case 'WinCE': case 'Win32NT':
defaults.smallImage = null;
defaults.image = null;
defaults.wideImage = null;
}
return defaults;
},
/**
* @private
*
* Creates a callback, which will be executed within a specific scope.
*
* @param {Function} callbackFn
* The callback function
* @param {Object} scope
* The scope for the function
*
* @return {Function}
* The new callback function
*/
createCallbackFn: function (callbackFn, scope) {
if (typeof callbackFn != 'function')
return;
return function () {
callbackFn.apply(scope || this, arguments);
};
},
/**
* Add a new entry to the registry
*
* @param {Object} options
* The notification properties
* @param {Function} callback
* A function to be called after the notification has been canceled
* @param {Object} scope
* The scope for the callback function
*
* @return {Number}
* The notification's ID
*/
add: function (options, callback, scope) {
var options = this.mergeWithDefaults(options),
callbackFn = this.createCallbackFn(callback, scope);
if (options.id) {
options.id = options.id.toString();
}
if (options.date === undefined) {
options.date = new Date();
}
if (options.title) {
options.title = options.title.toString();
}
if (options.message) {
options.message = options.message.toString();
}
if (typeof options.date == 'object') {
options.date = Math.round(options.date.getTime()/1000);
}
if (['WinCE', 'Win32NT'].indexOf(device.platform) > -1) {
callbackFn = function (cmd) {
eval(cmd);
};
}
cordova.exec(callbackFn, null, 'LocalNotification', 'add', [options]);
return options.id;
},
/**
* Cancels the specified notification.
*
* @param {String} id
* The ID of the notification
* @param {Function} callback
* A function to be called after the notification has been canceled
* @param {Object} scope
* The scope for the callback function
*/
cancel: function (id, callback, scope) {
var id = id.toString(),
callbackFn = this.createCallbackFn(callback, scope);
cordova.exec(callbackFn, null, 'LocalNotification', 'cancel', [id]);
},
/**
* Removes all previously registered notifications.
*
* @param {Function} callback
* A function to be called after all notifications have been canceled
* @param {Object} scope
* The scope for the callback function
*/
cancelAll: function (callback, scope) {
var callbackFn = this.createCallbackFn(callback, scope);
cordova.exec(callbackFn, null, 'LocalNotification', 'cancelAll', []);
},
/**
* Retrieves a list with all currently pending notifications.
*
* @param {Function} callback
* A callback function to be called with the list
* @param {Object} scope
* The scope for the callback function
*/
getScheduledIds: function (callback, scope) {
var callbackFn = this.createCallbackFn(callback, scope);
cordova.exec(callbackFn, null, 'LocalNotification', 'getScheduledIds', []);
},
/**
* Checks wether a notification with an ID is scheduled.
*
* @param {String} id
* The ID of the notification
* @param {Function} callback
* A callback function to be called with the list
* @param {Object} scope
* The scope for the callback function
*/
isScheduled: function (id, callback, scope) {
var id = id.toString(),
callbackFn = this.createCallbackFn(callback, scope);
cordova.exec(callbackFn, null, 'LocalNotification', 'isScheduled', [id]);
},
/**
* Retrieves a list with all triggered notifications.
*
* @param {Function} callback
* A callback function to be called with the list
* @param {Object} scope
* The scope for the callback function
*/
getTriggeredIds: function (callback, scope) {
var callbackFn = this.createCallbackFn(callback, scope);
cordova.exec(callbackFn, null, 'LocalNotification', 'getTriggeredIds', []);
},
/**
* Checks wether a notification with an ID was triggered.
*
* @param {String} id
* The ID of the notification
* @param {Function} callback
* A callback function to be called with the list
* @param {Object} scope
* The scope for the callback function
*/
isTriggered: function (id, callback, scope) {
var id = id.toString(),
callbackFn = this.createCallbackFn(callback, scope);
cordova.exec(callbackFn, null, 'LocalNotification', 'isTriggered', [id]);
},
/**
* Occurs when a notification was added.
*
* @param {String} id
* The ID of the notification
* @param {String} state
* Either "foreground" or "background"
* @param {String} json
* A custom (JSON) string
*/
onadd: function (id, state, json) {},
/**
* Occurs when the notification is triggered.
*
* @param {String} id
* The ID of the notification
* @param {String} state
* Either "foreground" or "background"
* @param {String} json
* A custom (JSON) string
*/
ontrigger: function (id, state, json) {},
/**
* Fires after the notification was clicked.
*
* @param {String} id
* The ID of the notification
* @param {String} state
* Either "foreground" or "background"
* @param {String} json
* A custom (JSON) string
*/
onclick: function (id, state, json) {},
/**
* Fires if the notification was canceled.
*
* @param {String} id
* The ID of the notification
* @param {String} state
* Either "foreground" or "background"
* @param {String} json
* A custom (JSON) string
*/
oncancel: function (id, state, json) {}
};
var plugin = new LocalNotification(),
channel = require('cordova/channel');
// Called after all 'deviceready' listener are called
channel.deviceready.subscribe( function () {
// Device is ready now, the listeners are registered and all queued events
// can be executed now.
cordova.exec(null, null, 'LocalNotification', 'deviceready', []);
});
channel.onCordovaReady.subscribe( function () {
// The cordova device plugin is ready now
channel.onCordovaInfoReady.subscribe( function () {
if (device.platform == 'Android') {
channel.onPause.subscribe( function () {
// Necessary to set the state to `background`
cordova.exec(null, null, 'LocalNotification', 'pause', []);
});
channel.onResume.subscribe( function () {
// Necessary to set the state to `foreground`
cordova.exec(null, null, 'LocalNotification', 'resume', []);
});
// Necessary to set the state to `foreground`
cordova.exec(null, null, 'LocalNotification', 'resume', []);
}
// Merges the platform specific properties into the default properties
plugin.applyPlatformSpecificOptions();
});
});
module.exports = plugin;