Support for iOS 8 interactive notifications in the app

    One of the innovations of iOS 8 are interactive notifications. Up to four actions can be assigned to such notifications, each of which will be represented by a separate button at the bottom of the banner with the notification text. Thus, the user has additional opportunities to respond to messages without activating (usually) the application. Consider how to add support for such functionality.

    Action definition

    Firstly, it is necessary to determine the actions that can be performed on the notification. This is done using instances of the UIMutableUserNotificationAction class :

    UIMutableUserNotificationAction *action_like = [[UIMutableUserNotificationAction alloc] init];
    action_like.identifier = @"Like"; 
    action_like.title = NSLocalizedString(@"Like",nil); 
    action_like.activationMode = UIUserNotificationActivationModeBackground; 
    action_like.destructive = NO; 
    action_like.authenticationRequired = NO;

    Instances of this class contain the following properties:

    identifier - identifier string of the action. This property will be used in the future to distinguish actions from each other.
    title - the title that will be shown on the button.
    activationMode - determines whether the action can be processed in the background or whether the application must be activated.
    destructive - Determines whether the action is destructive. If so, the button for such an action will be highlighted (usually in red).
    authenticationRequired — Determines whether user authentication is required to complete an action.

    An example of determining the destructive action for which the application will be activated:
    UIMutableUserNotificationAction *action_del = [[UIMutableUserNotificationAction alloc] init];
    action_del.identifier = @"Delete";
    action_del.title = NSLocalizedString(@"Delete",nil);
    action_del.activationMode = UIUserNotificationActivationModeForeground;
    action_del.destructive = YES;
    action_del.authenticationRequired = NO;

    Creation of a category and registration of notification processing parameters

    Secondly, we must create a category for our notifications using an instance of the UIMutableUserNotificationCategory class :
    UIMutableUserNotificationCategory *category= [[UIMutableUserNotificationCategory alloc] init];
    category.identifier = @"my_category"; 
    [category setActions:@[action_like, action_del] forContext:UIUserNotificationActionContextDefault]; 
    NSSet *categories = [NSSet setWithObjects:category, nil];

    The category identifier should be sent as the category key in pyaload 's push notification or category property of the local notification.
    The forContext argument sets the number of valid actions for notification.

    UIUserNotificationSettings *notificationSettings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:categories];
    [[UIApplication sharedApplication] registerUserNotificationSettings:notificationSettings];

    Submission of notification

    By sending a notification of the following form:
              {'alert': 'A sample of interactive notification text',
               'category': 'my_category',
               'some_id': 'my_id'

    (taking into account the restrictions on the size of payload, it makes sense to specify the notification category with a not-so-long identifier; but since this is a training example, my_category is used ), we will receive notifications of the following form.
    On the lock screen:
    and after the shift:
    In the notification area:
    And Notification center:

    Button Press Handling

    To process button clicks, you must define a method
    - application:handleActionWithIdentifier:forLocalNotification:completionHandler:

    to handle local notifications.
    - application:handleActionWithIdentifier:forRemoteNotification:completionHandler:

    to handle push notifications.
    The second argument to these methods is the action identifier, which we previously indicated as:
    action_del.identifier = @"Delete";

    The last argument is a block that must be called after processing the action:
    - (void) application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo completionHandler:(void (^)())completionHandler {
        NSLog(@"NOTIFICATION IDENT: %@ USER INFO: %@", identifier, userInfo);

    Please note that clicking on the ' Like ' button does not activate the application. Unlike clicking on ' Delete '.


    Thus, with a “flick of the wrist” (c), you can add additional functionality to the application, increasing its attractiveness to the user (in any case, try to increase the attractiveness).

    Also popular now: