最近在看《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. hdu 2007 - 平方和与立方和

    题目大意: 给定一段连续的整数,求出他们中所有偶数的平方和以及所有奇数的立方和. 解答: 坑你没商量!要考虑输入数a,b的大小.如果a>b,需要交换a,b的值. 1: #include<s ...

  2. Java 开发技巧

    一 读取配置文件 1 Properties读取配置文件 编写配置文件config.properties放在普通java工程的src目录(如果是maven工程就放在工程的src/main/resourc ...

  3. Docker distrubution in django

    https://www.syncano.io/blog/configuring-running-django-celery-docker-containers-pt-1/ Update: Fig ha ...

  4. DNS介绍

    DNS出现及演化 网络出现的早期 是使用IP地址通讯的,那时就几台主机通讯.但是随着接入网络主机的增多,这种数字标识的地址非常不便于记忆,UNIX上就出现了建立一个叫做hosts的文件(Linux和w ...

  5. 从Paxos到ZooKeeper-三、ZooKeeper的典型应用场景

    ZooKeeper是一个典型的发布/订阅模式的分布式数据管理与协调框架,开发人员可以使用它来进行分布式数据的发布与订阅.另一方面,通过对ZooKeeper中丰富的数据节点类型进行交叉使用,配合Watc ...

  6. java编程思想-复用类总结

    今天继续读<java 编程思想>,读到了复用类一章,看到总结写的很好,现贴上来,给大家分享. 继承和组合都能从现有类型生成新类型.组合一般是将现有类型作为新类型底层实现的一部分来加以复用, ...

  7. js021-Ajax与Comet

    js021-Ajax与Comet 本章内容: 使用XMLHttpRequet对象 使用XMLHttpRequet事件 跨域Ajax通信的限制 Ajax技术的核心是XMLHttpRequet对象 21. ...

  8. tomcat十大安全优化措施

    1.telnet管理端口保护 使用telnet连接进来可以输入SHUTDOWN可以直接关闭tomcat,极不安全,必须关闭.可以修改默认的管理端口8005改为其他端口,修改SHUTDOWN指令为其他字 ...

  9. photobooth.js

    HTML5 Webcam for your website, photobooth.js

  10. awk过滤数据

    awk -F ',' '{if($2 ~/\./ ) {print $1,$2 }}' 20160905_names > ttt1 awk -F ',' '{if($2 !~/[0-9]+\.[ ...