How to implement interactive notifications ios8

后端 未结 5 1867
名媛妹妹
名媛妹妹 2020-12-24 14:26

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

相关标签:
5条回答
  • 2020-12-24 14:57

    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.

    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.

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

    //JSON 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();
        }
    }
    
    0 讨论(0)
  • 2020-12-24 15:05

    In order to know more about interactive notifications- a new feature in iOS 8 go to the below link

    https://developer.apple.com/videos/wwdc/2014/

    and then go to the section "What's New in iOS Notifications"

    0 讨论(0)
  • 2020-12-24 15:07

    @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

    0 讨论(0)
  • 2020-12-24 15:07

    For ios10 Actionable push use this

     UNAuthorizationOptions authOptions =
    UNAuthorizationOptionAlert
    | UNAuthorizationOptionSound
        | UNAuthorizationOptionBadge;
        [[UNUserNotificationCenter currentNotificationCenter]
         requestAuthorizationWithOptions:authOptions
         completionHandler:^(BOOL granted, NSError * _Nullable error) {
         }
         ];
    UNNotificationAction *ok = [UNNotificationAction actionWithIdentifier:@"OK"
                                                                               title:NSLocalizedString(@"OK", nil)
                                                                             options:UNNotificationActionOptionForeground];
        UNNotificationAction *cancel = [UNNotificationAction actionWithIdentifier:@"CANCEL"
                                                                                   title:NSLocalizedString(@"CANCEL", nil)
                                                                                 options:UNNotificationActionOptionForeground];
        NSArray *buttons = @[ ok, cancel ];
    
        // create a category for message failed
        UNNotificationCategory *buttonsAction = [UNNotificationCategory categoryWithIdentifier:@"ACTION_BUTTON"
                                                                                        actions:buttons
                                                                              intentIdentifiers:@[]
                                                                                        options:UNNotificationCategoryOptionCustomDismissAction];
    NSSet *categories = [NSSet setWithObjects:buttonsAction, nil];
    
        // registration
        [[UNUserNotificationCenter currentNotificationCenter] setNotificationCategories:categories];
    
    0 讨论(0)
  • 2020-12-24 15:14

    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

    0 讨论(0)
提交回复
热议问题