Commit 8418c152 by Sebastián Katzer

New interface methods `getScheduled` and `getTriggered` (iOS)

parent 40599e17
...@@ -36,10 +36,14 @@ ...@@ -36,10 +36,14 @@
- (void) isScheduled:(CDVInvokedUrlCommand*)command; - (void) isScheduled:(CDVInvokedUrlCommand*)command;
// Check if a notification with an ID was triggered // Check if a notification with an ID was triggered
- (void) isTriggered:(CDVInvokedUrlCommand*)command; - (void) isTriggered:(CDVInvokedUrlCommand*)command;
// Retrieve a list of ids from all pending notifications // List all ids from all pending notifications
- (void) getScheduledIds:(CDVInvokedUrlCommand*)command; - (void) getScheduledIds:(CDVInvokedUrlCommand*)command;
// Retrieve a list of ids from all triggered notifications // List all ids from all triggered notifications
- (void) getTriggeredIds:(CDVInvokedUrlCommand*)command; - (void) getTriggeredIds:(CDVInvokedUrlCommand*)command;
// List all properties for given scheduled notifications
- (void) getScheduled:(CDVInvokedUrlCommand*)command;
// List all properties for given triggered notifications
- (void) getTriggered:(CDVInvokedUrlCommand*)command;
// Inform if the app has the permission to show notifications // Inform if the app has the permission to show notifications
- (void) hasPermission:(CDVInvokedUrlCommand*)command; - (void) hasPermission:(CDVInvokedUrlCommand*)command;
// Register permission to show notifications // Register permission to show notifications
......
...@@ -117,7 +117,7 @@ ...@@ -117,7 +117,7 @@
} }
/** /**
* Cancels all currently scheduled notifications. * Cancel all currently scheduled notifications.
*/ */
- (void) cancelAll:(CDVInvokedUrlCommand*)command - (void) cancelAll:(CDVInvokedUrlCommand*)command
{ {
...@@ -157,7 +157,35 @@ ...@@ -157,7 +157,35 @@
} }
/** /**
* List of ids from all currently pending notifications. * Check if a notification with an ID was triggered.
*
* @param id
* The ID of the notification
*/
- (void) isTriggered:(CDVInvokedUrlCommand*)command
{
[self.commandDelegate runInBackground:^{
NSString* id = [[command arguments]
objectAtIndex:0];
CDVPluginResult* result;
UILocalNotification* notification;
notification = [[UIApplication sharedApplication]
triggeredLocalNotificationWithId:id];
bool isTriggered = notification != NULL;
result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK
messageAsBool:isTriggered];
[self.commandDelegate sendPluginResult:result
callbackId:command.callbackId];
}];
}
/**
* List all ids from all pending notifications.
*/ */
- (void) getScheduledIds:(CDVInvokedUrlCommand*)command - (void) getScheduledIds:(CDVInvokedUrlCommand*)command
{ {
...@@ -177,27 +205,48 @@ ...@@ -177,27 +205,48 @@
} }
/** /**
* Checks wether a notification with an ID was triggered. * List all ids from all triggered notifications.
*
* @param id
* The ID of the notification
*/ */
- (void) isTriggered:(CDVInvokedUrlCommand*)command - (void) getTriggeredIds:(CDVInvokedUrlCommand*)command
{ {
[self.commandDelegate runInBackground:^{ [self.commandDelegate runInBackground:^{
NSString* id = [[command arguments]
objectAtIndex:0];
CDVPluginResult* result; CDVPluginResult* result;
UILocalNotification* notification; NSArray* triggeredIds;
notification = [[UIApplication sharedApplication] triggeredIds = [[UIApplication sharedApplication]
triggeredLocalNotificationWithId:id]; triggeredLocalNotificationIds];
bool isTriggered = notification != NULL; result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK
messageAsArray:triggeredIds];
[self.commandDelegate sendPluginResult:result
callbackId:command.callbackId];
}];
}
/**
* List all properties for given scheduled notifications.
*
* @param ids
* The IDs of the notifications
*/
- (void) getScheduled:(CDVInvokedUrlCommand*)command
{
[self.commandDelegate runInBackground:^{
NSArray* ids = command.arguments;
NSArray* notifications;
CDVPluginResult* result;
if (ids.count == 0) {
notifications = [[UIApplication sharedApplication]
scheduledLocalNotificationOptions];
} else {
notifications = [[UIApplication sharedApplication]
scheduledLocalNotificationOptions:ids];
}
result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK
messageAsBool:isTriggered]; messageAsArray:notifications];
[self.commandDelegate sendPluginResult:result [self.commandDelegate sendPluginResult:result
callbackId:command.callbackId]; callbackId:command.callbackId];
...@@ -205,19 +254,28 @@ ...@@ -205,19 +254,28 @@
} }
/** /**
* Retrieves a list of ids from all currently triggered notifications. * List all properties for given triggered notifications.
*
* @param ids
* The IDs of the notifications
*/ */
- (void) getTriggeredIds:(CDVInvokedUrlCommand*)command - (void) getTriggered:(CDVInvokedUrlCommand *)command
{ {
[self.commandDelegate runInBackground:^{ [self.commandDelegate runInBackground:^{
NSArray* ids = command.arguments;
NSArray* notifications;
CDVPluginResult* result; CDVPluginResult* result;
NSArray* triggeredIds;
triggeredIds = [[UIApplication sharedApplication] if (ids.count == 0) {
triggeredLocalNotificationIds]; notifications = [[UIApplication sharedApplication]
triggeredLocalNotificationOptions];
} else {
notifications = [[UIApplication sharedApplication]
triggeredLocalNotificationOptions:ids];
}
result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK
messageAsArray:triggeredIds]; messageAsArray:notifications];
[self.commandDelegate sendPluginResult:result [self.commandDelegate sendPluginResult:result
callbackId:command.callbackId]; callbackId:command.callbackId];
......
...@@ -33,5 +33,13 @@ ...@@ -33,5 +33,13 @@
- (UILocalNotification*) scheduledLocalNotificationWithId:(NSString*)id; - (UILocalNotification*) scheduledLocalNotificationWithId:(NSString*)id;
// Get the triggered local notification by ID // Get the triggered local notification by ID
- (UILocalNotification*) triggeredLocalNotificationWithId:(NSString*)id; - (UILocalNotification*) triggeredLocalNotificationWithId:(NSString*)id;
// List of properties from all scheduled notifications
- (NSArray*) scheduledLocalNotificationOptions;
// List of properties from given scheduled notifications
- (NSArray*) scheduledLocalNotificationOptions:(NSArray*)ids;
// List of properties from all triggered notifications
- (NSArray*) triggeredLocalNotificationOptions;
// List of properties from given triggered notifications
- (NSArray*) triggeredLocalNotificationOptions:(NSArray*)ids;
@end @end
...@@ -97,15 +97,15 @@ ...@@ -97,15 +97,15 @@
*/ */
- (NSArray*) triggeredLocalNotificationIds - (NSArray*) triggeredLocalNotificationIds
{ {
NSArray* triggeredNotifications = self.triggeredLocalNotifications; NSArray* notifications = self.triggeredLocalNotifications;
NSMutableArray* triggeredNotificationIds = [[NSMutableArray alloc] init]; NSMutableArray* ids = [[NSMutableArray alloc] init];
for (UILocalNotification* notification in triggeredNotifications) for (UILocalNotification* notification in notifications)
{ {
[triggeredNotificationIds addObject:notification.options.id]; [ids addObject:notification.options.id];
} }
return triggeredNotificationIds; return ids;
} }
/** /**
...@@ -113,22 +113,24 @@ ...@@ -113,22 +113,24 @@
*/ */
- (NSArray*) scheduledLocalNotificationIds - (NSArray*) scheduledLocalNotificationIds
{ {
NSArray* scheduledNotifications = self.scheduledLocalNotifications; NSArray* notifications = self.scheduledLocalNotifications;
NSMutableArray* scheduledNotificationIds = [[NSMutableArray alloc] init]; NSMutableArray* ids = [[NSMutableArray alloc] init];
for (UILocalNotification* notification in scheduledNotifications) for (UILocalNotification* notification in notifications)
{
if (notification)
{ {
[scheduledNotificationIds addObject:notification.options.id]; if (notification) {
[ids addObject:notification.options.id];
} }
} }
return scheduledNotificationIds; return ids;
} }
/** /**
* Get the scheduled local notification by ID. * Get the scheduled local notification by ID.
*
* @param id
* Notification ID
*/ */
- (UILocalNotification*) scheduledLocalNotificationWithId:(NSString*)id - (UILocalNotification*) scheduledLocalNotificationWithId:(NSString*)id
{ {
...@@ -136,8 +138,7 @@ ...@@ -136,8 +138,7 @@
for (UILocalNotification* notification in notifications) for (UILocalNotification* notification in notifications)
{ {
if (notification && [notification.options.id isEqualToString:id]) if (notification && [notification.options.id isEqualToString:id]) {
{
return notification; return notification;
} }
} }
...@@ -147,6 +148,9 @@ ...@@ -147,6 +148,9 @@
/** /**
* Get the triggered local notification by ID. * Get the triggered local notification by ID.
*
* @param id
* Notification ID
*/ */
- (UILocalNotification*) triggeredLocalNotificationWithId:(NSString*)id - (UILocalNotification*) triggeredLocalNotificationWithId:(NSString*)id
{ {
...@@ -159,4 +163,84 @@ ...@@ -159,4 +163,84 @@
return NULL; return NULL;
} }
/**
* List of properties from all scheduled notifications.
*/
- (NSArray*) scheduledLocalNotificationOptions
{
NSArray* notifications = self.scheduledLocalNotifications;
NSMutableArray* options = [[NSMutableArray alloc] init];
for (UILocalNotification* notification in notifications)
{
if (notification) {
[options addObject:notification.userInfo];
}
}
return options;
}
/**
* List of properties from given scheduled notifications.
*
* @param ids
* Notification IDs
*/
- (NSArray*) scheduledLocalNotificationOptions:(NSArray*)ids
{
UILocalNotification* notification;
NSMutableArray* options = [[NSMutableArray alloc] init];
for (NSString* id in ids)
{
notification = [self scheduledLocalNotificationWithId:id];
if (notification) {
[options addObject:notification.userInfo];
}
}
return options;
}
/**
* List of properties from all triggered notifications.
*/
- (NSArray*) triggeredLocalNotificationOptions
{
NSArray* notifications = self.triggeredLocalNotifications;
NSMutableArray* options = [[NSMutableArray alloc] init];
for (UILocalNotification* notification in notifications)
{
[options addObject:notification.userInfo];
}
return options;
}
/**
* List of properties from given triggered notifications.
*
* @param ids
* Notification IDs
*/
- (NSArray*) triggeredLocalNotificationOptions:(NSArray*)ids
{
UILocalNotification* notification;
NSMutableArray* options = [[NSMutableArray alloc] init];
for (NSString* id in ids)
{
notification = [self triggeredLocalNotificationWithId:id];
if (notification) {
[options addObject:notification.userInfo];
}
}
return options;
}
@end @end
...@@ -102,7 +102,7 @@ exports.setDefaults = function (newDefaults) { ...@@ -102,7 +102,7 @@ exports.setDefaults = function (newDefaults) {
* The notification properties * The notification properties
* @param {Function} callback * @param {Function} callback
* A function to be called after the notification has been canceled * A function to be called after the notification has been canceled
* @param {Object} scope * @param {Object?} scope
* The scope for the callback function * The scope for the callback function
*/ */
exports.add = function (props, callback, scope) { exports.add = function (props, callback, scope) {
...@@ -135,7 +135,7 @@ exports.add = function (props, callback, scope) { ...@@ -135,7 +135,7 @@ exports.add = function (props, callback, scope) {
* The notification properties to update * The notification properties to update
* @param {Function} callback * @param {Function} callback
* A function to be called after the notification has been updated * A function to be called after the notification has been updated
* @param {Object} scope * @param {Object?} scope
* The scope for the callback function * The scope for the callback function
*/ */
exports.update = function (options, callback, scope) { exports.update = function (options, callback, scope) {
...@@ -143,13 +143,13 @@ exports.update = function (options, callback, scope) { ...@@ -143,13 +143,13 @@ exports.update = function (options, callback, scope) {
}; };
/** /**
* Clears the specified notification. * Clear the specified notification.
* *
* @param {String} id * @param {String} id
* The ID of the notification * The ID of the notification
* @param {Function} callback * @param {Function} callback
* A function to be called after the notification has been cleared * A function to be called after the notification has been cleared
* @param {Object} scope * @param {Object?} scope
* The scope for the callback function * The scope for the callback function
*/ */
exports.clear = function (id, callback, scope) { exports.clear = function (id, callback, scope) {
...@@ -159,11 +159,11 @@ exports.clear = function (id, callback, scope) { ...@@ -159,11 +159,11 @@ exports.clear = function (id, callback, scope) {
}; };
/** /**
* Clears all previously sheduled notifications. * Clear all previously sheduled notifications.
* *
* @param {Function} callback * @param {Function} callback
* A function to be called after all notifications have been cleared * A function to be called after all notifications have been cleared
* @param {Object} scope * @param {Object?} scope
* The scope for the callback function * The scope for the callback function
*/ */
exports.clearAll = function (callback, scope) { exports.clearAll = function (callback, scope) {
...@@ -171,22 +171,20 @@ exports.clearAll = function (callback, scope) { ...@@ -171,22 +171,20 @@ exports.clearAll = function (callback, scope) {
}; };
/** /**
* Cancels the specified notifications. * Cancel the specified notifications.
* *
* @param {String[]} ids * @param {String[]} ids
* The IDs of the notifications * The IDs of the notifications
* @param {Function} callback * @param {Function} callback
* A function to be called after the notifications has been canceled * A function to be called after the notifications has been canceled
* @param {Object} scope * @param {Object?} scope
* The scope for the callback function * The scope for the callback function
*/ */
exports.cancel = function (ids, callback, scope) { exports.cancel = function (ids, callback, scope) {
ids = Array.isArray(ids) ? ids : [ids]; ids = Array.isArray(ids) ? ids : [ids];
for (var i = 0; i < ids.length; i++) { ids = this.convertIds(ids);
ids[i] = ids[i].toString();
}
if (device.platform != 'iOS') { if (device.platform != 'iOS') {
ids = ids[0]; ids = ids[0];
...@@ -196,11 +194,11 @@ exports.cancel = function (ids, callback, scope) { ...@@ -196,11 +194,11 @@ exports.cancel = function (ids, callback, scope) {
}; };
/** /**
* Removes all previously registered notifications. * Remove all previously registered notifications.
* *
* @param {Function} callback * @param {Function} callback
* A function to be called after all notifications have been canceled * A function to be called after all notifications have been canceled
* @param {Object} scope * @param {Object?} scope
* The scope for the callback function * The scope for the callback function
*/ */
exports.cancelAll = function (callback, scope) { exports.cancelAll = function (callback, scope) {
...@@ -208,39 +206,55 @@ exports.cancelAll = function (callback, scope) { ...@@ -208,39 +206,55 @@ exports.cancelAll = function (callback, scope) {
}; };
/** /**
* Retrieves a list with all currently pending notifications. * Check if a notification with an ID is scheduled.
* *
* @param {String} id
* The ID of the notification
* @param {Function} callback * @param {Function} callback
* A callback function to be called with the list * A callback function to be called with the list
* @param {Object} scope * @param {Object?} scope
* The scope for the callback function * The scope for the callback function
*/ */
exports.getScheduledIds = function (callback, scope) { exports.isScheduled = function (id, callback, scope) {
this.exec('getScheduledIds', null, callback, scope); var notId = (id || '0').toString();
this.exec('isScheduled', notId, callback, scope);
}; };
/** /**
* Checks wether a notification with an ID is scheduled. * Check if a notification with an ID was triggered.
* *
* @param {String} id * @param {String} id
* The ID of the notification * The ID of the notification
* @param {Function} callback * @param {Function} callback
* A callback function to be called with the list * A callback function to be called with the list
* @param {Object} scope * @param {Object?} scope
* The scope for the callback function * The scope for the callback function
*/ */
exports.isScheduled = function (id, callback, scope) { exports.isTriggered = function (id, callback, scope) {
var notId = (id || '0').toString(); var notId = (id || '0').toString();
this.exec('isScheduled', notId, callback, scope); this.exec('isTriggered', notId, callback, scope);
}; };
/** /**
* Retrieves a list with all triggered notifications. * List all currently pending notifications.
* *
* @param {Function} callback * @param {Function} callback
* A callback function to be called with the list * A callback function to be called with the list
* @param {Object} scope * @param {Object?} scope
* The scope for the callback function
*/
exports.getScheduledIds = function (callback, scope) {
this.exec('getScheduledIds', null, callback, scope);
};
/**
* List all triggered notifications.
*
* @param {Function} callback
* A callback function to be called with the list
* @param {Object?} scope
* The scope for the callback function * The scope for the callback function
*/ */
exports.getTriggeredIds = function (callback, scope) { exports.getTriggeredIds = function (callback, scope) {
...@@ -248,19 +262,89 @@ exports.getTriggeredIds = function (callback, scope) { ...@@ -248,19 +262,89 @@ exports.getTriggeredIds = function (callback, scope) {
}; };
/** /**
* Checks wether a notification with an ID was triggered. * List all properties for given scheduled notifications.
* If called without IDs, all notification will be returned.
* *
* @param {String} id * @param {Number[]?} ids
* The ID of the notification * Set of notification IDs
* @param {Function} callback * @param {Function} callback
* A callback function to be called with the list * A callback function to be called with the list
* @param {Object} scope * @param {Object?} scope
* The scope for the callback function * The scope for the callback function
*/ */
exports.isTriggered = function (id, callback, scope) { exports.getScheduled = function () {
var notId = (id || '0').toString(); var args = Array.apply(null, arguments);
this.exec('isTriggered', notId, callback, scope); if (typeof args[0] == 'function') {
args.unshift([]);
}
var ids = args[0],
callback = args[1],
scope = args[2];
if (!Array.isArray(ids)) {
ids = [ids];
}
ids = this.convertIds(ids);
this.exec('getScheduled', ids, callback, scope);
};
/**
* Retrieve the properties for all scheduled notifications.
*
* @param {Function} callback
* A callback function to be called with the list
* @param {Object?} scope
* The scope for the callback function
*/
exports.getAllScheduled = function (callback, scope) {
this.exec('getScheduled', null, callback, scope);
};
/**
* List all properties for given triggered notifications.
* If called without IDs, all notification will be returned.
*
* @param {Number[]?} ids
* Set of notification IDs
* @param {Function} callback
* A callback function to be called with the list
* @param {Object?} scope
* The scope for the callback function
*/
exports.getTriggered = function () {
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];
}
ids = this.convertIds(ids);
this.exec('getTriggered', ids, callback, scope);
};
/**
* Retrieve the properties for all triggered notifications.
*
* @param {Function} callback
* A callback function to be called with the list
* @param {Object?} scope
* The scope for the callback function
*/
exports.getAllTriggered = function (callback, scope) {
this.exec('getTriggered', null, callback, scope);
}; };
/** /**
...@@ -318,102 +402,6 @@ exports.promptForPermission = function (callback, scope) { ...@@ -318,102 +402,6 @@ exports.promptForPermission = function (callback, scope) {
}; };
/** /**
* Add new entries to the registry (more than one)
*
* @param {Object} options
* The notification properties
* @param {Function} callback
* A function to be called after the notification has been added
* @param {Object} scope
* The scope for the callback function
*
* @return {Number}
* The notification's ID
*/
exports.addMultiple = function (notifications, callback, scope) {
var length = notifications.length;
var notificationsMerged = new Array(),
callbackFn = this.createCallbackFn(callback, scope);
for (var i=0;i<length;i++){
var options = this.mergeWithDefaults(notifications[i]);
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);
};
}
notificationsMerged.push(options);
}
cordova.exec(callbackFn, null, 'LocalNotification', 'addMultiple', notificationsMerged);
return options.id;
};
/**
* Clear the specified notifications (more than one).
*
* @param {String} id
* The ID of the notification
* @param {Function} callback
* A function to be called after the notifications has been cleared.
* @param {Object} scope
* The scope for the callback function
*/
exports.clearMultiple = function (ids, callback, scope) {
var length = ids.length;
var idArray = new Array(),
callbackFn = this.createCallbackFn(callback, scope);
for (var i=0;i<length;i++){
var id = ids[i].toString();
idArray.push(id);
}
var callbackFn = this.createCallbackFn(callback, scope);
cordova.exec(callbackFn, null, 'LocalNotification', 'clearMultiple', [ids]);
};
/**
* Cancel the specified notifications (more than one).
*
* @param {String} id
* The ID of the notification
* @param {Function} callback
* A function to be called after the notifications has been canceled
* @param {Object} scope
* The scope for the callback function
*/
exports.cancelMultiple = function (ids, callback, scope) {
var length = ids.length;
var idArray = new Array(),
callbackFn = this.createCallbackFn(callback, scope);
for (var i=0;i<length;i++){
var id = ids[i].toString();
idArray.push(id);
}
var callbackFn = this.createCallbackFn(callback, scope);
cordova.exec(callbackFn, null, 'LocalNotification', 'cancelMultiple', [ids]);
};
/**
* Occurs when a notification was added. * Occurs when a notification was added.
* *
* @param {String} id * @param {String} id
...@@ -604,6 +592,25 @@ exports.createCallbackFn = function (callbackFn, scope) { ...@@ -604,6 +592,25 @@ exports.createCallbackFn = function (callbackFn, scope) {
/** /**
* @private * @private
* *
* Convert the IDs to Strings.
*
* @param {String/Number[]} ids
*
* @return Array of Strings
*/
exports.convertIds = function (ids) {
var convertedIds = [];
for (var i = 0; i < ids.length; i++) {
convertedIds.push(ids[i].toString());
}
return convertedIds;
};
/**
* @private
*
* Executes the native counterpart. * Executes the native counterpart.
* *
* @param {String} action * @param {String} action
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment