• iOS 8下简单,可交互式的通知


    本文翻译自:Simple, interactive notifications in iOS 8

    在iOS 8 中新添加了一个API用来创建可交互式的通知。这些API允许你向在应用外的用户提供额外的功能。我发现网络上缺少清晰的例子,所以我认为我应该写一篇文章来向你展示如何简单地实现这个功能。下面是一个例子。

    让我们开始吧,在iOS 8中有三个新的类是必须的: UIUserNotificationSettings, UIUserNotificationCategory, UIUserNotificationAction 以及它们的可变副本。

    除了简单地注册通知类型(声音,横幅通知,警告),你还可以注册自定义的通知类别和动作。类别描述了一个你应用发送的自定义的通知类型和包含的用户可以执行响应的动作。例如你收到了别人在社交网络上关注你的通知。为了响应,你可能想也关注它们或者忽略。

    下面是一个用Objectice-C写的简单例子,描述了如何注册一个有两个动作的通知。

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

    为了发送这个类型的通知,可以简单地添加这个类别到 payload。

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

    根据标识符来判断用户选择了哪个动作,最后按照文档,确保调用那个completionHandler。就这样了。这是一个非常简单的例子,只是简单地从表层接触了iOS 8通知可以干什么。在将来的文章中,我会根据深入。Enjoy。

  • 相关阅读:
    Quartz.Net在windows服务中的使用
    mysql之group by,order by
    mysql之select,insert,delete,update
    win8.1安装VMware Error:This product may not be installed on a comuputer that has Microsoft HyperV installed
    mysql之创建数据库,创建数据表
    深入浅出空间索引:2
    地图点聚合优化方案
    地理围栏算法解析
    GeoHash核心原理解析
    Mongodb地理空间索引
  • 原文地址:https://www.cnblogs.com/YungMing/p/4341869.html
Copyright © 2020-2023  润新知