我正在创建一个定时待办事项列表应用程序,用户可以通过滑动显示开始按钮,从锁屏通知中启动计时器。这是iOS 8中显示的一个新功能,但很少有文档说明如何实现此功能。有人能告诉我如何设置“开始”操作和按下这个按钮时运行的代码块吗?如果应用程序被关闭,这个功能还能工作吗?
发布于 2014-07-20 03:48:24
发布于 2015-04-28 15:12:59
iOS 8带来了一个令人兴奋的新应用程序接口,用于创建交互式通知。这些允许您向应用程序之外的用户提供附加功能。
我们开始吧。iOS 8中需要3个新类:UIUserNotificationSettings, UIUserNotificationCategory, UIUserNotificationAction和它们的可变对应类。
您现在可以注册自定义通知类别和操作,而不是简单地注册通知类型(声音、横幅、警报)。类别描述应用程序发送的自定义通知类型,并包含用户可以响应的操作。例如,您收到有人在社交网络上关注您的通知。作为回应,您可能想要跟随它们返回或忽略它们。
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有效负载:
{
"aps" : {
"alert" : "Pull down to interact.",
"category" : "ACTIONABLE"
}
}现在,为了处理用户选择的操作,在UIApplicationDelegate协议上有两个新方法:\
application:handleActionWithIdentifier:forLocalNotification:completionHandler:
application:handleActionWithIdentifier:forRemoteNotification:completionHandler:当用户从您的推送通知中选择一个操作时,将在后台调用这些方法。
- (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();
}
}发布于 2016-10-18 16:34:33
对于可操作的ios10推送,请使用以下内容
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];https://stackoverflow.com/questions/24840246
复制相似问题