Overview

Skill Level: Intermediate

This article provides notification service dynamic action categories and static action categories information.

Prerequisites

Not what you’re looking for? Check out all our available tutorials for mobile app messaging here.

 

 

Step-by-step

  1. Notification Service Dynamic Action Categories

    In iOS 10, Apple introduced the notification service extension that allows applications to intercept and pre-process remote notifications before they are presented to the user.

    For information about media attachments and dynamic action categories, see Enabling media attachment and multiple action support.

    Dynamic action categories are sets of actions in mobile app messages that are dynamically generated by the SDK in real time when a special payload is received, if the device is running iOS 10 (or later). Dynamic action categories are not predefined in the application binary.

    Note: Dynamic action categories are not supported in Xamarin for iOS.

    Developers define actions for dynamic action categories in the “category-actions” section of the payload. The payload includes the category-actions key, as well as a mutable-content flag in the normal aps payload. It can also include a category-name key if the name of the dynamic category must match a specific identifier. The category-name key is sometimes used for dynamic notification presentation.

    • The mutable-content flag allows the application’s notification service to process the push message before it is delivered to the user.
    • The category-actions key includes a list of action buttons that are assigned to a temporary category at run time.
    • The aps key includes the text, sound, and badge of the notification.
    • After the temporary category is created, the SDK assigns the category name to the notification and passes the category name to the operating system so that it can be displayed to the user.
    • The category-name key overrides the random category name that is generated by the SDK with a category name that you specify. Add this key to your payload when you want to use iOS 10 (and later) features that require a predefined category name.

    Tip: Always include a notification-action value so a default action can be executed when the notification is opened. Category actions only open if a user expands the push message and selects an alternative action button.

    {
    "category-actions": [ { "name": "IBM_CA", "type": "url", "value": "https://ibm.ca" },
    { "name": "IBM", "type": "url", "value": "https://ibm.com"} ],
    "notification-action": { "type": "url", "value": "https://ibm.com" },
    "aps": { "alert": "Message", "sound": "default", "mutable-content": 1 },
    "category-name": "content-extension"
    }
    
  2. Static action categories

    Because all application delegate methods are forwarded to your defined appDelegateClass, you can implement any standard application delegate methods, including handling static action categories, in any way you want. Static action categories are compiled into the app binary itself. They are sets of predefined buttons that have predefined actions behind them. Those are entirely implemented by the developer and do not interact with the SDK at all.

    Note: Static action categories are not supported in Xamarin for iOS.

    If you are using simple notifications and want to create static action categories with background actions, you must turn on Remote notifications in your XCode project. Go to the Background Modes section of the Capabilities tab for your target, and select Remote notifications.

    • Implement the standard method for handling the category callbacks in your AppDelegate class.

    #pragma mark Process Static Category
    - (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier
    forRemoteNotification:(NSDictionary *)userInfo completionHandler:(void (^)())completionHandler
    {
    NSLog(@"%@ button clicked", identifier);
    completionHandler();
    }
    
    • Register the categories that you want to handle in the APNS registration call in your AppDelegate.

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    if ([application respondsToSelector:@selector(registerUserNotificationSettings:)])
    {
    UIMutableUserNotificationAction* acceptAction = [[UIMutableUserNotificationAction alloc] init];
    acceptAction.identifier = @"Accept";
    acceptAction.title = @"Accept";
    acceptAction.activationMode = UIUserNotificationActivationModeForeground;
    acceptAction.destructive = false;
    acceptAction.authenticationRequire = false;
    
    UIMutableUserNotificationAction* rejectAction = [[UIMutableUserNotification alloc] init];
    rejectAction.identifier = @"Reject";
    rejectAction.title = @"Reject";
    rejectAction.activationMode = UIUserNotificationActivationModeForeground;
    rejectAction.destructive = false;
    rejectAction.authenticationRequired = false;
    
    UIMutableUserNotificationCategory* category = [UIMutableUserNotificationCategory alloc] init];
    category.identifier = @"example";
    [category setActions:@[acceptAction, rejectAction] forContext: UIUserNotificationActionContextDefault];
    [category setActions:@[acceptAction, rejectAction] forContext: UIUserNotificationActionContextMinimal];
    
    UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationType Badge |
    UIUserNotificationTypeSound |
    UIUserNotificationTypeAlert categories: [NSSet setWithObject: category ] ];
    [application registerUserNotificationSettings:settings];
    [application registerForRemoteNotification];
    }
    }
    

Expected outcome

Need more help? Check out all of our available tutorials for mobile app messaging here.

 

Join The Discussion

Your email address will not be published. Required fields are marked *