Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement interactive notifications ios8

I'm creating a timed todo list app in which users can start a timer from a lock screen notification by swiping to reveal a start button. This is a new feature shown in iOS 8 but there is little documentation showing how to implement this feature. Can anyone show how I would go about setting up the 'start' action and the block of code which runs when this is pressed? If the app were closed would this feature still work?

like image 830
dev_cd123 Avatar asked Jul 19 '14 12:07

dev_cd123


People also ask

What is interactive notification?

Interactive Notifications are push notifications that include buttons for secondary, complementary actions, allowing users to interact with notifications from outside of the app. They can drive the user to: Take immediate, specific action.


2 Answers

@Shubhendu thanks for the link. For those of you who don't want to have to sit through the video, here's a quick recap of what you need to do in order to include interactive notifications in you application.

  • Define all the actions that the user may execute from your app's notifications. These actions are created using the UIMutableUserNotificationAction class.

    UIMutableUserNotificationAction *action = [[UIMutableUserNotificationAction alloc] init];
    action.identifier = @"ACTION_ID"; // The id passed when the user selects the action
    action.title = NSLocalizedString(@"Title",nil); // The title displayed for the action
    action.activationMode = UIUserNotificationActivationModeBackground; // Choose whether the application is launched in foreground when the action is clicked
    action.destructive = NO; // If YES, then the action is red
    action.authenticationRequired = NO; // Whether the user must authenticate to execute the action
    
  • Place these actions into categories. Each category defines a group of actions that a user may execute from a notification. These categories are created using UIMutableUserNotificationCategory.

    UIMutableUserNotificationCategory *category = [[UIMutableUserNotificationCategory alloc] init];
    category.identifier = @"CATEGORY_ID"; // Identifier passed in the payload
    [category setActions:@[action] forContext:UIUserNotificationActionContextDefault]; // The context determines the number of actions presented (see documentation) 
    
  • Register the categories in the settings. Note that registering the categories doesn't replace asking for user permission to send remote notifications,using [[UIApplication sharedApplication] registerForRemoteNotifications]

    NSSet *categories = [NSSet setWithObjects:category, nil];
    NSUInteger types = UIUserNotificationTypeNone; // Add badge, sound, or alerts here
    UIUserNotificationSettings *settings = [UIUSerNotificationSettings settingsForTypes:types categories:categories];
    [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
    
  • Send the identifier of the category in the notification payload.

    {
        "aps":{
            "alert":"Here's a notification",
            ...
            "category":"CATEGORY_ID"
        }
    }
    
  • Handle user actions in the app delegate by implementing the UIApplicationDelegate protocol methods: application:handleActionWithIdentifier:forRemoteNotification:completionHandler: for remote notifications application:handleActionWithIdentifier:forLocalNotification:completionHandler: for local notifications

like image 59
Martin Devillers Avatar answered Oct 04 '22 16:10

Martin Devillers


STEP 1:

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];
}

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

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

STEP 3: Use this method

application:handleActionWithIdentifier:forRemoteNotification:completionHand
ler:

[application:handleActionWithIdentifier:forLocalNotification:completionHandler:
 -> For LOCAL 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();
    }
}

1.Refer link: Obj C tutorial

2.Swift Code Tutorial here

like image 45
Ramdhas Avatar answered Oct 04 '22 16:10

Ramdhas