最近在看《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. [JavaEE]调用Restful Service 出现415 Unsupported Media Type的问题(Rest Request Header中的Content-Type问题)

    用Chrome的插件Simple REST Client 调用POST的REST服务时,老是报415错误,如图. 一开始就以为是服务端的问题,各种google,百度,折腾了一下午未果. 晚上继续看,一 ...

  2. 启动和关闭ADB服务(adb start-server和adb kill-server)

    1  Android SDK中的常用命令行工具 在<Android SDK安装目录>\tools目录中带了很多命令行工具.虽然一般的开发人员并不需要完全掌握这些工具的使用方法,但了解这些工 ...

  3. 如何用iframe标签以及Javascript制作时钟?

    如何利用iframe标签以及Javascript制作时钟? 如何制作一个时钟呢?效果如下图所示: 这里的时钟会不停的走,但是下面的页面是不会变得,这样就实现了我们想要的效果了.下面我将分为以下几个方面 ...

  4. linux第一面

    随着Linux应用的扩展许多朋友开始接触Linux,根据学习Windwos的经验往往有一些茫然的感觉:不知从何处开始学起.作为一个 Linux系统管理员,我看了许多有关Linux的文档和书籍,并为学习 ...

  5. O(1)快速乘注意事项

    O(1)快速乘是经典玄学优化啦~由于刚挂了一次特此总结一番. ll mul(ll u,ll v){ return(u*v-ll((long double)u*v/p)*p+p)%p; } double ...

  6. 会话控制:session与cookie

    我们在浏览网站时,访问的每一个web页面都需要使用"http协议"实现.而HTTP协议是无状态协议,就是说HTTP协议没有一个内建机制来维护两个事务之间的状态.当一个用户请求一个页 ...

  7. angularjs中$watch监听model(对象属性、对象)变化

    昨天看了一下教学视频,学到了有关$watch的用法,想到最近做的一个页面中有个select下拉选项(select中的值变化了,则后面input中的值也跟着相应的变化),不知是否可以使用$watch来代 ...

  8. 9月23日JavaScript作业----日期时间选择

    作业二:日期时间选择 <div style="width:600px; height:100px;"> <select id="year"&g ...

  9. 9月9日下午HTML样式表(宽度和高度、背景字体、对齐方式边界与边框)

    样式表 一.大小 1.width   宽度 2.height  高度 <div style="width:200px; height:200px"></div&g ...

  10. 一种nodejs的MVC框架

    mvc会针对请求进行分发,分发一般有controller(针对模块),action(针对模块中的方法),args(请求的参数). 1.先对http请求的url进行设置,解析url中的各种参数: //c ...