首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何实现交互式通知ios8

如何实现交互式通知ios8
EN

Stack Overflow用户
提问于 2014-07-19 20:10:05
回答 3查看 12.5K关注 0票数 17

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

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2014-07-20 03:48:24

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

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

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

票数 3
EN

Stack Overflow用户

发布于 2015-04-28 15:12:59

iOS 8带来了一个令人兴奋的新应用程序接口,用于创建交互式通知。这些允许您向应用程序之外的用户提供附加功能。

我们开始吧。iOS 8中需要3个新类:UIUserNotificationSettings, UIUserNotificationCategory, UIUserNotificationAction和它们的可变对应类。

您现在可以注册自定义通知类别和操作,而不是简单地注册通知类型(声音、横幅、警报)。类别描述应用程序发送的自定义通知类型,并包含用户可以响应的操作。例如,您收到有人在社交网络上关注您的通知。作为回应,您可能想要跟随它们返回或忽略它们。

代码语言:javascript
复制
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有效负载:

代码语言:javascript
复制
{
"aps" : { 
    "alert"    : "Pull down to interact.",
    "category" : "ACTIONABLE"
    }
}

现在,为了处理用户选择的操作,在UIApplicationDelegate协议上有两个新方法:\

代码语言:javascript
复制
application:handleActionWithIdentifier:forLocalNotification:completionHandler:
application:handleActionWithIdentifier:forRemoteNotification:completionHandler:

当用户从您的推送通知中选择一个操作时,将在后台调用这些方法。

代码语言:javascript
复制
- (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();
    }
}
票数 2
EN

Stack Overflow用户

发布于 2016-10-18 16:34:33

对于可操作的ios10推送,请使用以下内容

代码语言:javascript
复制
 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];
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24840246

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档