ios8如何实现交互式通知

Posted

技术标签:

【中文标题】ios8如何实现交互式通知【英文标题】:How to implement interactive notifications ios8 【发布时间】:2014-07-19 12:10:05 【问题描述】:

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

【问题讨论】:

您找到解决问题的方法了吗? 观看开发者视频向我展示了如何实现基本的通知操作代码。真的很有帮助的视频。为了锻炼计时器的结束时间,我使用了用户与开始通知交互的时间,并添加了计时器的持续时间,并使用它来设置新通知的触发日期。 请提供示例或git链接来实现。谢谢 参考此链接...***.com/questions/25929665/…了解更多信息,请访问...github.com/sgup77/SGNotification 参考***.com/questions/25929665/… 【参考方案1】:

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

定义用户可以从您的应用通知中执行的所有操作。这些操作是使用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: 用于本地通知

【讨论】:

非常感谢您对通知负载的描述! 是否可以有 3 个交互式通知操作? 如何处理舔屏上的远程通知【参考方案2】:

第 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];

第 2 步: 要发送此类通知,只需将类别添加到有效负载。

  
    "aps" :  
        "alert"    : "Pull down to interact.",
        "category" : "ACTIONABLE"
    

第 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.参考链接:Obj C tutorial

2.Swift Code Tutorial here

【讨论】:

所有这些都已弃用。 在 ios 11 中不工作,按钮不显示。你能在 ios 10+ 中提供吗?【参考方案3】:

要了解有关交互式通知的更多信息 - iOS 8 中的一项新功能,请访问以下链接

https://developer.apple.com/videos/wwdc/2014/

然后转到“iOS 通知中的新功能”部分

【讨论】:

【参考方案4】:

随着 iOS 8 的推出,一个令人兴奋的新 API 用于创建交互式通知。这些允许您在您的应用程序之外为您的用户提供额外的功能。

让我们开始吧。 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 协议上有 2 个新方法:\

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();
    

【讨论】:

【参考方案5】:

对于 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];

【讨论】:

以上是关于ios8如何实现交互式通知的主要内容,如果未能解决你的问题,请参考以下文章

交互式通知支持 iOS 8

iOS8 和 iOS7 推送通知负载

无法在 iOS8 上设置交互式推送通知

iOS 8 交互式通知默认不显示按钮

ios如何实现本地推送,兼容ios8

如何在 iOS8 的通知窗口中创建按钮?