iOS8/9的Notification新特性
来源:互联网 发布:拼长图不损画质的软件 编辑:程序博客网 时间:2024/05/22 15:07
在iOS8和iOS9下,notification增加了一些新的特性,使之更加强大:
- iOS8增加了下拉时的Action按钮,像微信一样;
- iOS9增加了像信息一样的可以下拉直接输入;
iOS8为了实现action按钮,增加了4个类,一起完成这个功能:
- UIUserNotificationSettings;
- UIUserNotificationType:alert/sound/badge
- UIUserNotificationCategory;
- UIUserNotificationAction;
闲话少说,还是代码讲的清楚:
//初始化action UIMutableUserNotificationAction* action1 = [[UIMutableUserNotificationAction alloc] init]; //设置action的identifier [action1 setIdentifier:@"action1"]; //title就是按钮上的文字 [action1 setTitle:@"title1"]; //设置点击后在后台处理,还是打开APP [action1 setActivationMode:UIUserNotificationActivationModeBackground]; //是不是像UIActionSheet那样的Destructive样式 [action1 setDestructive:NO]; //在锁屏界面操作时,是否需要解锁 [action1 setAuthenticationRequired:NO]; UIMutableUserNotificationAction* action2 = [[UIMutableUserNotificationAction alloc] init]; [action2 setIdentifier:@"action2"]; [action2 setTitle:@"title2"]; [action2 setActivationMode:UIUserNotificationActivationModeForeground]; [action2 setDestructive:NO]; [action2 setAuthenticationRequired:NO]; //一个category包含一组action,作为一种显示样式 UIMutableUserNotificationCategory* category = [[UIMutableUserNotificationCategory alloc] init]; [category setIdentifier:@"category1"]; //minimal作为banner样式时使用,最多只能有2个actions;default最多可以有4个actions [category setActions:@[action1,action2] forContext:UIUserNotificationActionContextMinimal]; NSSet* set = [NSSet setWithObject:category]; UIUserNotificationSettings* settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:set]; //注册notification设置 [[UIApplication sharedApplication] registerUserNotificationSettings:settings]; //添加一个notification,10秒后触发 UILocalNotification* notification = [[UILocalNotification alloc] init]; //设置notifiction的样式为"category1" [notification setCategory:@"category1"]; [notification setFireDate:[NSDate dateWithTimeIntervalSinceNow:10]]; [notification setAlertBody:@"this is an alert"]; [[UIApplication sharedApplication] scheduleLocalNotification:notification];
结果是这个样子的:
这时候点击action1,alert就直接消失了,因为action1的setActivationMode是background的;而点击action2,app就会被打开;
无论哪个action,系统打开的时候都会调用AppDelegate的-(void)application: handleActionWithIdentifier: forLocalNotification:(nonnull UILocalNotification *)completionHandler:
方法;如果直接点击alert的话,会调用application: didFinishLaunchingWithOptions:
方法,而options参数里就包含了alert的信息;
几个要注意的地方:
- push notification的时候,如何指定alert的样式,即category?
只需要在push的内容里添加category的Identifier:
"aps":{ "alert":"This is a notification", "category":<category的Identifier>}
APP正在运行的时候,收到了notification,如何接收?
此时会调用application:didReceiveLocalNotification:
方法;而系统不会有任何操作,不会发出声音、添加badge icon、也不会弹出alert;alert可以设置一个alertLaunchImage,可以替代系统默认的launch image;
APP的badge number如何自动+1:
苹果没有提供这个方法,因此我们要在服务器上保存某个用户的badge number,在推送时+1;能不能像信息那样,下拉直接回复?
iOS8没有这个功能,最新的iOS9可以:
action.behavior = UIUserNotificationBehaviorTextInput;
iOS8时,增加了在指定地点弹出的功能:
[notification setRegion:];
如何确定用户有没有禁止发送notifications:
设置notificationCategory后,检查AppDelegate有没有调用下面的方法:
- (void) application: didRegisterUserNotificationSettings:
- iOS8/9的Notification新特性
- IOS8 通知中心(Notification Center)新特性
- IOS8 通知中心(Notification Center)新特性
- iOS8的新特性
- 简述iOS8的新特性
- iOS8 Xcode6的新特性
- iOS7、iOS8的新特性
- Notification的一些新特性
- WWDC2014之iOS8的新特性预览
- 列举下iOS8的新特性
- Xcode6 ios8的定位新特性
- iOS8 新特性 TodayExtension 的简单实现
- iOS8的新特性(MOdel笔记)
- iOS8和iOS9新特性的总结
- iOS8新功能新特性
- iOS8 新特性
- ios8 新特性
- iOS8 新特性
- Path Sum II
- 【移动开发】Android应用程序中实用的代码框架(二)
- 关于listview中checkbox问题
- 【C#】解决进行反序列化时出错:。InnerException 消息是“反序列化对象 属于类型 System.String 时出现错误。读取 XML 数据时,超出最大字符串内容长度配额 (8192)。
- Binary Tree Maximum Path Sum
- iOS8/9的Notification新特性
- jQuery EasyUi实现tab页
- PlanNode enNode = PlanNode.withCityNameAndPlace可以搜到结果但PlanNode enNode = PlanNode.withLocation()却搜不到
- UITableView 分页显示、加载更多,数据源远程数据,解析JSON,以及iOS☞Category
- 【IOS 开发学习总结-OC-3】objective-c 类型转换
- Populating Next Right Pointers in Each Node
- 10个关于人工智能和机器学习的有趣开源项目
- 一个公共方法 绝对保证 sex的值为1或2
- Sum Root to Leaf Numbers