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 ...
随机推荐
- Yocto开发笔记之《驱动调试-GPS数据采集》(QQ交流群:519230208)
开了一个交流群,欢迎爱好者和开发者一起交流,转载请注明出处. QQ群:519230208,为避免广告骚扰,申请时请注明 “开发者” 字样 =============================== ...
- Linux bash 中,如何将正常信息和错误信息输出到文件
问题描述: $ command 2>> error $ command 1>> output 是否有方法,在bash同一行,实现输出stderr到error文件,输出stdou ...
- C#中值类型和引用类型图解
举几个值类型和引用类型的内存配置: 值类型存储在栈中,引用类型堆里: 1,数组 数组是引用类型,但是数组的元素可以是值类型或引用类型 2. 结构 结构是值类型,简略的看个例子 struct sampl ...
- js014-表单脚本
js014-表单脚本 本章内容: 理解表单 文本框验证与交互 使用其他表单控制 14.1 表单的基础知识 在HTML中,表单时由<form>元素来表示的,在JS中,表单对应的时HTMLFo ...
- easyUI-combobox 后台导入Json数据的方法
一.前台页面: <input id="List" class="easyui-combobox" data-options="valueFiel ...
- CentOS7安装Nginx并部署
服务器IP是192.168.36.136 1.直接yum install nginx即可 2.主配置文件是/etc/nginx/下的nginx.conf,另外一个是/etc/nginx/conf.d/ ...
- Java观察者设计模式
在java.util包中提供了Observable类和Observer接口,使用它们即可完成观察者模式. 多个观察者都在关注着价格的变化,只要价格一有变化,则所有的观察者会立即有所行动. //==== ...
- CodeForces 689B Mike and Shortcuts (BFS or 最短路)
题目链接:http://codeforces.com/problemset/problem/689/B 题目大意: 留坑 明天中秋~
- Jquerymobile 简单安装
需要导入三个文件jquery,jquerymobile,css(jquerymobile地址:http://jquerymobile.com/) <script src="js/jqu ...
- Redis学习——ae事件处理源码分析
0. 前言 Redis在封装事件的处理采用了Reactor模式,添加了定时事件的处理.Redis处理事件是单进程单线程的,而经典Reator模式对事件是串行处理的.即如果有一个事件阻塞过久的话会导致整 ...