xcode ios8如何实现交互式通知
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24840246/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to implement interactive notifications ios8
提问by dev_cd123
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 8 but there is little documentation showing how to implement this feature. Can anyone show how I would go about setting up the 'start' action and the block of code which runs when this is pressed? If the app were closed would this feature still work?
我正在创建一个定时待办事项列表应用程序,用户可以在其中通过滑动以显示开始按钮来从锁定屏幕通知启动计时器。这是 iOS 8 中显示的新功能,但几乎没有文档显示如何实现此功能。任何人都可以展示我将如何设置“开始”操作以及按下该操作时运行的代码块?如果应用程序关闭,此功能是否仍然有效?
采纳答案by Shubhendu
In order to know more about interactive notifications- a new feature in iOS 8 go to the below link
要了解有关交互式通知的更多信息 - iOS 8 中的一项新功能,请访问以下链接
https://developer.apple.com/videos/wwdc/2014/
https://developer.apple.com/videos/wwdc/2014/
and then go to the section "What's New in iOS Notifications"
然后转到“iOS 通知中的新功能”部分
回答by Martin Devillers
@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.
@Shubhendu 感谢您的链接。对于那些不想坐视整个视频的人,这里简要回顾一下您需要做什么才能在您的应用程序中包含交互式通知。
Define all the actions that the user may execute from your app's notifications. These actions are created using the UIMutableUserNotificationActionclass.
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 notificationsapplication:handleActionWithIdentifier:forLocalNotification:completionHandler:
for local notifications
定义用户可以从您的应用程序通知中执行的所有操作。这些操作是使用UIMutableUserNotificationAction类创建的。
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 协议方法处理应用程序委托中的用户操作: 用于本地通知的
application:handleActionWithIdentifier:forRemoteNotification:completionHandler:
远程application:handleActionWithIdentifier:forLocalNotification:completionHandler:
通知
回答by Ramdhas
STEP 1:
第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.
第 2 步:要发送此类通知,只需将类别添加到有效负载中。
{
"aps" : {
"alert" : "Pull down to interact.",
"category" : "ACTIONABLE"
}
}
STEP 3: Use this method
第 3 步:使用此方法
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
1.参考链接:Obj C教程
回答by vijeesh
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.
iOS 8 带来了一个令人兴奋的新 API,用于创建交互式通知。这些允许您在您的应用程序之外为您的用户提供额外的功能。
Let's get started. There are 3 new classes in iOS 8 which are needed: UIUserNotificationSettings, UIUserNotificationCategory, UIUserNotificationAction
and their mutable counterparts.
让我们开始吧。iOS 8 中需要 3 个新类:UIUserNotificationSettings, UIUserNotificationCategory, UIUserNotificationAction
以及它们的可变对应物。
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:
//JSON 有效载荷:
{
"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:\
现在为了处理用户选择的操作,UIApplicationDelegate
协议中有 2 个新方法:\
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();
}
}
回答by DURGESH
For ios10 Actionable push use this
对于 ios10 Actionable push 使用这个
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];