Simple, interactive iOS notifications

One of the lesser-known features of iOS notifications, is the ability to provide different ways that users can respond. These are called interactive notifications. Instead of simply tapping the notifiation, they can pull down to reveal additional functionality. I found a lack of clear examples online so I thought I would do a post to show you how easy it is to implement. Here is an example.

Let’s get started. There are 3 classes in UIKit which are needed: UIUserNotificationSettings, UIUserNotificationCategory, UIUserNotificationAction and their mutable counterparts.

Instead of simply registering for notification types (sounds, banners, alerts) you can now also register for custom notification categories and actions. Categories describe a custom type of notification that your application sends and contains actions that a user can perform in response. For example you receive a notification that someone followed you on a social network. In response you might want to follow them back or ignore.

Here is a very simple example, in Objective-C, of how to register a notification that has 2 actions.

NSString * const NotificationCategoryIdent  = @"ACTIONABLE";
NSString * const NotificationActionOneIdent = @"ACTION_ONE";
NSString * const NotificationActionTwoIdent = @"ACTION_TWO";
 
- (void)registerForNotification {
 
    UIMutableUserNotificationAction *action1;
    action1 = [[UIMutableUserNotificationAction alloc] init];
    [action1 setActivationMode:UIUserNotificationActivationModeBackground];
    [action1 setTitle:@"Action 1"];
    [action1 setIdentifier:NotificationActionOneIdent];
    [action1 setDestructive:NO];
    [action1 setAuthenticationRequired:NO];
 
    UIMutableUserNotificationAction *action2;
    action2 = [[UIMutableUserNotificationAction alloc] init];
    [action2 setActivationMode:UIUserNotificationActivationModeBackground];
    [action2 setTitle:@"Action 2"];
    [action2 setIdentifier:NotificationActionTwoIdent];
    [action2 setDestructive:NO];
    [action2 setAuthenticationRequired:NO];
 
    UIMutableUserNotificationCategory *actionCategory;
    actionCategory = [[UIMutableUserNotificationCategory alloc] init];
    [actionCategory setIdentifier:NotificationCategoryIdent];
    [actionCategory setActions:@[action1, action2] 
                    forContext:UIUserNotificationActionContextDefault];
 
    NSSet *categories = [NSSet setWithObject:actionCategory];
    UIUserNotificationType types = (UIUserNotificationTypeAlert|
                                    UIUserNotificationTypeSound|
                                    UIUserNotificationTypeBadge);
 
    UIUserNotificationSettings *settings;
    settings = [UIUserNotificationSettings settingsForTypes:types
                                                 categories:categories];
 
    [[UIApplication sharedApplication] 
     registerUserNotificationSettings:settings];
}

To send this type of notification simply add the category to the payload.

"aps" : { 
    "alert"    : "Pull down to interact.",
    "category" : "ACTIONABLE"
}

Now to handle the actions the user selects, implement the following in your UIApplicationDelegate.

- (void)application:(UIApplication *)application 
handleActionWithIdentifier:(NSString *)identifier 
forRemoteNotification:(NSDictionary *)userInfo 
completionHandler:(void (^)())completionHandler {
 
    if ([identifier isEqualToString:NotificationActionOneIdent]) {
 
        NSLog(@"You chose action 1.");
    }
    else if ([identifier isEqualToString:NotificationActionTwoIdent]) {   
 
        NSLog(@"You chose action 2.");
    }    
    if (completionHandler) {
 
        completionHandler();
    }
}

The above method will be called in the background when the user selects an action from your notification. Check the identifier to determine which action was selected and finally be sure to call the completionHandler, as per the docs.

That’s it! Hope this helps you get you started with interactive notifications.