|
|
@@ -15,6 +15,8 @@
|
|
|
* limitations under the License.
|
|
|
*/
|
|
|
|
|
|
+var exec = require('cordova/exec');
|
|
|
+
|
|
|
/**
|
|
|
* Request permission to show notifications.
|
|
|
*
|
|
|
@@ -24,7 +26,14 @@
|
|
|
* @return [ Void ]
|
|
|
*/
|
|
|
exports.hasPermission = function (callback, scope) {
|
|
|
- this.core.hasPermission(callback, scope);
|
|
|
+ var fn = this.createCallbackFn(callback, scope);
|
|
|
+
|
|
|
+ if (device.platform != 'iOS') {
|
|
|
+ fn(true);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ exec(fn, null, 'LocalNotification', 'check', []);
|
|
|
};
|
|
|
|
|
|
/**
|
|
|
@@ -36,43 +45,49 @@ exports.hasPermission = function (callback, scope) {
|
|
|
* @return [ Void ]
|
|
|
*/
|
|
|
exports.requestPermission = function (callback, scope) {
|
|
|
- this.core.requestPermission(callback, scope);
|
|
|
+ var fn = this.createCallbackFn(callback, scope);
|
|
|
+
|
|
|
+ if (device.platform != 'iOS') {
|
|
|
+ fn(true);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ exec(fn, null, 'LocalNotification', 'request', []);
|
|
|
};
|
|
|
|
|
|
-// /**
|
|
|
-// * Returns the default settings.
|
|
|
-// *
|
|
|
-// * @return {Object}
|
|
|
-// */
|
|
|
-// exports.getDefaults = function () {
|
|
|
-// return this.core.getDefaults();
|
|
|
-// };
|
|
|
+/**
|
|
|
+ * Schedule notifications.
|
|
|
+ *
|
|
|
+ * @param [ Array ] notifications The notifications to schedule.
|
|
|
+ * @param [ Function ] callback The function to be exec as the callback.
|
|
|
+ * @param [ Object ] scope The callback function's scope.
|
|
|
+ * @param [ Object ] args Optional flags how to schedule.
|
|
|
+ *
|
|
|
+ * @return [ Void ]
|
|
|
+ */
|
|
|
+exports.schedule = function (msgs, callback, scope, args) {
|
|
|
+ var fn = function (granted) {
|
|
|
|
|
|
-// /**
|
|
|
-// * Overwrite default settings.
|
|
|
-// *
|
|
|
-// * @param {Object} defaults
|
|
|
-// */
|
|
|
-// exports.setDefaults = function (defaults) {
|
|
|
-// this.core.setDefaults(defaults);
|
|
|
-// };
|
|
|
+ if (!granted) return;
|
|
|
|
|
|
-// /**
|
|
|
-// * Schedule a new local notification.
|
|
|
-// *
|
|
|
-// * @param {Object} notifications
|
|
|
-// * 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
|
|
|
-// * @param {Object?} args
|
|
|
-// * skipPermission:true schedules the notifications immediatly without
|
|
|
-// * registering or checking for permission
|
|
|
-// */
|
|
|
-// exports.schedule = function (notifications, callback, scope, args) {
|
|
|
-// this.core.schedule(notifications, callback, scope, args);
|
|
|
-// };
|
|
|
+ var notifications = Array.isArray(msgs) ? msgs : [msgs];
|
|
|
+
|
|
|
+ for (var i = 0; i < notifications.length; i++) {
|
|
|
+ var notification = notifications[i];
|
|
|
+
|
|
|
+ this.mergeWithDefaults(notification);
|
|
|
+ this.convertProperties(notification);
|
|
|
+ }
|
|
|
+
|
|
|
+ this.exec('schedule', notifications, callback, scope);
|
|
|
+ };
|
|
|
+
|
|
|
+ if (args && args.skipPermission) {
|
|
|
+ fn.call(this, true);
|
|
|
+ } else {
|
|
|
+ this.requestPermission(fn, this);
|
|
|
+ }
|
|
|
+};
|
|
|
|
|
|
// /**
|
|
|
// * Update existing notifications specified by IDs in options.
|
|
|
@@ -87,8 +102,27 @@ exports.requestPermission = function (callback, scope) {
|
|
|
// * skipPermission:true schedules the notifications immediatly without
|
|
|
// * registering or checking for permission
|
|
|
// */
|
|
|
-// exports.update = function (notifications, callback, scope, args) {
|
|
|
-// this.core.update(notifications, callback, scope, args);
|
|
|
+// exports.update = function (msgs, callback, scope, args) {
|
|
|
+// var fn = function(granted) {
|
|
|
+
|
|
|
+// if (!granted) return;
|
|
|
+
|
|
|
+// var notifications = Array.isArray(msgs) ? msgs : [msgs];
|
|
|
+
|
|
|
+// for (var i = 0; i < notifications.length; i++) {
|
|
|
+// var notification = notifications[i];
|
|
|
+
|
|
|
+// this.convertProperties(notification);
|
|
|
+// }
|
|
|
+
|
|
|
+// this.exec('update', notifications, callback, scope);
|
|
|
+// };
|
|
|
+
|
|
|
+// if (args && args.skipPermission) {
|
|
|
+// fn.call(this, true);
|
|
|
+// } else {
|
|
|
+// this.registerPermission(fn, this);
|
|
|
+// }
|
|
|
// };
|
|
|
|
|
|
// /**
|
|
|
@@ -102,7 +136,10 @@ exports.requestPermission = function (callback, scope) {
|
|
|
// * The scope for the callback function
|
|
|
// */
|
|
|
// exports.clear = function (ids, callback, scope) {
|
|
|
-// this.core.clear(ids, callback, scope);
|
|
|
+// ids = Array.isArray(ids) ? ids : [ids];
|
|
|
+// ids = this.convertIds(ids);
|
|
|
+
|
|
|
+// this.exec('clear', ids, callback, scope);
|
|
|
// };
|
|
|
|
|
|
// /**
|
|
|
@@ -114,7 +151,7 @@ exports.requestPermission = function (callback, scope) {
|
|
|
// * The scope for the callback function
|
|
|
// */
|
|
|
// exports.clearAll = function (callback, scope) {
|
|
|
-// this.core.clearAll(callback, scope);
|
|
|
+// this.exec('clearAll', null, callback, scope);
|
|
|
// };
|
|
|
|
|
|
// /**
|
|
|
@@ -128,7 +165,10 @@ exports.requestPermission = function (callback, scope) {
|
|
|
// * The scope for the callback function
|
|
|
// */
|
|
|
// exports.cancel = function (ids, callback, scope) {
|
|
|
-// this.core.cancel(ids, callback, scope);
|
|
|
+// ids = Array.isArray(ids) ? ids : [ids];
|
|
|
+// ids = this.convertIds(ids);
|
|
|
+
|
|
|
+// this.exec('cancel', ids, callback, scope);
|
|
|
// };
|
|
|
|
|
|
// /**
|
|
|
@@ -140,7 +180,7 @@ exports.requestPermission = function (callback, scope) {
|
|
|
// * The scope for the callback function
|
|
|
// */
|
|
|
// exports.cancelAll = function (callback, scope) {
|
|
|
-// this.core.cancelAll(callback, scope);
|
|
|
+// this.exec('cancelAll', null, callback, scope);
|
|
|
// };
|
|
|
|
|
|
// /**
|
|
|
@@ -154,7 +194,7 @@ exports.requestPermission = function (callback, scope) {
|
|
|
// * The scope for the callback function
|
|
|
// */
|
|
|
// exports.isPresent = function (id, callback, scope) {
|
|
|
-// this.core.isPresent(id, callback, scope);
|
|
|
+// this.exec('isPresent', id || 0, callback, scope);
|
|
|
// };
|
|
|
|
|
|
// /**
|
|
|
@@ -168,10 +208,10 @@ exports.requestPermission = function (callback, scope) {
|
|
|
// * The scope for the callback function
|
|
|
// */
|
|
|
// exports.isScheduled = function (id, callback, scope) {
|
|
|
-// this.core.isScheduled(id, callback, scope);
|
|
|
+// this.exec('isScheduled', id || 0, callback, scope);
|
|
|
// };
|
|
|
|
|
|
-// *
|
|
|
+// /**
|
|
|
// * Check if a notification with an ID was triggered.
|
|
|
// *
|
|
|
// * @param {String} id
|
|
|
@@ -180,21 +220,21 @@ exports.requestPermission = function (callback, scope) {
|
|
|
// * A callback function to be called with the list
|
|
|
// * @param {Object?} scope
|
|
|
// * The scope for the callback function
|
|
|
-
|
|
|
+// */
|
|
|
// exports.isTriggered = function (id, callback, scope) {
|
|
|
-// this.core.isTriggered(id, callback, scope);
|
|
|
+// this.exec('isTriggered', id || 0, callback, scope);
|
|
|
// };
|
|
|
|
|
|
-// /**
|
|
|
+// *
|
|
|
// * List all local notification IDs.
|
|
|
// *
|
|
|
// * @param {Function} callback
|
|
|
// * A callback function to be called with the list
|
|
|
// * @param {Object?} scope
|
|
|
// * The scope for the callback function
|
|
|
-// */
|
|
|
+
|
|
|
// exports.getAllIds = function (callback, scope) {
|
|
|
-// this.core.getAllIds(callback, scope);
|
|
|
+// this.exec('getAllIds', null, callback, scope);
|
|
|
// };
|
|
|
|
|
|
// /**
|
|
|
@@ -213,7 +253,7 @@ exports.requestPermission = function (callback, scope) {
|
|
|
// * The scope for the callback function
|
|
|
// */
|
|
|
// exports.getScheduledIds = function (callback, scope) {
|
|
|
-// this.core.getScheduledIds(callback, scope);
|
|
|
+// this.exec('getScheduledIds', null, callback, scope);
|
|
|
// };
|
|
|
|
|
|
// /**
|
|
|
@@ -225,7 +265,7 @@ exports.requestPermission = function (callback, scope) {
|
|
|
// * The scope for the callback function
|
|
|
// */
|
|
|
// exports.getTriggeredIds = function (callback, scope) {
|
|
|
-// this.core.getTriggeredIds(callback, scope);
|
|
|
+// this.exec('getTriggeredIds', null, callback, scope);
|
|
|
// };
|
|
|
|
|
|
// /**
|
|
|
@@ -240,7 +280,24 @@ exports.requestPermission = function (callback, scope) {
|
|
|
// * The scope for the callback function
|
|
|
// */
|
|
|
// exports.get = function () {
|
|
|
-// this.core.get.apply(this.core, arguments);
|
|
|
+// var args = Array.apply(null, arguments);
|
|
|
+
|
|
|
+// if (typeof args[0] == 'function') {
|
|
|
+// args.unshift([]);
|
|
|
+// }
|
|
|
+
|
|
|
+// var ids = args[0],
|
|
|
+// callback = args[1],
|
|
|
+// scope = args[2];
|
|
|
+
|
|
|
+// if (!Array.isArray(ids)) {
|
|
|
+// this.exec('getSingle', Number(ids), callback, scope);
|
|
|
+// return;
|
|
|
+// }
|
|
|
+
|
|
|
+// ids = this.convertIds(ids);
|
|
|
+
|
|
|
+// this.exec('getAll', ids, callback, scope);
|
|
|
// };
|
|
|
|
|
|
// /**
|
|
|
@@ -252,7 +309,7 @@ exports.requestPermission = function (callback, scope) {
|
|
|
// * The scope for the callback function
|
|
|
// */
|
|
|
// exports.getAll = function (callback, scope) {
|
|
|
-// this.core.getAll(callback, scope);
|
|
|
+// this.exec('getAll', null, callback, scope);
|
|
|
// };
|
|
|
|
|
|
// /**
|
|
|
@@ -267,7 +324,28 @@ exports.requestPermission = function (callback, scope) {
|
|
|
// * The scope for the callback function
|
|
|
// */
|
|
|
// exports.getScheduled = function () {
|
|
|
-// this.core.getScheduled.apply(this.core, arguments);
|
|
|
+// var args = Array.apply(null, arguments);
|
|
|
+
|
|
|
+// if (typeof args[0] == 'function') {
|
|
|
+// args.unshift([]);
|
|
|
+// }
|
|
|
+
|
|
|
+// var ids = args[0],
|
|
|
+// callback = args[1],
|
|
|
+// scope = args[2];
|
|
|
+
|
|
|
+// if (!Array.isArray(ids)) {
|
|
|
+// ids = [ids];
|
|
|
+// }
|
|
|
+
|
|
|
+// if (!Array.isArray(ids)) {
|
|
|
+// this.exec('getSingleScheduled', Number(ids), callback, scope);
|
|
|
+// return;
|
|
|
+// }
|
|
|
+
|
|
|
+// ids = this.convertIds(ids);
|
|
|
+
|
|
|
+// this.exec('getScheduled', ids, callback, scope);
|
|
|
// };
|
|
|
|
|
|
// /**
|
|
|
@@ -279,7 +357,7 @@ exports.requestPermission = function (callback, scope) {
|
|
|
// * The scope for the callback function
|
|
|
// */
|
|
|
// exports.getAllScheduled = function (callback, scope) {
|
|
|
-// this.core.getAllScheduled(callback, scope);
|
|
|
+// this.exec('getScheduled', null, callback, scope);
|
|
|
// };
|
|
|
|
|
|
// /**
|
|
|
@@ -294,7 +372,28 @@ exports.requestPermission = function (callback, scope) {
|
|
|
// * The scope for the callback function
|
|
|
// */
|
|
|
// exports.getTriggered = function () {
|
|
|
-// this.core.getTriggered.apply(this.core, arguments);
|
|
|
+// var args = Array.apply(null, arguments);
|
|
|
+
|
|
|
+// if (typeof args[0] == 'function') {
|
|
|
+// args.unshift([]);
|
|
|
+// }
|
|
|
+
|
|
|
+// var ids = args[0],
|
|
|
+// callback = args[1],
|
|
|
+// scope = args[2];
|
|
|
+
|
|
|
+// if (!Array.isArray(ids)) {
|
|
|
+// ids = [ids];
|
|
|
+// }
|
|
|
+
|
|
|
+// if (!Array.isArray(ids)) {
|
|
|
+// this.exec('getSingleTriggered', Number(ids), callback, scope);
|
|
|
+// return;
|
|
|
+// }
|
|
|
+
|
|
|
+// ids = this.convertIds(ids);
|
|
|
+
|
|
|
+// this.exec('getTriggered', ids, callback, scope);
|
|
|
// };
|
|
|
|
|
|
// /**
|
|
|
@@ -306,31 +405,78 @@ exports.requestPermission = function (callback, scope) {
|
|
|
// * The scope for the callback function
|
|
|
// */
|
|
|
// exports.getAllTriggered = function (callback, scope) {
|
|
|
-// this.core.getAllTriggered(callback, scope);
|
|
|
+// this.exec('getTriggered', null, callback, scope);
|
|
|
// };
|
|
|
|
|
|
-// /**
|
|
|
-// * Register callback for given event.
|
|
|
-// *
|
|
|
-// * @param {String} event
|
|
|
-// * The event's name
|
|
|
-// * @param {Function} callback
|
|
|
-// * The function to be exec as callback
|
|
|
-// * @param {Object?} scope
|
|
|
-// * The callback function's scope
|
|
|
-// */
|
|
|
-// exports.on = function (event, callback, scope) {
|
|
|
-// this.core.on(event, callback, scope);
|
|
|
-// };
|
|
|
+/**
|
|
|
+ * The (platform specific) default settings.
|
|
|
+ *
|
|
|
+ * @return [ Object ]
|
|
|
+ */
|
|
|
+exports.getDefaults = function () {
|
|
|
+ return this._defaults;
|
|
|
+};
|
|
|
|
|
|
-// /**
|
|
|
-// * Unregister callback for given event.
|
|
|
-// *
|
|
|
-// * @param {String} event
|
|
|
-// * The event's name
|
|
|
-// * @param {Function} callback
|
|
|
-// * The function to be exec as callback
|
|
|
-// */
|
|
|
-// exports.un = function (event, callback) {
|
|
|
-// this.core.un(event, callback);
|
|
|
-// };
|
|
|
+/**
|
|
|
+ * Overwrite default settings.
|
|
|
+ *
|
|
|
+ * @param [ Object ] newDefaults New default values.
|
|
|
+ *
|
|
|
+ * @return [ Void ]
|
|
|
+ */
|
|
|
+exports.setDefaults = function (newDefaults) {
|
|
|
+ var defaults = this.getDefaults();
|
|
|
+
|
|
|
+ for (var key in defaults) {
|
|
|
+ if (newDefaults.hasOwnProperty(key)) {
|
|
|
+ defaults[key] = newDefaults[key];
|
|
|
+ }
|
|
|
+ }
|
|
|
+};
|
|
|
+
|
|
|
+/**
|
|
|
+ * Register callback for given event.
|
|
|
+ *
|
|
|
+ * @param [ String ] event The name of the event.
|
|
|
+ * @param [ Function ] callback The function to be exec as callback.
|
|
|
+ * @param [ Object ] scope The callback function's scope.
|
|
|
+ *
|
|
|
+ * @return [ Void ]
|
|
|
+ */
|
|
|
+exports.on = function (event, callback, scope) {
|
|
|
+
|
|
|
+ if (typeof callback !== "function")
|
|
|
+ return;
|
|
|
+
|
|
|
+ if (!this._listener[event]) {
|
|
|
+ this._listener[event] = [];
|
|
|
+ }
|
|
|
+
|
|
|
+ var item = [callback, scope || window];
|
|
|
+
|
|
|
+ this._listener[event].push(item);
|
|
|
+};
|
|
|
+
|
|
|
+/**
|
|
|
+ * Unregister callback for given event.
|
|
|
+ *
|
|
|
+ * @param [ String ] event The name of the event.
|
|
|
+ * @param [ Function ] callback The function to be exec as callback.
|
|
|
+ *
|
|
|
+ * @return [ Void ]
|
|
|
+ */
|
|
|
+exports.un = function (event, callback) {
|
|
|
+ var listener = this._listener[event];
|
|
|
+
|
|
|
+ if (!listener)
|
|
|
+ return;
|
|
|
+
|
|
|
+ for (var i = 0; i < listener.length; i++) {
|
|
|
+ var fn = listener[i][0];
|
|
|
+
|
|
|
+ if (fn == callback) {
|
|
|
+ listener.splice(i, 1);
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+};
|