How to implement interactive notifications ios8

后端 未结 5 1922
名媛妹妹
名媛妹妹 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 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

提交回复
热议问题