iOS8无法弹出本地通知?
最近在看《iOS编程(第4版)》(就是Big Nerd Ranch用的那本教材)。这本书写的不错,推荐一下,写的很细致,循序渐进,不能不赞一下外国人写书的思路,确实跟国人不同。之前学Android的时候,看了《Android Programming The Big Nerd Ranch Guide》,虽然全英文看得有点慢,但是慢慢看觉得很有意思,对于有了一定基础的初学者,收获很大。回到这本国人翻译的iOS编程,中文翻译过来的一些词汇有点拗口,我表示有点记不住╭(╯^╰)╮,看到大段中文的时候,耐心不足。
言归正传,今天看到一处,关于添加Local Notification的,自己用XCode6进行构建的时候,出现了错误。
程序是在一个按钮的点击事件的响应方法中,注册本地通知。下面就是ViewController.m中的按钮响应方法。
-(IBAction)addReminder:(id)sender
{
NSDate *date=self.datePicker.date;
UILocalNotification *note=[[UILocalNotification alloc] init];
note.alertBody=@"Hypnotize me!";
note.fireDate=date;
[[UIApplication sharedApplication] scheduleLocalNotification:note];
}
构建之后,点击模拟器应用中的Button,没有弹出通知,而XCode却给出了下面的Debug信息。
-- ::31.538 HypnoNerd[:] Attempting to schedule a local notification <UIConcreteLocalNotification: 0x7fcd5be826b0>{fire date = Thursday, July , at :: AM China Standard Time, time zone = (null), repeat interval = , repeat count = UILocalNotificationInfiniteRepeatCount, next fire date = (null), user info = (null)} with an alert but haven't received permission from the user to display alerts
确认代码输入没有错,那么为什么会弹出这样的信息呢?抓住提示信息中的最后一句,有个关键词“permission”,瞬间感觉和写Android应用时候用到的permission很像呀。
上网查了一下,看到这篇文章“iOS8系统下的无法弹出通知”,说是iOS8系统变更了注册方法(没错,我用的SDK是iOS 8.3)。用了作者的方法,确实解决了问题。
不过作者只写了解决办法,对于原因没有多做解释。
我到XCode的SDK Guide帮助文档 Local and Remote Notification Programming Guide: Registering, Scheduling, and Handling User Notifications
中,找到了Apple关于这个问题的说明。
In iOS 8 and later, apps that use either local or remote notifications must register the types of notifications they intend to deliver. The system then gives the user the ability to limit the types of notifications your app displays. The system does not badge icons, display alert messages, or play alert sounds if any of these notification types are not enabled for your app, even if they are specified in the notification payload.
在iOS8以及更高版本的iOS系统中,如果要用本地通知或者远程通知,必须要注册通知的类型,注册成功之后,系统才会给予用户传递通知(显示通知)的权限。
UIUserNotificationType type=UIUserNotificationTypeAlert;
UIUserNotificationSettings *settings=[UIUserNotificationSettings settingsForTypes:type categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
在ApplicationDelegate中注册通知之后,第一次运行App,会弹出一个对话框,询问用户是否允许这个App发送通知,用户点击“确认”后,App才能正常发送通知。
检讨一下自己,感觉到现在还没养成到官方的帮助文档寻找答案的习惯,一遇到问题就习惯找度娘╭(╯^╰)╮
iOS8无法弹出本地通知?的更多相关文章
- iOS APP 在前台时弹出本地通知
iOS10 之后使用才有效果 1.在 AppDelegate.m 文件里面添加下面的方法. - (void)userNotificationCenter:(UNUserNotificationCent ...
- 在iOS 8及以后使用UIAlertController 等各种弹出警告通知
原文转自:在iOS 8中使用UIAlertController 感谢作者分享,自我学习之用 iOS 8的新特性之一就是让接口更有适应性.更灵活,因此许多视图控制器的实现方式发生了巨大的变化.全新的UI ...
- Notification 弹出一个通知在桌面右下角
if (!("Notification" in window)) { //alert("This browser does not support desktop not ...
- iOS8 UIAlertController弹出框中添加视图(例如日期选择器等等)
UIDatePicker *datePicker = [[UIDatePicker alloc] init]; datePicker.datePickerMode = UIDatePickerMode ...
- selenium如何屏蔽谷歌浏览器弹出的通知
使用selenium访问新浪微博的时候 浏览器总会有个通知,需要点击 类似下面这样 下面使用chromeoptions来修改浏览器的设置 from selenium import webdrive ...
- Chrome插件:弹出桌面通知
if (window.Notification) { var popNotice = function() { if (Notification.permission == "granted ...
- selenium屏蔽谷歌浏览器弹出的通知
使用chromeoptions来修改浏览器的设置 from selenium import webdriver import time options = webdriver.ChromeOption ...
- Windows Phone 8仿Toast弹出通知。
Wp8后台收到消息可以弹出Toast通知,可是应用在前台跑的时候却好像没有办法弹出Toast通知.而需求上有要求要弹出Toast通知,在这种情况下,就开始考虑是否能够做一个类似Toast的弹出通知呢? ...
- 关于iOS8上本地通知接收不到的问题
在iOS8系统开发使用本地通知时,会出现如下的相关提示语: 1 Attempting to schedule a local notification2 with an alert but haven ...
随机推荐
- Linux下shell脚本中信号捕获和函数练习脚本之ping一个网段
该脚本主要的目的是练习在Linux bash脚本中捕获信号,顺便练习一下函数的使用,还有就是终止一个正在运行的程序后,该程序打开的文件的后续处理问题等等!脚本功能: ping一个网段内的IP,检测哪 ...
- UVA1213Sum of Different Primes(素数打表 + DP)
题目链接 题意:选择k个素数,使得和为N(1120)的方案数: 筛选出 <= N 的素数,然后就背包 写的时候没初始dp[0][0] = 1;而且方案数也没相加,真是弱逼 #include &l ...
- 初学angular-简单的angular指令
实现一个简单的input清空内容,且清空对应ngModel 前台部分 <html ng-app="hpapp"> <head> <meta chars ...
- CSS3-实现单选框radio的小动画
在微信上看到一个教程文,觉得制作的小动画还是很有意思的,自己也试验了一下.一开始动画怎么都不执行(我用的HB),因为内置浏览器对css3的不兼容.加上各种浏览器前缀后就好了.但是旋转那个效果,在HB里 ...
- CIQRCodeGenerator Core Image Filter Reference
https://developer.apple.com/library/prerelease/content/documentation/GraphicsImaging/Reference/CoreI ...
- BigDecimal 类型数据的一些应用
1.比较大小 可以通过BigDecimal的compareTo方法来进行比较.返回的结果是int类型,-1表示小于,0是等于,1是大于. 例如: if(a.compareTo(b) == -1){ a ...
- 使用ultraiso制作启动盘
1.以管理员方式运行Ultralso 2.点击菜单栏里的“启动”菜单下的“写入硬盘映像”命令,打开“写入硬盘映像”对话框. “硬盘驱动器”里就是选择你要刻录的U盘,这里演示用的是一张数码相机的内存卡. ...
- HashMap与ArrayList互相嵌套的代码实现
HashMap嵌套ArrayList的代码实现 结果要求为 三国演义 吕布 周瑜笑傲江湖 令狐冲 林平之神雕侠侣 ...
- npm run-script
package.json "scripts": { "start": "electron .", "package": ...
- DataView
表示用于排序.筛选.搜索.编辑和导航的 DataTable 的可绑定数据的自定义视图. DataView的功能类似于数据库的视图,他是数据源DataTable的封装对象,可以对数据源进行排序.搜索.过 ...