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

为了创建交互式通知,需要iOS 8提供的3个新类:UIUserNotificationSettings, UIUserNotificationCategory, UIUserNotificationAction 以及它们的变体。
和以前简单地注册通知类型(sounds、banners、alerts)相比,现在你可以注册自定义的通知类别(categories)和动作(actions)。类别描述了应用自定义的通知类型,并且包含用户能够执行的响应动作。比如,你收到一个通知说某人在社交网上了关注了你,作为回应你可能会想要关注他或者忽略。
这里是一个非常简单的使用Objective-C编写的示例,演示如何注册一个包含两个动作的通知。
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
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添加到声明里。
|
1
2
3
4
|
"aps" : { "alert" : "Pull down to interact.", "category" : "ACTIONABLE"} |
现在为了响应用户选择的操作,你需要在UIApplicationDelegate协议添加两个新方法:
|
1
2
|
application:handleActionWithIdentifier:forLocalNotification:completionHandler:application:handleActionWithIdentifier:forRemoteNotification:completionHandler: |
用户从你的推送通知中选择一个动作后,该方法将会在后台被调用。
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
- (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表面上的功能,今后将进行更深入的研究。
iOS 8创建交互式通知-备的更多相关文章
- iOS 8创建交互式通知
iOS 8提供了一个令人兴奋的新API来创建交互式通知(interactive notifications),它能让你在你的应用之外为用户提供额外的功能.我发现网上还没有关于如何实现它的比较好的示例教 ...
- iOS创建本地通知和删除对应的通知,工作日通知
本文的代码主要是:创建本地通知,删除对应的本地通知,创建工作日闹钟 直接上代码: // // ViewController.m // LocalNSNotification // // Created ...
- iOS之创建通知、发送通知和移除通知的坑
1.创建通知,最好在viewDidLoad的方法中创建 - (void)viewDidLoad { [super viewDidLoad]; //创建通知 [[NSNotificationCenter ...
- iOS 10推送通知开发
原文地址:Developing Push Notifications for iOS 10,译者:李剑飞 虽然通知经常被过度使用,但是通知确实是一种获得用户关注和通知他们需要更新或行动的有效方式.iO ...
- iOS 本地推送通知
1.什么是本地推送通知 不需要联网的情况下,应用程序经由系统发出的通知 2.本地推送的使用场景 定时提醒,如玩游戏.记账.闹钟.备忘录等 3.实现本地推送通知的步骤 创建本地推送通知的对象UILoca ...
- 【Android】利用服务Service创建标题栏通知
创建标题栏通知的核心代码 public void CreateInform() { //定义一个PendingIntent,当用户点击通知时,跳转到某个Activity(也可以发送广播等) Inten ...
- 基本属性 - iOS中的本地通知
本地通知的基本使用 创建本地通知 设置属性 调度通知(添加通知到本地通知调度池) 注册用户通知权限(只需一次, 可以单独放在Appdelegate中, 或者别的地方) —> iOS8以后必须, ...
- android wear开发:为可穿戴设备创建一个通知 - Creating a Notification for Wearables
注:本文内容来自:https://developer.android.com/training/wearables/notifications/creating.html 翻译水平有限,如有疏漏,欢迎 ...
- (七十三)iOS本地推送通知的实现
iOS的推送通知分为本地推送和网络推送两种,如果App处于挂起状态,是可以发送本地通知的,如果已经被杀掉,则只有定时通知可以被执行,而类似于QQ的那种网络消息推送就无法实现了,因为App的网络模块在被 ...
随机推荐
- Uncaught TypeError: Cannot read property 'post' of undefined
- 探索WebKit内核(一)------ 菜鸟起步
为什么搞WebKit 如今研究WebKit的人越来越多,俺不能免俗,也增加当中.WebKit的火爆也是得益于浏览器和WebOS的混战,随着Palm WebOS, Chrome OS, Firefox ...
- [D3] 13. Cleaner D3 code with selection.call()
selection.call() method in D3 can aid in code organization and flexibility by eliminating the need t ...
- Retrofit2源码分析(一)
本文将顺着构建请求对象→构建请求接口→发起同步/异步请求的流程,分析retrofit2是如何实现的. 组成部分 Retrofit2源码主要分为以下几个部分: retrofit retrofit-ada ...
- readonly时禁用删除键,readonly按删除键后页面后退解决方案
readonly时禁用删除键, readonly按删除键后页面后退解决方案 >>>>>>>>>>>>>>>&g ...
- ubuntu自定义服务模板
根据他人代码修改: #!/bin/sh ### BEGIN INIT INFO # Provides: <pragram name> # Required-Start: $local_fs ...
- OpenCV 2 Computer Vision Application Programming Cookbook读书笔记
### `highgui`的常用函数: `cv::namedWindow`:一个命名窗口 `cv::imshow`:在指定窗口显示图像 `cv::waitKey`:等待按键 ### 像素级 * 在灰度 ...
- Oracle 汉字在不同字符集下所占字节
今天发现了一个问题,一个长度设置为2000字节的字段,插入一条长度为1000的汉字数据,竟然报错了. 一个汉字占两个字节,按理说刚好是2000个字节.但通过查看日志,发现插入数据的长度为3000字节. ...
- JDK自带方法实现RSA数字签名
JDK 6只支持MD2withRSA, MD5withRSA, SHA1withRSA 其他的如SHA512withRSA需要第三方包支持,如BC(bouncy castle) --20151126 ...
- PHP四种传参方式
test1界面: <html> <head> <title>testPHP</title> <meta http-equiv = "co ...