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。

最近的文章

在iOS8 下用Swift 创建自定义的键盘

本文翻译自How to make a custom keyboard in iOS 8 using Swift我将讲解一些关于键盘扩展的基本知识,然后使用iOS 8 提供的新应用扩展API来创建一个莫斯码键盘。大概需要你花20多分钟来走完所有的步骤。 完整代码综述一个自定义的键盘会替换系统的键盘,来提供给用户一个新的文本输入方法,或者输入哪些iOS系统还不支持的语言。一个自定义键盘的基本功能很简单:响应点击,手势或者其它输入事件以及在当前的文本输入对象的文本插入点上提供非属性化的NSS...…

iOS继续阅读
更早的文章

在 OS X 上构建和运行 .Net 的 CoreCLR

本文翻译自:Building and Running .NET’s CoreCLR on OS X没错,你没听错,微软已经开源了.Net 核心的完整运行时实现 CLR,而它不仅仅可以运行在Windows 上。他们不是随便地转储一堆ZIP文件到一个FTP服务器上,而是给我们提供了一个功能齐全,易于编译,容易托管在所有人喜欢的文件共享介质上的东西。微软甚至走得更远,在GitHub上设置了双向镜像,这样他们的内部系统会与我们看到的保持同步。这种行为给我留下了深刻的印象。他们最初的版本适用于W...…

iOS继续阅读