Tuesday, October 28, 2014

iOS 8.0 Registering Notification Actions


To use notification Actions, an app needs to define actions, group them into categories and register them. 
Below is the code to do this 

  UIMutableUserNotificationAction *acceptAction =
            [[UIMutableUserNotificationAction alloc] init];
            
            // Define id string to be passed back to your app when you handle the action
            acceptAction.identifier = @"ACCEPT_IDENTIFIER";
            
            // Localized string displayed to the user
            acceptAction.title = @"Accept";
            
            // If you need to show UI, choose foreground
            acceptAction.activationMode = UIUserNotificationActivationModeBackground;
            
            // Destructive actions display in red
            acceptAction.destructive = NO;
            
            // Does the action require authentication?
            acceptAction.authenticationRequired = NO;


The activationMode property tells whether iOS should launch the app in foreground or background when the user responds to the notification. If the activation mode is set to background UIUserNotificationActivationModeBackground, then the app is given seconds to run. If the destructive property is NO, then the action button appears blue. 
If its YES, it will appear as red. If the authentication required is set to YES, then if the device is locked when user is acting on the notification, 
OS will ask user the passcode. When the activation mode property is activation in foreground  UIUserNotificationActivationModeForeground, the value of the authenticationRequired value will be set to YES. Once the action is defined, they need to be grouped into categories. The entire code for this is like below 

// First create the category
            UIMutableUserNotificationCategory *inviteCategory =
            [[UIMutableUserNotificationCategory alloc] init];
            
            // Identifier to include in your push payload and local notification
            inviteCategory.identifier = @"INVITE_CATEGORY";
            
            // Add the actions to the category and set the action context
            [inviteCategory setActions:@[acceptAction, maybeAction, declineAction]
                            forContext:UIUserNotificationActionContextDefault];
            
            // Set the actions to present in a minimal context
            [inviteCategory setActions:@[acceptAction, declineAction]
                            forContext:UIUserNotificationActionContextMinimal];

  NSSet *categories = [NSSet setWithObjects:inviteCategory, alarmCategory, ...
                                 
                                 UIUserNotificationSettings *settings =
                                 [UIUserNotificationSettings settingsForTypes:types categories:categories];
                                 

                                 [[UIApplication sharedApplication] registerUserNotificationSettings:settings];

References:
https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/IPhoneOSClientImp.html

No comments:

Post a Comment