`
xiaotian01
  • 浏览: 466 次
最近访客 更多访客>>
文章分类
社区版块
存档分类
最新评论

DevStore技术支持:在iOS 8中创建交互式通知

阅读更多
摘要:iOS 8提供了一个令人兴奋的新API来创建交互式通知,它能让你在你的应用之外为用户提供额外的功能。

由于发现网上还没有关于如何实现它的比较好的示例教程,所以我将在这篇文章里来实现一个简单的交互式通知示例,分享给大家。

和以前简单地注册通知类型(sounds、banners、alerts)相比,现在你可以注册自定义的通知类别和动作。类别描述了应用自定义的通知类型,并且包含用户能够执行的响应动作。比如,你收到一个通知说某人在社交网上了关注了你,作为回应你可能会想要关注他或者忽略。
这里是一个非常简单的使用Objective-C编写的示例,演示如何注册一个包含两个动作的通知。
01
NSString * const NotificationCategoryIdent  = @"ACTIONABLE";
02
NSString * const NotificationActionOneIdent = @"ACTION_ONE";
03
NSString * const NotificationActionTwoIdent = @"ACTION_TWO";
04
- (void)registerForNotification {
05
UIMutableUserNotificationAction *action1;
06
action1 = [[UIMutableUserNotificationAction alloc] init];
07
[action1 setActivationMode:UIUserNotificationActivationModeBackground];
08
[action1 setTitle:@"Action 1"];
09
[action1 setIdentifier:NotificationActionOneIdent];
10
[action1 setDestructive:NO];
11
[action1 setAuthenticationRequired:NO];
12
UIMutableUserNotificationAction *action2;
13
action2 = [[UIMutableUserNotificationAction alloc] init];
14
[action2 setActivationMode:UIUserNotificationActivationModeBackground];
15
[action2 setTitle:@"Action 2"];
16
[action2 setIdentifier:NotificationActionTwoIdent];
17
[action2 setDestructive:NO];
18
[action2 setAuthenticationRequired:NO];
19
UIMutableUserNotificationCategory *actionCategory;
20
actionCategory = [[UIMutableUserNotificationCategory alloc] init];
21
[actionCategory setIdentifier:NotificationCategoryIdent];
22
[actionCategory setActions:@[action1, action2]
23
               forContext:UIUserNotificationActionContextDefault];
24
NSSet *categories = [NSSet setWithObject:actionCategory];
25
UIUserNotificationType types = (UIUserNotificationTypeAlert|
26
                                    UIUserNotificationTypeSound|
27
                                    UIUserNotificationTypeBadge);
28
UIUserNotificationSettings *settings;
29
settings = [UIUserNotificationSettings settingsForTypes:types
30
                                                 categories:categories];
31
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
32
}
要发送这个通知类型,只需简单的将category添加到声明里。
1
"aps" : {
2
    "alert"    : "Pull down to interact.",
3
    "category" : "ACTIONABLE"
4
}
现在为了响应用户选择的操作,你需要在UIApplicationDelegate协议添加两个新方法:
1
application:handleActionWithIdentifier:forLocalNotification:completionHandler:
2
application:handleActionWithIdentifier:forRemoteNotification:completionHandler
用户从你的推送通知中选择一个动作后,该方法将会在后台被调用。
01
- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo completionHandler:(void (^)())completionHandler {
02
  if ([identifier isEqualToString:NotificationActionOneIdent]) {
03
  NSLog(@"You chose action 1.");
04
    }
05
  else if ([identifier isEqualToString:NotificationActionTwoIdent]) {  
06
  NSLog(@"You chose action 2.");
07
    }   
08
  if (completionHandler) {
09
  completionHandler();
10
    }
11
}
如文档所述,通过标示符来判定是哪个动作被选中,最后调用completionHandler,即可大功告成。这里仅仅简单的演示了一下iOS 8 新通知API表面上的功能,今后我将更深入的研究一下,有机会再和大家分享。
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics