我正在创建一个定时待办事项列表应用程序,用户可以在该应用程序中通过滑动显示开始按钮从锁定屏幕通知启动计时器。这是 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/