objective-c - ios8如何实现交互式通知

标签 objective-c xcode ios8

我正在创建一个定时待办事项列表应用程序,用户可以在该应用程序中通过滑动显示开始按钮从锁定屏幕通知启动计时器。这是 iOS 8 中显示的一项新功能,但很少有文档显示如何实现此功能。 任何人都可以展示我将如何设置“开始”操作和按下它时运行的代码块吗?如果应用关闭,此功能是否仍然有效?

最佳答案

@Shubhendu 感谢您的链接。对于那些不想观看视频的人,这里简要回顾一下为了在您的应用程序中包含交互式通知需要执行的操作。

  • 定义用户可以从您的应用通知中执行的所有操作。这些操作是使用 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
    
  • 将这些操作分类。每个类别定义了用户可以从通知中执行的一组操作。这些类别是使用 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) 
    
  • 在设置中注册类别。请注意,注册类别并不能代替使用 [[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];
    
  • 在通知负载中发送类别的标识符。

    {
        "aps":{
            "alert":"Here's a notification",
            ...
            "category":"CATEGORY_ID"
        }
    }
    
  • 通过实现 UIApplicationDelegate 协议(protocol)方法来处理应用委托(delegate)中的用户操作: application:handleActionWithIdentifier:forRemoteNotification:completionHandler: 用于远程通知 application:handleActionWithIdentifier:forLocalNotification:completionHandler: 用于本地通知

关于objective-c - ios8如何实现交互式通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24840246/

相关文章:

iphone - 在 iOS 中处理登录和保留 token

objective-c - 设置自定义 UIView 实例的属性时出现无法识别的选择器错误

sorting - 如何在 swift 中使用 "ABAddressBookCopyArrayOfAllPeopleInSourceWithSortOrdering"?

ios - 是否有适用于 iOS 8 的新字体?

ios - SignalR-Objc - 架构 armv7 的 undefined symbol

ios - UICollectionView : Support for reordering on iOS 8

ios - 检测 iOS 设备类型

ios - 从另一个 ViewController 为 UIPickerView 重新加载数据

objective-c - 将包含 URL 的 NSString 转换为 POSIX 路径

xcode - 在 iOS 上升级到 PhoneGap 2.0 后,JSONKit.m 中出现多个 "Format String Issue"警告