最近在看《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无法弹出本地通知?的更多相关文章

  1. iOS APP 在前台时弹出本地通知

    iOS10 之后使用才有效果 1.在 AppDelegate.m 文件里面添加下面的方法. - (void)userNotificationCenter:(UNUserNotificationCent ...

  2. 在iOS 8及以后使用UIAlertController 等各种弹出警告通知

    原文转自:在iOS 8中使用UIAlertController 感谢作者分享,自我学习之用 iOS 8的新特性之一就是让接口更有适应性.更灵活,因此许多视图控制器的实现方式发生了巨大的变化.全新的UI ...

  3. Notification 弹出一个通知在桌面右下角

    if (!("Notification" in window)) { //alert("This browser does not support desktop not ...

  4. iOS8 UIAlertController弹出框中添加视图(例如日期选择器等等)

    UIDatePicker *datePicker = [[UIDatePicker alloc] init]; datePicker.datePickerMode = UIDatePickerMode ...

  5. selenium如何屏蔽谷歌浏览器弹出的通知

    使用selenium访问新浪微博的时候  浏览器总会有个通知,需要点击  类似下面这样 下面使用chromeoptions来修改浏览器的设置 from selenium import webdrive ...

  6. Chrome插件:弹出桌面通知

    if (window.Notification) { var popNotice = function() { if (Notification.permission == "granted ...

  7. selenium屏蔽谷歌浏览器弹出的通知

    使用chromeoptions来修改浏览器的设置 from selenium import webdriver import time options = webdriver.ChromeOption ...

  8. Windows Phone 8仿Toast弹出通知。

    Wp8后台收到消息可以弹出Toast通知,可是应用在前台跑的时候却好像没有办法弹出Toast通知.而需求上有要求要弹出Toast通知,在这种情况下,就开始考虑是否能够做一个类似Toast的弹出通知呢? ...

  9. 关于iOS8上本地通知接收不到的问题

    在iOS8系统开发使用本地通知时,会出现如下的相关提示语: 1 Attempting to schedule a local notification2 with an alert but haven ...

随机推荐

  1. AngularJs 键盘事件和鼠标事件

    ngKeydown/ngKeypress/ngKeyup 该指令在按键按下/按键按下/按键松开时执行指定表达式. ngKeydown和ngKeypress略有不同,目前的测试是ngKeypress针对 ...

  2. POJ 2559 Largest Rectangle in a Histogram(单调栈)

    传送门 Description A histogram is a polygon composed of a sequence of rectangles aligned at a common ba ...

  3. 【项目】Http请求在NSMutableURLRequest添加HttpBody的字典params属性

    在请求头中加入字典集合的Body,首先把字典转换成json,然后json转换成NSData,然后加入到HTTPBody中,我有已下写法 // 参数paramsNSDictionary * params ...

  4. Unicode 与 UTF 字符标准

    Unicode 国际字符标准(UCS)是一个字符编码系统,它被设计用来支持世界各国不同语言书面文体之间的数据交换.处理以及显示.        Unicode用两个字节表示一个字符.前127个字符与A ...

  5. 运用java集合Collections对List进行max和min操作

    我想创建了一个List,里面有一堆的数,一个需求是去掉一个最大值,一个最小值,List有remove的方法,但是找到最大值和最小值的方法不在List里,而是Collections的静态方法.真心觉得还 ...

  6. JavaWeb学习笔记——javabean与表单

  7. JavaWeb学习笔记——开发动态WEB资源(五)servlet身份验证

    本工程的功能是实现Javaweb的servlet身份验证 一下是login.html文件中的代码 <!DOCTYPE html> <html> <head> < ...

  8. 安卓、swiper标准的文字滚动

    溢出滚动基本原理还是relative,absolute.  核心高度  swiper-wrapper 和gun-swiper-slide 的自适应高度成为关键 <style> .gun-s ...

  9. MVC实现动态二级域名

    前段时间,一个朋友问我ASP.NET MVC下实现动态二级域名的问题.跟他聊了一些解决方案,这里也总结一下,以供参考. 相信大家都发现类似58同城这样的网站,成都的网址是cd.58.com 上海的是s ...

  10. Kafka——分布式消息系统

    Kafka——分布式消息系统 架构 Apache Kafka是2010年12月份开源的项目,采用scala语言编写,使用了多种效率优化机制,整体架构比较新颖(push/pull),更适合异构集群. 设 ...