zoukankan      html  css  js  c++  java
  • iOS 8创建交互式通知

    iOS 8提供了一个令人兴奋的新API来创建交互式通知(interactive notifications),它能让你在你的应用之外为用户提供额外的功能。我发现网上还没有关于如何实现它的比较好的示例教程,所以我将在这篇文章里来实现一个简单的交互式通知示例,分享给大家。

    interactive.gif

    为了创建交互式通知,需要iOS 8提供的3个新类:UIUserNotificationSettingsUIUserNotificationCategoryUIUserNotificationAction 以及它们的变体。

    和以前简单地注册通知类型(sounds、banners、alerts)相比,现在你可以注册自定义的通知类别(categories)和动作(actions)。类别描述了应用自定义的通知类型,并且包含用户能够执行的响应动作。比如,你收到一个通知说某人在社交网上了关注了你,作为回应你可能会想要关注他或者忽略。

    这里是一个非常简单的使用Objective-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];
    }

    要发送这个通知类型,只需简单的将category添加到声明里。

    "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 新通知API表面上的功能,今后我将更深入的研究一下,有机会再和大家分享。

    我们需要注意这个UIUserNotificationActionContextDefault,如果我们使用这个,我们会得到这个推送行为,Maybe和Accept

    我们还可以使用UIUserNotificationActionContextMinimal得到的是Decline和Accept行为

    Maybe和Accept 

    Decline和Accept
  • 相关阅读:
    2 安装驱动出现异常
    1 Yoga3 系统装机总结.
    6 关于 Oracle NULL栏位和PL./SQL执行实验
    4 C#和Java 的比较
    3 委托、匿名函数、lambda表达式
    2 跨线程访问控件InvokeHelper类
    2 跨线程访问控件InvokeHelper类
    1 Winform 异步更新控件
    1 Winform 异步更新控件
    C# DataTable的詳細用法
  • 原文地址:https://www.cnblogs.com/fengmin/p/5438121.html
Copyright © 2011-2022 走看看