Jelajahi Sumber

Internal changes related with action groups

Sebastián Katzer 7 tahun lalu
induk
melakukan
fc41c7d0e7

+ 3 - 0
plugin.xml

@@ -72,6 +72,9 @@
         <header-file src="src/ios/APPLocalNotification.h" />
         <source-file src="src/ios/APPLocalNotification.m" />
 
+        <header-file src="src/ios/APPNotificationCategory.h" />
+        <source-file src="src/ios/APPNotificationCategory.m" />
+
         <header-file src="src/ios/APPNotificationContent.h" />
         <source-file src="src/ios/APPNotificationContent.m" />
 

+ 4 - 3
src/android/LocalNotification.java

@@ -232,12 +232,13 @@ public class LocalNotification extends CordovaPlugin {
     private void actions (JSONArray args, CallbackContext command) {
         int task        = args.optInt(0);
         String id       = args.optString(1);
-        JSONObject spec = args.optJSONObject(2);
+        JSONArray list  = args.optJSONArray(2);
+        Context context = cordova.getActivity();
 
         switch (task) {
             case 0:
-                ActionGroup group = ActionGroup.parse(cordova.getActivity(), spec);
-                if (group != null) ActionGroup.register(group);
+                ActionGroup group = ActionGroup.parse(context, id, list);
+                ActionGroup.register(group);
                 command.success();
                 break;
             case 1:

+ 1 - 1
src/android/notification/Options.java

@@ -612,7 +612,7 @@ public final class Options {
             group = ActionGroup.lookup(groupId);
         } else
         if (actions != null && actions.length() > 0) {
-            group = ActionGroup.parse(context, options);
+            group = ActionGroup.parse(context, actions);
         }
 
         return (group != null) ? group.getActions() : null;

+ 15 - 18
src/android/notification/action/ActionGroup.java

@@ -37,12 +37,8 @@ import static android.os.Build.VERSION_CODES.N;
 
 public final class ActionGroup {
 
-    // Default action group id
-    private static final String GENERAL_ACTION_GROUP = "DEFAULT_GROUP";
-
     // Saves all groups for later lookup.
-    private static final Map<String, ActionGroup> groups =
-            new HashMap<String, ActionGroup>();
+    private static final Map<String, ActionGroup> groups = new HashMap<String, ActionGroup>();
 
     // The ID of the action group.
     private final String id;
@@ -67,9 +63,7 @@ public final class ActionGroup {
      * @param group The action group to register.
      */
     public static void register (ActionGroup group) {
-        if (!group.getId().equalsIgnoreCase(GENERAL_ACTION_GROUP)) {
-            groups.put(group.getId(), group);
-        }
+        groups.put(group.getId(), group);
     }
 
     /**
@@ -93,17 +87,23 @@ public final class ActionGroup {
     /**
      * Creates an action group by parsing the specified action specs.
      *
-     * @param spec The action group spec containing the id and list of actions.
+     * @param list The list of actions.
      *
      * @return A new action group.
      */
-    public static ActionGroup parse (Context context, JSONObject spec) {
-        String id = spec.optString("actionGroupId", GENERAL_ACTION_GROUP);
-        JSONArray list = spec.optJSONArray("actions");
-
-        if (list == null || list.length() == 0)
-            return null;
+    public static ActionGroup parse (Context context, JSONArray list) {
+        return parse(context, null, list);
+    }
 
+    /**
+     * Creates an action group by parsing the specified action specs.
+     *
+     * @param id   The id for the action group.
+     * @param list The list of actions.
+     *
+     * @return A new action group.
+     */
+    public static ActionGroup parse (Context context, String id, JSONArray list) {
         List<Action> actions = new ArrayList<Action>(list.length());
 
         for (int i = 0; i < list.length(); i++) {
@@ -123,9 +123,6 @@ public final class ActionGroup {
             actions.add(new Action(context, opts));
         }
 
-        if (actions.isEmpty())
-            return null;
-
         return new ActionGroup(id, actions.toArray(new Action[actions.size()]));
     }
 

+ 8 - 9
src/ios/APPLocalNotification.m

@@ -20,10 +20,11 @@
  */
 
 #import "APPLocalNotification.h"
+#import "APPNotificationContent.h"
 #import "APPNotificationOptions.h"
+#import "APPNotificationCategory.h"
 #import "UNUserNotificationCenter+APPLocalNotification.h"
 #import "UNNotificationRequest+APPLocalNotification.h"
-#import "APPNotificationContent.h"
 
 @interface APPLocalNotification ()
 
@@ -398,18 +399,16 @@ UNNotificationPresentationOptions const OptionAlert = UNNotificationPresentation
 - (void) actions:(CDVInvokedUrlCommand *)command
 {
     [self.commandDelegate runInBackground:^{
-        int code              = [command.arguments[0] intValue];
-        NSString* identifier  = [command argumentAtIndex:1];
-        NSDictionary* options = [command argumentAtIndex:2];
-        APPNotificationContent* notification;
+        int code             = [command.arguments[0] intValue];
+        NSString* identifier = [command argumentAtIndex:1];
+        NSArray* actions     = [command argumentAtIndex:2];
+        UNNotificationCategory* group;
         BOOL found;
         
         switch (code) {
             case 0:
-                notification = [[APPNotificationContent alloc]
-                                initWithOptions:options];
-                
-                [_center addActionGroup:notification.category];
+                group = [APPNotificationCategory parse:actions withId:identifier];
+                [_center addActionGroup:group];
                 [self execCallback:command];
                 break;
             case 1:

+ 28 - 0
src/ios/APPNotificationCategory.h

@@ -0,0 +1,28 @@
+/*
+ * Apache 2.0 License
+ *
+ * Copyright (c) Sebastian Katzer 2017
+ *
+ * This file contains Original Code and/or Modifications of Original Code
+ * as defined in and that are subject to the Apache License
+ * Version 2.0 (the 'License'). You may not use this file except in
+ * compliance with the License. Please obtain a copy of the License at
+ * http://opensource.org/licenses/Apache-2.0/ and read it before using this
+ * file.
+ *
+ * The Original Code and all software distributed under the License are
+ * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
+ * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
+ * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
+ * Please see the License for the specific language governing rights and
+ * limitations under the License.
+ */
+
+@import UserNotifications;
+
+@interface APPNotificationCategory : NSObject
+
++ (UNNotificationCategory*) parse:(NSArray*)list withId:(NSString*)groupId;
+
+@end

+ 111 - 0
src/ios/APPNotificationCategory.m

@@ -0,0 +1,111 @@
+/*
+ * Apache 2.0 License
+ *
+ * Copyright (c) Sebastian Katzer 2017
+ *
+ * This file contains Original Code and/or Modifications of Original Code
+ * as defined in and that are subject to the Apache License
+ * Version 2.0 (the 'License'). You may not use this file except in
+ * compliance with the License. Please obtain a copy of the License at
+ * http://opensource.org/licenses/Apache-2.0/ and read it before using this
+ * file.
+ *
+ * The Original Code and all software distributed under the License are
+ * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
+ * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
+ * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
+ * Please see the License for the specific language governing rights and
+ * limitations under the License.
+ */
+
+#import "APPNotificationCategory.h"
+
+@import UserNotifications;
+
+@implementation APPNotificationCategory : NSObject
+
+#pragma mark -
+#pragma mark Public
+
+/**
+ * Parse the provided spec map into an action group.
+ *
+ * @param [ NSDictionary* ] spec A key-value property map.
+ *                               Must contain an id and a list of actions.
+ *
+ * @return [ UNNotificationCategory* ]
+ */
++ (UNNotificationCategory*) parse:(NSArray*)list withId:(NSString*)groupId
+{
+    NSArray* actions = [self parseActions:list];
+
+    return [UNNotificationCategory categoryWithIdentifier:groupId
+                                                  actions:actions
+                                        intentIdentifiers:@[]
+                                                  options:UNNotificationCategoryOptionCustomDismissAction];
+}
+
+#pragma mark -
+#pragma mark Private
+
+/**
+ * The actions of the action group.
+ *
+ * @return [ NSArray* ]
+ */
++ (NSArray<UNNotificationAction *> *) parseActions:(NSArray*)items
+{
+    NSMutableArray* actions = [[NSMutableArray alloc] init];
+    
+    for (NSDictionary* item in items) {
+        NSString* id    = item[@"id"];
+        NSString* title = item[@"title"];
+        NSString* type  = item[@"type"];
+        
+        UNNotificationActionOptions options = UNNotificationActionOptionNone;
+        UNNotificationAction* action;
+        
+        if ([item[@"launch"] boolValue]) {
+            options = UNNotificationActionOptionForeground;
+        }
+        
+        if ([item[@"ui"] isEqualToString:@"decline"]) {
+            options = options | UNNotificationActionOptionDestructive;
+        }
+        
+        if ([item[@"needsAuth"] boolValue]) {
+            options = options | UNNotificationActionOptionAuthenticationRequired;
+        }
+        
+        if ([type isEqualToString:@"input"]) {
+            NSString* submitTitle = item[@"submitTitle"];
+            NSString* placeholder = item[@"emptyText"];
+            
+            if (!submitTitle.length) {
+                submitTitle = @"Submit";
+            }
+            
+            action = [UNTextInputNotificationAction actionWithIdentifier:id
+                                                                   title:title
+                                                                 options:options
+                                                    textInputButtonTitle:submitTitle
+                                                    textInputPlaceholder:placeholder];
+        } else
+            if (!type.length || [type isEqualToString:@"button"]) {
+                action = [UNNotificationAction actionWithIdentifier:id
+                                                              title:title
+                                                            options:options];
+            } else {
+                NSLog(@"Unknown action type: %@", type);
+            }
+        
+        if (action) {
+            [actions addObject:action];
+        }
+    }
+    
+    return actions;
+}
+
+@end

+ 0 - 1
src/ios/APPNotificationContent.h

@@ -28,6 +28,5 @@
 - (id) initWithOptions:(NSDictionary*)dict;
 - (APPNotificationOptions*) options;
 - (UNNotificationRequest*) request;
-- (UNNotificationCategory*) category;
 
 @end

+ 0 - 19
src/ios/APPNotificationContent.m

@@ -104,25 +104,6 @@ static char optionsKey;
                                                 trigger:opts.trigger];
 }
 
-/**
- * The category for the notification with all the actions.
- *
- * @return [ UNNotificationCategory* ]
- */
-- (UNNotificationCategory*) category
-{
-    NSString* categoryId = self.categoryIdentifier;
-    NSArray* actions     = self.options.actions;
-
-    if (!actions.count)
-        return NULL;
-
-    return [UNNotificationCategory categoryWithIdentifier:categoryId
-                                                  actions:actions
-                                        intentIdentifiers:@[]
-                                                  options:UNNotificationCategoryOptionCustomDismissAction];
-}
-
 #pragma mark -
 #pragma mark Private
 

+ 0 - 1
src/ios/APPNotificationOptions.h

@@ -34,7 +34,6 @@
 @property (readonly, getter=priority)      int                  priority;
 @property (readonly, getter=sound)         UNNotificationSound* sound;
 @property (readonly, getter=userInfo)      NSDictionary*        userInfo;
-@property (readonly, getter=actions)       NSArray<UNNotificationAction*>*actions;
 @property (readonly, getter=attachments)   NSArray<UNNotificationAttachment*>*attachments;
 
 - (id) initWithDict:(NSDictionary*) dict;

+ 12 - 80
src/ios/APPNotificationOptions.m

@@ -67,7 +67,7 @@ static NSInteger WEEKDAYS[8] = { 0, 2, 3, 4, 5, 6, 7, 1 };
  */
 - (NSNumber*) id
 {
-    NSInteger id = [[dict objectForKey:@"id"] integerValue];
+    NSInteger id = [dict[@"id"] integerValue];
 
     return [NSNumber numberWithInteger:id];
 }
@@ -89,7 +89,7 @@ static NSInteger WEEKDAYS[8] = { 0, 2, 3, 4, 5, 6, 7, 1 };
  */
 - (NSString*) title
 {
-    return [dict objectForKey:@"title"];
+    return dict[@"title"];
 }
 
 /**
@@ -111,7 +111,7 @@ static NSInteger WEEKDAYS[8] = { 0, 2, 3, 4, 5, 6, 7, 1 };
  */
 - (NSString*) text
 {
-    return [dict objectForKey:@"text"];
+    return dict[@"text"];
 }
 
 /**
@@ -121,7 +121,7 @@ static NSInteger WEEKDAYS[8] = { 0, 2, 3, 4, 5, 6, 7, 1 };
  */
 - (BOOL) silent
 {
-    return [[dict objectForKey:@"silent"] boolValue];
+    return [dict[@"silent"] boolValue];
 }
 
 /**
@@ -131,7 +131,7 @@ static NSInteger WEEKDAYS[8] = { 0, 2, 3, 4, 5, 6, 7, 1 };
  */
 - (int) priority
 {
-    return [[dict objectForKey:@"priority"] intValue];
+    return [dict[@"priority"] intValue];
 }
 
 /**
@@ -141,7 +141,7 @@ static NSInteger WEEKDAYS[8] = { 0, 2, 3, 4, 5, 6, 7, 1 };
  */
 - (NSNumber*) badge
 {
-    id value = [dict objectForKey:@"badge"];
+    id value = dict[@"badge"];
 
     return (value == NULL) ? NULL : [NSNumber numberWithInt:[value intValue]];
 }
@@ -153,14 +153,9 @@ static NSInteger WEEKDAYS[8] = { 0, 2, 3, 4, 5, 6, 7, 1 };
  */
 - (NSString*) actionGroupId
 {
-    id actions = [dict objectForKey:@"actions"];
+    id actions = dict[@"actions"];
     
-    if ([actions isKindOfClass:NSString.class])
-        return actions;
-    
-    NSString* value = [dict objectForKey:@"actionGroupId"];
-
-    return value.length ? value : kAPPGeneralCategory;
+    return ([actions isKindOfClass:NSString.class]) ? actions : kAPPGeneralCategory;
 }
 
 /**
@@ -170,7 +165,7 @@ static NSInteger WEEKDAYS[8] = { 0, 2, 3, 4, 5, 6, 7, 1 };
  */
 - (UNNotificationSound*) sound
 {
-    NSString* path = [dict objectForKey:@"sound"];
+    NSString* path = dict[@"sound"];
     NSString* file;
 
     if ([path isKindOfClass:NSNumber.class]) {
@@ -198,7 +193,7 @@ static NSInteger WEEKDAYS[8] = { 0, 2, 3, 4, 5, 6, 7, 1 };
  */
 - (NSArray<UNNotificationAttachment *> *) attachments
 {
-    NSArray* paths              = [dict objectForKey:@"attachments"];
+    NSArray* paths              = dict[@"attachments"];
     NSMutableArray* attachments = [[NSMutableArray alloc] init];
 
     if (!paths)
@@ -221,69 +216,6 @@ static NSInteger WEEKDAYS[8] = { 0, 2, 3, 4, 5, 6, 7, 1 };
     return attachments;
 }
 
-/**
- * Additional actions for the notification.
- *
- * @return [ NSArray* ]
- */
-- (NSArray<UNNotificationAction *> *) actions
-{
-    NSArray* items          = [dict objectForKey:@"actions"];
-    NSMutableArray* actions = [[NSMutableArray alloc] init];
-
-    if (!items)
-        return actions;
-
-    for (NSDictionary* item in items) {
-        NSString* id    = [item objectForKey:@"id"];
-        NSString* title = [item objectForKey:@"title"];
-        NSString* type  = [item objectForKey:@"type"];
-
-        UNNotificationActionOptions options = UNNotificationActionOptionNone;
-        UNNotificationAction* action;
-
-        if ([[item objectForKey:@"launch"] boolValue]) {
-            options = UNNotificationActionOptionForeground;
-        }
-
-        if ([[item objectForKey:@"ui"] isEqualToString:@"decline"]) {
-            options = options | UNNotificationActionOptionDestructive;
-        }
-
-        if ([[item objectForKey:@"needsAuth"] boolValue]) {
-            options = options | UNNotificationActionOptionAuthenticationRequired;
-        }
-
-        if ([type isEqualToString:@"input"]) {
-            NSString* submitTitle = [item objectForKey:@"submitTitle"];
-            NSString* placeholder = [item objectForKey:@"emptyText"];
-
-            if (!submitTitle.length) {
-                submitTitle = @"Submit";
-            }
-
-            action = [UNTextInputNotificationAction actionWithIdentifier:id
-                                                                   title:title
-                                                                 options:options
-                                                    textInputButtonTitle:submitTitle
-                                                    textInputPlaceholder:placeholder];
-        } else
-        if (!type.length || [type isEqualToString:@"button"]) {
-            action = [UNNotificationAction actionWithIdentifier:id
-                                                          title:title
-                                                        options:options];
-        } else {
-            NSLog(@"Unknown action type: %@", type);
-        }
-
-        if (action) {
-            [actions addObject:action];
-        }
-    }
-
-    return actions;
-}
-
 #pragma mark -
 #pragma mark Public
 
@@ -315,7 +247,7 @@ static NSInteger WEEKDAYS[8] = { 0, 2, 3, 4, 5, 6, 7, 1 };
  */
 - (NSDictionary*) userInfo
 {
-    if ([dict objectForKey:@"updatedAt"]) {
+    if (dict[@"updatedAt"]) {
         NSMutableDictionary* data = [dict mutableCopy];
 
         [data removeObjectForKey:@"updatedAt"];
@@ -331,7 +263,7 @@ static NSInteger WEEKDAYS[8] = { 0, 2, 3, 4, 5, 6, 7, 1 };
 
 - (id) valueForTriggerOption:(NSString*)key
 {
-    return [[dict objectForKey:@"trigger"] objectForKey:key];
+    return dict[@"trigger"][key];
 }
 
 /**

+ 3 - 3
src/windows/LocalNotificationProxy.js

@@ -271,12 +271,12 @@ exports.actions = function (success, error, args) {
         code        = args[0],
         id          = args[1],
         res         = [],
-        opts, group;
+        list, group;
 
     switch (code) {
         case 0:
-            opts  = exports.parseOptions(args[2]);
-            group = new ActionGroup(id, opts.actions);
+            list  = exports.parseActions({ actions:args[2] });
+            group = new ActionGroup(id, list);
 
             ActionGroup.register(group);
             break;

+ 1 - 2
www/local-notification.js

@@ -393,8 +393,7 @@ exports.getTriggered = function (callback, scope) {
  * @return [ Void ]
  */
 exports.addActions = function (id, actions, callback, scope) {
-    var config = { actionGroupId: id, actions: actions };
-    this._exec('actions', [0, id, config], callback, scope);
+    this._exec('actions', [0, id, actions], callback, scope);
 };
 
 /**