Thursday 6 November 2014

Simple, interactive notifications in iOS 8

With iOS 8 came an exciting new API for creating interactive notifications. These allow you to provide additional functionality to your users outside of your application. 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 new classes in iOS 8 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, there are 2 new methods on the UIApplicationDelegate protocol:
application:handleActionWithIdentifier:forLocalNotification:completionHandler:
application:handleActionWithIdentifier:forRemoteNotification:completionHandler:
These methods will get called, in the background, when the user selects an action from your push notification.
- (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();
    }
}
Switch on the identifier to determine which action was selected and finally be sure to call the completionHandler, as per the docs. That’s it! This is a very simple example and only touches on the surface of what iOS 8 notifications can do. In a future article I will dive deeper. Enjoy.

No comments:

Post a Comment