Wednesday, December 24, 2014

Interactive Notifications in iOS 8


In iOS 8 developers can now present interactive notifications in their apps.

To do so, first you need to register all possible notification “categories”. A category is defined by a set of actions. In my example I’m going to setup a category with reply and delete actions

Each action is shown to the user as a button. Actions have two possible activation modes: foreground or background. If your app can perform the action with no further interaction from the user then you want to use the background activation mode. If the action needs further user input then you can set the activation mode to foreground to have your app launched when actioned, for example, presenting a keyboard in your app when they reply button is tapped.




UIMutableUserNotificationAction* deleteAction = [[UIMutableUserNotificationAction alloc] init];
[deleteAction setIdentifier:@"delete_action_id"];
[deleteAction setTitle:@"Delete"];
[deleteAction setActivationMode:UIUserNotificationActivationModeBackground];
[deleteAction setDestructive:YES];

UIMutableUserNotificationAction* replyAction = [[UIMutableUserNotificationAction alloc] init];
[replyAction setIdentifier:@"reply_action_id"];
[replyAction setTitle:@"Reply"];
[replyAction setActivationMode:UIUserNotificationActivationModeForeground];
[replyAction setDestructive:NO];

UIMutableUserNotificationCategory* deleteReplyCategory = [[UIMutableUserNotificationCategory alloc] init];
[deleteReplyCategory setIdentifier:@"custom_category_id"];
[deleteReplyCategory setActions:@[replyAction, deleteAction] forContext:UIUserNotificationActionContextDefault];


NSSet* categories = [NSSet setWithArray:@[deleteReplyCategory]];
UIUserNotificationSettings* settings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert) categories:categories];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];


Now we can schedule a notification to use the previously setup category. This is the string identifier from before. This notification is scheduled after 10 seconds.

UILocalNotification* notification = [[UILocalNotification alloc] init];
[notification setFireDate:[NSDate dateWithTimeIntervalSinceNow:10]];
[notification setAlertBody:@"Somebody sent you a message"];
[notification setCategory:@"custom_category_id"];
[[UIApplication sharedApplication] scheduleLocalNotification:notification];


When the user selects an option we get a callback to the app delegate.


- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UILocalNotification *)notification completionHandler:(void(^)())completionHandler
{
    if([notification.category isEqualToString:@"custom_category_id"])
    {
        if([identifier isEqualToString:@"delete_action_id"])
        {
            NSLog(@"Delete was pressed");
        }
        else if([identifier isEqualToString:@"reply_action_id"])
        {
            NSLog(@"Reply was pressed");
        }
    }
   
    //    Important to call this when finished
    completionHandler();
}






No comments: