Commit 5759483e by Sebastián Katzer

Disable support for implicit action group decleration

parent c008cc68
...@@ -131,17 +131,16 @@ public class LocalNotification extends CordovaPlugin { ...@@ -131,17 +131,16 @@ public class LocalNotification extends CordovaPlugin {
if (action.equals("ready")) { if (action.equals("ready")) {
deviceready(); deviceready();
} else } else
if (action.equalsIgnoreCase("check")) { if (action.equals("check")) {
check(command); check(command);
} else } else
if (action.equalsIgnoreCase("request")) { if (action.equals("request")) {
request(command); request(command);
} else } else
if (action.equalsIgnoreCase("actions")) { if (action.equals("actions")) {
actions(args.optJSONObject(0)); actions(args, command);
command.success();
} else } else
if (action.equalsIgnoreCase("schedule")) { if (action.equals("schedule")) {
schedule(args); schedule(args);
check(command); check(command);
} else } else
...@@ -228,9 +227,7 @@ public class LocalNotification extends CordovaPlugin { ...@@ -228,9 +227,7 @@ public class LocalNotification extends CordovaPlugin {
*/ */
private void check (CallbackContext command) { private void check (CallbackContext command) {
boolean allowed = getNotMgr().hasPermission(); boolean allowed = getNotMgr().hasPermission();
PluginResult result = new PluginResult(PluginResult.Status.OK, allowed); success(command, allowed);
command.sendPluginResult(result);
} }
/** /**
...@@ -246,13 +243,41 @@ public class LocalNotification extends CordovaPlugin { ...@@ -246,13 +243,41 @@ public class LocalNotification extends CordovaPlugin {
/** /**
* Register action group. * Register action group.
* *
* @param args The action group spec. * @param args The exec() arguments in JSON form.
* @param command The callback context used when calling back into
* JavaScript.
*/ */
private void actions (JSONObject args) { private void actions (JSONArray args, CallbackContext command) {
ActionGroup group = ActionGroup.parse(cordova.getActivity(), args); int task = args.optInt(0);
ActionGroup group;
JSONObject spec;
boolean found;
String id;
switch (task) {
case 1:
spec = args.optJSONObject(1);
group = ActionGroup.parse(cordova.getActivity(), spec);
if (group != null) ActionGroup.register(group);
command.success();
break;
case -1:
id = args.optString(1);
ActionGroup.unregister(id);
command.success();
break;
case 0:
id = args.optString(1);
found = ActionGroup.isRegistered(id);
success(command, found);
if (group != null) { break;
ActionGroup.register(group);
} }
} }
...@@ -485,6 +510,18 @@ public class LocalNotification extends CordovaPlugin { ...@@ -485,6 +510,18 @@ public class LocalNotification extends CordovaPlugin {
} }
/** /**
* Invoke success callback with a single boolean argument.
*
* @param command The callback context used when calling back into
* JavaScript.
* @param arg The single argument to pass through.
*/
private void success(CallbackContext command, boolean arg) {
PluginResult result = new PluginResult(PluginResult.Status.OK, arg);
command.sendPluginResult(result);
}
/**
* Fire given event on JS side. Does inform all event listeners. * Fire given event on JS side. Does inform all event listeners.
* *
* @param event The event name. * @param event The event name.
......
...@@ -596,24 +596,26 @@ public final class Options { ...@@ -596,24 +596,26 @@ public final class Options {
* Gets the list of actions to display. * Gets the list of actions to display.
*/ */
Action[] getActions() { Action[] getActions() {
String groupId = options.optString("actionGroupId", null); Object value = options.opt("actions");
JSONArray actions = options.optJSONArray("actions"); String groupId = null;
JSONArray actions = null;
ActionGroup group = null; ActionGroup group = null;
if (actions != null && actions.length() > 0) { if (value instanceof String) {
group = ActionGroup.parse(context, options); groupId = (String) value;
} else
if (value instanceof JSONArray) {
actions = (JSONArray) value;
} }
if (group == null && groupId != null) { if (groupId != null) {
group = ActionGroup.lookup(groupId); group = ActionGroup.lookup(groupId);
} else
if (actions != null && actions.length() > 0) {
group = ActionGroup.parse(context, options);
} }
if (group != null) { return (group != null) ? group.getActions() : null;
ActionGroup.register(group);
return group.getActions();
}
return null;
} }
/** /**
......
...@@ -73,6 +73,24 @@ public final class ActionGroup { ...@@ -73,6 +73,24 @@ public final class ActionGroup {
} }
/** /**
* Unregister the action group.
*
* @param id The id of the action group to remove.
*/
public static void unregister (String id) {
groups.remove(id);
}
/**
* Check if a action group with that id is registered.
*
* @param id The id of the action group to check for.
*/
public static boolean isRegistered (String id) {
return groups.containsKey(id);
}
/**
* Creates an action group by parsing the specified action specs. * Creates an action group by parsing the specified action specs.
* *
* @param spec The action group spec containing the id and list of actions. * @param spec The action group spec containing the id and list of actions.
......
...@@ -176,7 +176,7 @@ UNNotificationPresentationOptions const OptionAlert = UNNotificationPresentation ...@@ -176,7 +176,7 @@ UNNotificationPresentationOptions const OptionAlert = UNNotificationPresentation
- (void) clearAll:(CDVInvokedUrlCommand*)command - (void) clearAll:(CDVInvokedUrlCommand*)command
{ {
[self.commandDelegate runInBackground:^{ [self.commandDelegate runInBackground:^{
[_center clearAllNotifications]; [_center clearNotifications];
[self clearApplicationIconBadgeNumber]; [self clearApplicationIconBadgeNumber];
[self fireEvent:@"clearall"]; [self fireEvent:@"clearall"];
[self execCallback:command]; [self execCallback:command];
...@@ -217,7 +217,7 @@ UNNotificationPresentationOptions const OptionAlert = UNNotificationPresentation ...@@ -217,7 +217,7 @@ UNNotificationPresentationOptions const OptionAlert = UNNotificationPresentation
- (void) cancelAll:(CDVInvokedUrlCommand*)command - (void) cancelAll:(CDVInvokedUrlCommand*)command
{ {
[self.commandDelegate runInBackground:^{ [self.commandDelegate runInBackground:^{
[_center cancelAllNotifications]; [_center cancelNotifications];
[self clearApplicationIconBadgeNumber]; [self clearApplicationIconBadgeNumber];
[self fireEvent:@"cancelall"]; [self fireEvent:@"cancelall"];
[self execCallback:command]; [self execCallback:command];
...@@ -411,12 +411,7 @@ UNNotificationPresentationOptions const OptionAlert = UNNotificationPresentation ...@@ -411,12 +411,7 @@ UNNotificationPresentationOptions const OptionAlert = UNNotificationPresentation
BOOL enabled = settings.notificationCenterSetting == UNNotificationSettingEnabled; BOOL enabled = settings.notificationCenterSetting == UNNotificationSettingEnabled;
BOOL permitted = authorized && enabled; BOOL permitted = authorized && enabled;
CDVPluginResult* result; [self execCallback:command arg:permitted];
result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK
messageAsBool:permitted];
[self.commandDelegate sendPluginResult:result
callbackId:command.callbackId];
}]; }];
} }
...@@ -443,14 +438,37 @@ UNNotificationPresentationOptions const OptionAlert = UNNotificationPresentation ...@@ -443,14 +438,37 @@ UNNotificationPresentationOptions const OptionAlert = UNNotificationPresentation
- (void) actions:(CDVInvokedUrlCommand *)command - (void) actions:(CDVInvokedUrlCommand *)command
{ {
[self.commandDelegate runInBackground:^{ [self.commandDelegate runInBackground:^{
NSDictionary* options = command.arguments[0]; int task = [command.arguments[0] intValue];
APPNotificationContent* notification; APPNotificationContent* notification;
NSDictionary* options;
NSString* identifier;
BOOL found;
switch (task) {
case 1:
options = command.arguments[1];
notification = [[APPNotificationContent alloc] notification = [[APPNotificationContent alloc]
initWithOptions:options]; initWithOptions:options];
[_center addNotificationCategory:notification.category]; [_center addActionGroup:notification.category];
[self execCallback:command];
break;
case -1:
identifier = command.arguments[1];
[_center removeActionGroup:identifier];
[self execCallback:command]; [self execCallback:command];
break;
case 0:
identifier = command.arguments[1];
found = [_center hasActionGroup:identifier];
[self execCallback:command arg:found];
break;
}
}]; }];
} }
...@@ -470,8 +488,6 @@ UNNotificationPresentationOptions const OptionAlert = UNNotificationPresentation ...@@ -470,8 +488,6 @@ UNNotificationPresentationOptions const OptionAlert = UNNotificationPresentation
UNNotificationRequest* request = notification.request; UNNotificationRequest* request = notification.request;
NSString* event = [request wasUpdated] ? @"update" : @"add"; NSString* event = [request wasUpdated] ? @"update" : @"add";
[_center addNotificationCategory:notification.category];
[_center addNotificationRequest:request withCompletionHandler:^(NSError* e) { [_center addNotificationRequest:request withCompletionHandler:^(NSError* e) {
__strong APPLocalNotification* strongSelf = weakSelf; __strong APPLocalNotification* strongSelf = weakSelf;
[strongSelf fireEvent:event notification:request]; [strongSelf fireEvent:event notification:request];
...@@ -642,6 +658,21 @@ UNNotificationPresentationOptions const OptionAlert = UNNotificationPresentation ...@@ -642,6 +658,21 @@ UNNotificationPresentationOptions const OptionAlert = UNNotificationPresentation
} }
/** /**
* Invokes the callback with a single boolean parameter.
*
* @return [ Void ]
*/
- (void) execCallback:(CDVInvokedUrlCommand*)command arg:(BOOL)arg
{
CDVPluginResult *result = [CDVPluginResult
resultWithStatus:CDVCommandStatus_OK
messageAsBool:arg];
[self.commandDelegate sendPluginResult:result
callbackId:command.callbackId];
}
/**
* Fire general event. * Fire general event.
* *
* @param [ NSString* ] event The name of the event to fire. * @param [ NSString* ] event The name of the event to fire.
......
...@@ -64,7 +64,7 @@ static char optionsKey; ...@@ -64,7 +64,7 @@ static char optionsKey;
self.sound = options.sound; self.sound = options.sound;
self.badge = options.badge; self.badge = options.badge;
self.attachments = options.attachments; self.attachments = options.attachments;
self.categoryIdentifier = options.categoryId; self.categoryIdentifier = options.actionGroupId;
} }
#pragma mark - #pragma mark -
......
...@@ -25,7 +25,7 @@ ...@@ -25,7 +25,7 @@
@property (readonly, getter=id) NSNumber* id; @property (readonly, getter=id) NSNumber* id;
@property (readonly, getter=identifier) NSString* identifier; @property (readonly, getter=identifier) NSString* identifier;
@property (readonly, getter=categoryId) NSString* categoryId; @property (readonly, getter=actionGroupId) NSString* actionGroupId;
@property (readonly, getter=title) NSString* title; @property (readonly, getter=title) NSString* title;
@property (readonly, getter=subtitle) NSString* subtitle; @property (readonly, getter=subtitle) NSString* subtitle;
@property (readonly, getter=badge) NSNumber* badge; @property (readonly, getter=badge) NSNumber* badge;
...@@ -34,10 +34,10 @@ ...@@ -34,10 +34,10 @@
@property (readonly, getter=priority) int priority; @property (readonly, getter=priority) int priority;
@property (readonly, getter=sound) UNNotificationSound* sound; @property (readonly, getter=sound) UNNotificationSound* sound;
@property (readonly, getter=userInfo) NSDictionary* userInfo; @property (readonly, getter=userInfo) NSDictionary* userInfo;
@property (readonly, getter=actions) NSArray<UNNotificationAction *> * actions; @property (readonly, getter=actions) NSArray<UNNotificationAction*>*actions;
@property (readonly, getter=attachments) NSArray<UNNotificationAttachment *> * attachments; @property (readonly, getter=attachments) NSArray<UNNotificationAttachment*>*attachments;
- (id) initWithDict:(NSDictionary*)dict; - (id) initWithDict:(NSDictionary*) dict;
- (UNNotificationTrigger*) trigger; - (UNNotificationTrigger*) trigger;
@end @end
...@@ -52,11 +52,8 @@ static NSInteger WEEKDAYS[8] = { 0, 2, 3, 4, 5, 6, 7, 1 }; ...@@ -52,11 +52,8 @@ static NSInteger WEEKDAYS[8] = { 0, 2, 3, 4, 5, 6, 7, 1 };
- (id) initWithDict:(NSDictionary*)dictionary - (id) initWithDict:(NSDictionary*)dictionary
{ {
self = [self init]; self = [self init];
self.dict = dictionary; self.dict = dictionary;
[self actions];
return self; return self;
} }
...@@ -154,8 +151,13 @@ static NSInteger WEEKDAYS[8] = { 0, 2, 3, 4, 5, 6, 7, 1 }; ...@@ -154,8 +151,13 @@ static NSInteger WEEKDAYS[8] = { 0, 2, 3, 4, 5, 6, 7, 1 };
* *
* @return [ NSString* ] * @return [ NSString* ]
*/ */
- (NSString*) categoryId - (NSString*) actionGroupId
{ {
id actions = [dict objectForKey:@"actions"];
if ([actions isKindOfClass:NSString.class])
return actions;
NSString* value = [dict objectForKey:@"actionGroupId"]; NSString* value = [dict objectForKey:@"actionGroupId"];
return value.length ? value : kAPPGeneralCategory; return value.length ? value : kAPPGeneralCategory;
......
...@@ -25,11 +25,8 @@ ...@@ -25,11 +25,8 @@
@interface UNNotificationRequest (APPLocalNotification) @interface UNNotificationRequest (APPLocalNotification)
// The options provided by the plug-in
- (APPNotificationOptions*) options; - (APPNotificationOptions*) options;
// If the notification was updated
- (BOOL) wasUpdated; - (BOOL) wasUpdated;
// Encode the user info dict to JSON
- (NSString*) encodeToJSON; - (NSString*) encodeToJSON;
@end @end
...@@ -37,34 +37,24 @@ typedef NS_ENUM(NSUInteger, APPNotificationType) { ...@@ -37,34 +37,24 @@ typedef NS_ENUM(NSUInteger, APPNotificationType) {
@property (readonly, getter=getNotifications) NSArray* localNotifications; @property (readonly, getter=getNotifications) NSArray* localNotifications;
@property (readonly, getter=getNotificationIds) NSArray* localNotificationIds; @property (readonly, getter=getNotificationIds) NSArray* localNotificationIds;
// Register general notification category to listen for dismiss actions
- (void) registerGeneralNotificationCategory; - (void) registerGeneralNotificationCategory;
// Add the specified category to the list of categories - (void) addActionGroup:(UNNotificationCategory*)category;
- (void) addNotificationCategory:(UNNotificationCategory*)category; - (void) removeActionGroup:(NSString*)identifier;
- (BOOL) hasActionGroup:(NSString*)identifier;
// List of all notification IDs from given type
- (NSArray*) getNotificationIdsByType:(APPNotificationType)type; - (NSArray*) getNotificationIdsByType:(APPNotificationType)type;
// Find notification by ID
- (UNNotificationRequest*) getNotificationWithId:(NSNumber*)id; - (UNNotificationRequest*) getNotificationWithId:(NSNumber*)id;
// Find notification type by ID
- (APPNotificationType) getTypeOfNotificationWithId:(NSNumber*)id; - (APPNotificationType) getTypeOfNotificationWithId:(NSNumber*)id;
// Property list from all local notifications
- (NSArray*) getNotificationOptions; - (NSArray*) getNotificationOptions;
// Property list from given local notifications
- (NSArray*) getNotificationOptionsById:(NSArray*)ids; - (NSArray*) getNotificationOptionsById:(NSArray*)ids;
// Property list from all local notifications with type constraint
- (NSArray*) getNotificationOptionsByType:(APPNotificationType)type; - (NSArray*) getNotificationOptionsByType:(APPNotificationType)type;
// Clear specified notfication
- (void) clearNotification:(UNNotificationRequest*)notification; - (void) clearNotification:(UNNotificationRequest*)notification;
// Clear all notfications - (void) clearNotifications;
- (void) clearAllNotifications;
// Cancel specified notfication
- (void) cancelNotification:(UNNotificationRequest*)notification; - (void) cancelNotification:(UNNotificationRequest*)notification;
// Cancel all notfications - (void) cancelNotifications;
- (void) cancelAllNotifications;
@end @end
...@@ -56,7 +56,7 @@ NSString * const kAPPGeneralCategory = @"GENERAL"; ...@@ -56,7 +56,7 @@ NSString * const kAPPGeneralCategory = @"GENERAL";
* *
* @return [ Void ] * @return [ Void ]
*/ */
- (void) addNotificationCategory:(UNNotificationCategory*)category - (void) addActionGroup:(UNNotificationCategory*)category
{ {
if (!category) if (!category)
return; return;
...@@ -64,7 +64,8 @@ NSString * const kAPPGeneralCategory = @"GENERAL"; ...@@ -64,7 +64,8 @@ NSString * const kAPPGeneralCategory = @"GENERAL";
[self getNotificationCategoriesWithCompletionHandler:^(NSSet<UNNotificationCategory *> *set) { [self getNotificationCategoriesWithCompletionHandler:^(NSSet<UNNotificationCategory *> *set) {
NSMutableSet* categories = [NSMutableSet setWithSet:set]; NSMutableSet* categories = [NSMutableSet setWithSet:set];
for (UNNotificationCategory* item in categories) { for (UNNotificationCategory* item in categories)
{
if ([category.identifier isEqualToString:item.identifier]) { if ([category.identifier isEqualToString:item.identifier]) {
[categories removeObject:item]; [categories removeObject:item];
break; break;
...@@ -76,6 +77,58 @@ NSString * const kAPPGeneralCategory = @"GENERAL"; ...@@ -76,6 +77,58 @@ NSString * const kAPPGeneralCategory = @"GENERAL";
}]; }];
} }
/**
* Remove if the specified category does exist.
*
* @param [ NSString* ] identifier The category id to remove.
*
* @return [ Void ]
*/
- (void) removeActionGroup:(NSString*)identifier
{
[self getNotificationCategoriesWithCompletionHandler:^(NSSet<UNNotificationCategory *> *set) {
NSMutableSet* categories = [NSMutableSet setWithSet:set];
for (UNNotificationCategory* item in categories)
{
if ([item.identifier isEqualToString:identifier]) {
[categories removeObject:item];
break;
}
}
[self setNotificationCategories:categories];
}];
}
/**
* Check if the specified category does exist.
*
* @param [ NSString* ] identifier The category id to check for.
*
* @return [ Void ]
*/
- (BOOL) hasActionGroup:(NSString*)identifier
{
dispatch_semaphore_t sema = dispatch_semaphore_create(0);
__block BOOL found = NO;
[self getNotificationCategoriesWithCompletionHandler:^(NSSet<UNNotificationCategory *> *items) {
for (UNNotificationCategory* item in items)
{
if ([item.identifier isEqualToString:identifier]) {
found = YES;
dispatch_semaphore_signal(sema);
break;
}
}
}];
dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
return found;
}
#pragma mark - #pragma mark -
#pragma mark LocalNotifications #pragma mark LocalNotifications
...@@ -101,7 +154,8 @@ NSString * const kAPPGeneralCategory = @"GENERAL"; ...@@ -101,7 +154,8 @@ NSString * const kAPPGeneralCategory = @"GENERAL";
dispatch_semaphore_t sema = dispatch_semaphore_create(0); dispatch_semaphore_t sema = dispatch_semaphore_create(0);
[self getDeliveredNotificationsWithCompletionHandler:^(NSArray<UNNotification *> *delivered) { [self getDeliveredNotificationsWithCompletionHandler:^(NSArray<UNNotification *> *delivered) {
for (UNNotification* notification in delivered) { for (UNNotification* notification in delivered)
{
[notifications addObject:notification.request]; [notifications addObject:notification.request];
} }
dispatch_semaphore_signal(sema); dispatch_semaphore_signal(sema);
...@@ -276,7 +330,7 @@ NSString * const kAPPGeneralCategory = @"GENERAL"; ...@@ -276,7 +330,7 @@ NSString * const kAPPGeneralCategory = @"GENERAL";
/* /*
* Clear all notfications. * Clear all notfications.
*/ */
- (void) clearAllNotifications - (void) clearNotifications
{ {
[self removeAllDeliveredNotifications]; [self removeAllDeliveredNotifications];
} }
...@@ -298,7 +352,7 @@ NSString * const kAPPGeneralCategory = @"GENERAL"; ...@@ -298,7 +352,7 @@ NSString * const kAPPGeneralCategory = @"GENERAL";
/* /*
* Cancel all notfications. * Cancel all notfications.
*/ */
- (void) cancelAllNotifications - (void) cancelNotifications
{ {
[self removeAllPendingNotificationRequests]; [self removeAllPendingNotificationRequests];
[self removeAllDeliveredNotifications]; [self removeAllDeliveredNotifications];
......
...@@ -24,7 +24,6 @@ var exec = require('cordova/exec'), ...@@ -24,7 +24,6 @@ var exec = require('cordova/exec'),
// Defaults // Defaults
exports._defaults = { exports._defaults = {
actionGroupId : null,
actions : [], actions : [],
attachments : [], attachments : [],
autoClear : true, autoClear : true,
...@@ -384,7 +383,7 @@ exports.getTriggered = function (callback, scope) { ...@@ -384,7 +383,7 @@ exports.getTriggered = function (callback, scope) {
}; };
/** /**
* Register an group of actions by id. * Add an group of actions by id.
* *
* @param [ String ] id The Id of the group. * @param [ String ] id The Id of the group.
* @param [ Array] actions The action config settings. * @param [ Array] actions The action config settings.
...@@ -393,10 +392,35 @@ exports.getTriggered = function (callback, scope) { ...@@ -393,10 +392,35 @@ exports.getTriggered = function (callback, scope) {
* *
* @return [ Void ] * @return [ Void ]
*/ */
exports.addActionGroup = function (id, actions, callback, scope) { exports.addActions = function (id, actions, callback, scope) {
var config = { actionGroupId: id, actions: actions }; var config = { actionGroupId: id, actions: actions };
this._exec('actions', [1, config], callback, scope);
};
this._exec('actions', config, callback, scope); /**
* Remove an group of actions by id.
*
* @param [ String ] id The Id of the group.
* @param [ Function ] callback The function to be exec as the callback.
* @param [ Object ] scope The callback function's scope.
*
* @return [ Void ]
*/
exports.removeActions = function (id, callback, scope) {
this._exec('actions', [-1, id], callback, scope);
};
/**
* Check if a group of actions is defined.
*
* @param [ String ] id The Id of the group.
* @param [ Function ] callback The function to be exec as the callback.
* @param [ Object ] scope The callback function's scope.
*
* @return [ Void ]
*/
exports.hasActions = function (id, callback, scope) {
this._exec('actions', [0, id], callback, scope);
}; };
/** /**
...@@ -651,8 +675,8 @@ exports._convertPriority = function (options) { ...@@ -651,8 +675,8 @@ exports._convertPriority = function (options) {
exports._convertActions = function (options) { exports._convertActions = function (options) {
var actions = []; var actions = [];
if (!options.actions) if (!options.actions || typeof options.actions === 'string')
return null; return options;
for (var i = 0, len = options.actions.length; i < len; i++) { for (var i = 0, len = options.actions.length; i < len; i++) {
var action = options.actions[i]; var action = options.actions[i];
......
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