iOS8 UILocalNotification 和 UIRemoteNotification 使用注意 草稿,正在整理中。。。。
先说一个关于UILocalNotification的知识点,容易被忘记:
Each app on a device is limited to 64 scheduled local notifications. The system discards scheduled notifications in excess of this limit, keeping only the 64 notifications that will fire the soonest. Recurring notifications are treated as a single notification.
上面说的是,local notification 最多64个,重复触发的notification被当成一个。
再来一个注意点:ios7 和以上版本
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler: (void (^)(UIBackgroundFetchResult))completionHandler
如果这个实现了这个函数,那么下面这个函数
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
不会被调用!所以只写上面那个函数就行了。
在ios8中,有了关于本地通知和远程通知的新改动,下面来看看。
先看一段网上抄来的代码
UIMutableUserNotificationAction *notificationAction1 = [[UIMutableUserNotificationAction alloc] init];
notificationAction1.identifier = @"Accept";
notificationAction1.title = @"Accept";
notificationAction1.activationMode = UIUserNotificationActivationModeBackground;
notificationAction1.destructive = NO;
notificationAction1.authenticationRequired = NO; UIMutableUserNotificationAction *notificationAction2 = [[UIMutableUserNotificationAction alloc] init];
notificationAction2.identifier = @"Reject";
notificationAction2.title = @"Reject";
notificationAction2.activationMode = UIUserNotificationActivationModeBackground;
notificationAction2.destructive = YES;
notificationAction2.authenticationRequired = YES; UIMutableUserNotificationAction *notificationAction3 = [[UIMutableUserNotificationAction alloc] init];
notificationAction3.identifier = @"Reply";
notificationAction3.title = @"Reply";
notificationAction3.activationMode = UIUserNotificationActivationModeForeground;
notificationAction3.destructive = NO;
notificationAction3.authenticationRequired = YES; UIMutableUserNotificationCategory *notificationCategory = [[UIMutableUserNotificationCategory alloc] init];
notificationCategory.identifier = @"Email"; // [notificationCategory setActions:@[notificationAction3] forContext:UIUserNotificationActionContextDefault];
[notificationCategory setActions:@[notificationAction1] forContext:UIUserNotificationActionContextMinimal]; NSSet *categories = [NSSet setWithObjects:notificationCategory, nil]; UIUserNotificationType notificationType = UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert;
UIUserNotificationSettings *notificationSettings = [UIUserNotificationSettings settingsForTypes:notificationType categories:categories]; [[UIApplication sharedApplication] registerUserNotificationSettings:notificationSettings]; UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:];
localNotification.alertBody = @"Testing";
localNotification.category = @"Email"; // Same as category identifier
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
这是一段发送本地通知的代码,其中涉及到了这次更新的几个类,UIMutableUserNotificationAction,UIMutableUserNotificationCategory,UIUserNotificationSettings 我们需要依次生成这几个对象,最后使用
[[UIApplication sharedApplication] registerUserNotificationSettings:notificationSettings];注意,如果注册了多次,那么以最后一次为准。把程序的通知设定注册到系统中去。
如果是程序第一次进行注册,那么系统会弹出对话框,索取notification权限,对话框内容和notificationSettings的设置无关,都是下图中的内容
如果点击不允许,那么在settings中notification被这是成关闭的,如下图:
否则,notification就会被打开。 如果UIUserNotificationType notificationType = UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert; 那么settings中的设定项目如下图:
如果UIUserNotificationType notificationType = UIUserNotificationTypeAlert;那么settings中的设定项目如下图:
注意,这个设定中根本不会出现UIUserNotificationTypeBadge | UIUserNotificationTypeSound 相关的设定 有趣的地方来了,如果一开始注册的设定是
UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert,而后来又注册了
UIUserNotificationTypeAlert,那么settings中的图就会从 左边的变成右边的!settings中的设定会根据代码动态改变!
如果不使用UIUserNotificationTypeAlert,那么不会弹出notification,对应的提醒内容,可以在notification center或者锁屏界面查看。 上面有2个参数UIUserNotificationActionContextDefault 和 UIUserNotificationActionContextMinimal 这里需要说明一下
官方说明如下
UIUserNotificationActionContextDefault
The default context for displaying the alert. In this context, the full UI is displayed for the notification’s alert. You may specify up to four custom actions in this context. UIUserNotificationActionContextMinimal
A notification where space is minimal. In this context, a minimal UI is displayed for the notification’s alert. You may specify up to two custom actions in this context.
那么什么是Minimal 和default context 呢,见下图
期中,Banners就是UIUserNotificationActionContextMinimal 而 alerts 就是 UIUserNotificationActionContextDefault!!
如果你使用了UIUserNotificationActionContextDefault,那么有以下的效果图
其中的accept,reject,reply是代码指定的,open是系统提供的,效果如同利用banners样式时点击通知内容本身,调用的是didReceiveLocalNotification 或 didReceiveRemoteNotification。
如果代码中不用UIUserNotificationActionContextMinimal,而在系统设置中用了banners样式,那么会使用UIUserNotificationActionContextDefault的多个action中的最后一个,在banners样式中显示!
注意,当应用程序在前台时,通知到来,系统不会为程序弹出alert(共有3中弹出方式,用户可以在settings 中进行设定),而是会调用相应的didReceiveLocalNotification 和 didReceiveRemoteNotification 这2个代理函数,把通知内容交给程序自身处理。而当程序不在前台时,系统才会为用户弹出alert,提示用户进行相应操作。 另外,点击通知中心的通知时,有2中情况:
一种是程序已经启动了而且不在前台,这时,也会调用didReceiveLocalNotification,那么如何和系统在前台时调用的didReceiveLocalNotification区别呢?(为什么要区别:一般点击notification center后进入程序不弹出alert,而程序在前台时收到通知要弹出alert,这是苹果程序的一般做法)答案是:可以通过UIApplication.sharedApplication().applicationState来区别,当从notification center进来,调用didReceiveLocalNotification时 程序的状态是UIApplicationState.Inactive。
另一种状态是程序没有系统,这时会调用didFinishLaunchingWithOptions函数,把notification通过参数传入,不会调用
didReceiveLocalNotification 了。
。
这里UIMutableUserNotificationAction就代表了一个操作,在界面上的表示形式就是一个按钮,当点击按钮时,就会调用
- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UILocalNotification *)notification completionHandler:(void (^)())completionHandler 或者 - (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo completionHandler:(void(^)())completionHandler NS_AVAILABLE_IOS(8_0);
不会再调用 didReceiveLocalNotification 或 didReceiveRemoteNotification 这2个代理函数了。
另外,在UIMutableUserNotificationAction 中有一个属性,activationMode,当它是UIUserNotificationActivationModeBackground时,系统会选在在后台调用handleActionWithIdentifier函数,并执行里面的代码,不启动程序界面。
,执行的时间是有限的,我测试的是30s,如果超过30s你的程序还在后台执行,那么系统会终止调你的程序,并抛出异常。异常如下
<Warning>: <BKNewProcess: 0x17e685f0; -.bbbb; pid: ; hostpid: -> has active assertions beyond permitted time:
{(
<BKProcessAssertion: 0x17d6b4f0> id: -2B9D290F-7F21-4DCB-955B-9D80DE693382 name: Notification action process: <BKNewProcess: 0x17e685f0; -.bbbb; pid: ; hostpid: -> permittedBackgroundDuration: 30.000000 reason: notificationAction owner pid: preventSuspend preventThrottleDownUI preventIdleSleep preventSuspendOnSleep ,
<BKProcessAssertion: 0x17d6de50> id: -707C3B54-51BC-47DA-B779-B11888416FE4 name: Deliver Message process: <BKNewProcess: 0x17e685f0; -.bbbb; pid: ; hostpid: -> permittedBackgroundDuration: 10.000000 reason: suspend owner pid: preventSuspend preventThrottleDownCPU preventThrottleDownUI preventSuspendOnSleep
)}
注意,虽然你设置了action,但是如果你没有点击action按钮,而是通过点击通知内容本身出发消息,那么系统扔会像ios7一样,调用didReceiveLocalNotification 或 didReceiveRemoteNotification 这2个代理函数(可以自己体验一下ios8通知的操作方法))。
如果程序没有启动,用户点击action按钮后,系统会先调用didFinishLaunchingWithOptions启动程序,再调用
- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UILocalNotification *)notification completionHandler:(void (^)())completionHandler 或者 - (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo completionHandler:(void(^)())completionHandler NS_AVAILABLE_IOS(8_0);
来处理action,这一点和以前点击通知内容本身的效果不一样,点击内容本身时,是不会调用didReceiveLocalNotification函数的,仅仅调用didFinishLaunchingWithOptions。我感觉这是因为action处理比较复杂,无法再直接附加在didFinishLaunchingWithOptions当做参数传入了。
iOS8 UILocalNotification 和 UIRemoteNotification 使用注意 草稿,正在整理中。。。。的更多相关文章
- iOS8中 UILocalNotification 和 UIRemoteNotification 使用注意
先说一个关于UILocalNotification的知识点,容易被忘记: Each app on a device is limited to 64 scheduled local notificat ...
- iOS8 UILocalNotification 添加启动授权
猴子原创.欢迎转载.转载请注明: 转载自Cocos2Der-CSDN,谢谢! 原文地址: http://blog.csdn.net/cocos2der/article/details/46810357 ...
- iOS8 UILocalNotification 增加启动授权
猴子原创,欢迎转载.转载请注明: 转载自Cocos2Der-CSDN,谢谢! 原文地址: http://blog.csdn.net/cocos2der/article/details/46810357 ...
- ios项目里扒出来的json文件
p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 13.0px Menlo; color: #000000 } p.p2 { margin: 0.0px 0. ...
- Github上关于iOS的各种开源项目集合(强烈建议大家收藏,查看,总有一款你需要)
下拉刷新 EGOTableViewPullRefresh - 最早的下拉刷新控件. SVPullToRefresh - 下拉刷新控件. MJRefresh - 仅需一行代码就可以为UITableVie ...
- iOS学习资源个人整理
1208更新: http://www.tuyiyi.com 图翼网 https://github.com/Alamofire/Al ...
- iOS及Mac开源项目和学习资料【超级全面】
UI 下拉刷新 EGOTableViewPullRefresh – 最早的下拉刷新控件. SVPullToRefresh – 下拉刷新控件. MJRefresh – 仅需一行代码就可以为UITable ...
- CSV - 操作比较
在数据报表生成的时候,我们一般会用程序去生成CSV.其中有些需要注意的地方. log_file = open('delay.%s.csv' % s_end, 'w') log_file.write(' ...
- iOS:iOS开发非常全的三方库、插件等等
iOS开发非常全的三方库.插件等等 github排名:https://github.com/trending, github搜索:https://github.com/search. 此文章转自git ...
随机推荐
- BZOJ-3130 费用流 (听题目胡扯丶裸最大流) 二分判定+最大流+实数精度乱搞
DCrusher爷喜欢A我做的水题,没办法,只能A他做不动的题了.... 3130: [Sdoi2013]费用流 Time Limit: 10 Sec Memory Limit: 128 MBSec ...
- BZOJ-2748 音量调节 DP+背包(脑残)
水DP,一开始竟然想错了...水了半天....真可怕 2748: [HAOI2012]音量调节 Time Limit: 3 Sec Memory Limit: 128 MB Submit: 1156 ...
- .net 时间戳互相转换(精确到毫秒)
这里记录一个时间戳的互相转换方法,网上都找了,基本都没有精确到毫秒,我的这个基本可以满足精确到毫秒的级别,代码如下: /// <summary> /// Unix时间戳转换为DateTim ...
- dijkstra,SPFA,Floyd求最短路
Dijkstra: 裸的算法,O(n^2),使用邻接矩阵: 算法思想: 定义两个集合,一开始集合1只有一个源点,集合2有剩下的点. STEP1:在集合2中找一个到源点距离最近的顶点k:min{d[k] ...
- 洛谷P2014 TYVJ1051 选课
题目描述 在大学里每个学生,为了达到一定的学分,必须从很多课程里选择一些课程来学习,在课程里有些课程必须在某些课程之前学习,如高等数学总是在其它课程之前学习.现在有N门功课,每门课有个学分,每门课有一 ...
- 表单form action的url写法
在写web页面时,标签 是很常见的元素,它的一个属性是action,用来标识将表单交给谁去处理.很显然,这里有一个地址的问题,而且是在服务器这边的地址.比如服务器内的一个servlet. 那么这个 ...
- (Beta)Let's-版本发布说明
Let's App(Beta)现已隆重上市 GIT源码请戳此处 版本的新功能 我们在这一版本对于项目的规划目标主要集中在三个方面——预约用户观感,完善功能链条,改善用户体验 界面 首先,在β阶 ...
- insert 多个values
INSERT INTO `user_mail_attach` VALUES(, , , , , ), (, , , , , ); 这种比写多条insert语句效率高
- set集合类型 redis
向名称为key的set中添加元素: 命令:sadd #不允许有重复的值 2 删除名称为key的set中的元素: 命令:srem 3 随机返回并删除名称称为ke ...
- json格式不对引起的报错
报JSONDecondeError这种类型的错误的时候就要检查下json格式是否是正确的了,这里提供一个http://www.bejson.com/ Traceback (most recent ca ...