iOS 10.0之前和之后的Local Notification有神马不同
在iOS 10.0之前apple还没有将通知功能单独拿出来自成一系.而从10.0开始原来的本地通知仍然可用,只是被标记为过时.于是乎我们可以使用10.0全新的通知功能.别急…让我们慢慢来,先从iOS 10.0之前的本地通知讲起吧 ;)
这里不会面面俱到,因为不是面向初学者.如果你有一定的iOS开发经验相信可以很快掌握知识要点.如果是初学者也没关系,你可以在本篇blog后面直接提问,如有时间我会为你解答.
iOS < 10.0
我们首先要取得访问权限:
let notificationSettings = UIUserNotificationSettings(types: [.alert,.badge,.sound], categories: nil)
UIApplication.shared.registerUserNotificationSettings(notificationSettings)
接下来是发送本地通知:
@IBAction func scheduleLocal(_ sender: AnyObject) {
//如果取不到当前通知的设置数据则直接退出
guard let settings = UIApplication.shared.currentUserNotificationSettings else{
return
}
//如果用户权限为.none则提示用户未授权
if settings.types == UIUserNotificationType(rawValue: 0){
let ac = UIAlertController(title: "Can't schedule", message: "Either we don't have permission to schedule notifications, or we haven't asked yet.", preferredStyle: .alert)
ac.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
present(ac, animated: true, completion: nil)
return
}
//开始设置本地通知
let notification = UILocalNotification()
notification.fireDate = Date(timeIntervalSinceNow: 20)
notification.alertBody = "Hey you! Yeah you! Swipe to unlock!"
notification.alertAction = "be awesome!"
notification.soundName = UILocalNotificationDefaultSoundName
notification.userInfo = ["CustomField1":"w00t"]
//发送本地通知
UIApplication.shared.scheduleLocalNotification(notification)
}
最后是本地通知的接收,这分为两种情况:
- 通知到达时App未退出
- 通知到达时App已退出
对于第一种情况我们实现以下方法来接收处理通知:
func application(_ application: UIApplication, didReceive notification: UILocalNotification) {
if let userInfo = notification.userInfo{
let customField1 = userInfo["CustomField1"] as! String
print("\(#function) recive a notification: customField1 is \(customField1)")
}
}
而对于后者我们在didFinishLaunchingWithOptions方法中处理通知:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
if let options = launchOptions{
if let notification = options[UIApplicationLaunchOptionsKey.localNotification] as? UILocalNotification{
if let userInfo = notification.userInfo{
let customField1 = userInfo["CustomField1"] as! String
print("when App did launch,recive a noficition:customField1 is \(customField1)")
//do anything you want!!!
}
}
}
return true
}
iOS >= 10.0
在iOS 10.0中apple为通知添加了单独的框架UserNotifications,一切通知都要围绕它打交道哦.
首先同样是询问用户访问权限:
@IBAction func regNotification(_ sender: AnyObject) {
if #available(iOS 10.0, *){
let center = UNUserNotificationCenter.current()
center.getNotificationSettings {settings in
print(settings)
}
center.requestAuthorization(options: [.alert,.sound]) {(granted,error) in
if let err = error{
self.msg(title: "Error", string: "本地通知授权时发生了错误:\(err.localizedDescription)")
}else if !granted{
self.msg(title: "Error", string: "你没有获得本地通知的许可")
}
}
}
}
接着你需要设置通知中心的委托,我把它放在didFinishLaunchingWithOptions方法中:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
let center = UNUserNotificationCenter.current()
center.delegate = self
return true
}
注意这里didFinishLaunchingWithOptions方法已不再处理本地通知,在iOS 10.0中所有的通知接收处理都放在一个地方,无论App是运行还是退出,这个后面会说到.
接下来是发送本地通知:
@IBAction func sendNotification(_ sender: AnyObject) {
let center = UNUserNotificationCenter.current()
let content = UNMutableNotificationContent()
content.title = "Hello!"
content.body = "你好世界!!!"
content.sound = UNNotificationSound.default()
content.userInfo = ["key0":"value0"]
//content.badge = NSNumber(value: UIApplication.shared.applicationIconBadgeNumber + 1)
content.categoryIdentifier = "com.hopy.localNotification"
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5.0, repeats: false)
let request = UNNotificationRequest(identifier: "FiveSecond", content: content, trigger: trigger)
center.add(request){[unowned self] error in
if let err = error{
self.msg(title: "ERROR", string: "NotificationCenter add N failed:\(err.localizedDescription)")
}else{
//do anything you want!
}
}
}
最后是本地通知的接收,我们为AppDelegate添加UNUserNotificationCenterDelegate协议,然后实现如下方法:
//在展示通知前进行处理,有机会在展示通知前再修改通知内容
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
//do any stuff!!
completionHandler(UNNotificationPresentationOptions.alert)
}
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
let notification = response.notification
notificationInfo = notification.request.content.userInfo
//do any stuff!!!
completionHandler()
}
iOS 10.0之前和之后的Local Notification有神马不同的更多相关文章
- iOS 10.0 更新点(开发者视角)
html, body {overflow-x: initial !important;}html { font-size: 14px; } body { margin: 0px; padding: 0 ...
- 在 iOS 10.0 之后, App 要调用手机相机与相簿应注意的事项
iOS 的 SDK 每一年至少都会有一次大改版,从 2009 到 2016 年,版号已经到了第 10 版了,很轻易的就追上了 Mac OSX. 每一次的大改版都会有不少新的功能或新的规范,在 iOS ...
- iOS 10.0前的Notification推送
前言 推送为远程推送,一般由苹果APNS服务器发送给苹果设备(iPhone,iPad) 推送分在前台和后台.在前台时 用户可以在application 的代理回调接口中做相应处理:在后台时 系统会全权 ...
- iOS 10.0适配之旅
1.升级Xcode体验 升级到Xcode之后,调试程序好多东西都不是太适应 控制台莫名给你打印一堆不是太好理解的东西 之前使用 Alcatraz 下载的插件都不能用(如何使用Alcatraz) 打开麦 ...
- 关于iOS10 Xcode8真机测试项目出现的问题 "code signing is required for product type 'xxxxx' in SDK 'iOS 10.0"..
昨天用真机测试项目出现这样的错误,在网上搜集了一些信息,所以将自己的经验分享出来帮助更多的人. 第一步: 检查你的1和2是否填写正确,如果你是运行别人的项目,BundleIdentifier要和你的X ...
- [转载]iOS 10 UserNotifications 框架解析
活久见的重构 - iOS 10 UserNotifications 框架解析 TL;DR iOS 10 中以前杂乱的和通知相关的 API 都被统一了,现在开发者可以使用独立的 UserNotifica ...
- iOS 10 UserNotifications 框架解析
摘自:https://onevcat.com/2016/08/notification/ iOS 10 中以前杂乱的和通知相关的 API 都被统一了,现在开发者可以使用独立的 UserNotifica ...
- 第三十二篇、iOS 10开发
1.语音识别 苹果官方在文档中新增了API Speech,那么在以前我们处理语音识别非常的繁琐甚至很多时候可能需要借助于第三方框架处理,那么苹果推出了这个后,我们以后处理起来就非常的方便了,spe ...
- iOS 10、Xcode 8 遇到部分问题解决记录
今天把iphone 6 升级到ios10 后,用Xcode 7进行真机调试的时候提示: Could not find Developer Disk Image 果断准备升级到Xcode 8 .但是想保 ...
随机推荐
- 八:Vue下的国际化处理
p { margin-bottom: 0.25cm; line-height: 120% } 1:首先安装 Vue-i8n npm install vue-i18n --save 注:-save-de ...
- spring源码阅读(1)bean解析
public class Test { public static void main(String[] args) throws Exception { BeanFactory beanFactor ...
- [AHOI 2012]树屋阶梯
Description 暑假期间,小龙报名了一个模拟野外生存作战训练班来锻炼体魄,训练的第一个晚上,教官就给他们出了个难题.由于地上露营湿气重,必须选择在高处的树屋露营.小龙分配的树屋建立在一颗高度为 ...
- 51nod 1364 最大字典序排列(线段树)
1364 最大字典序排列基准时间限制:1 秒 空间限制:131072 KB 分值: 80 难度:5级算法题 给出一个1至N的排列,允许你做不超过K次操作,每次操作可以将相邻的两个数交换,问能够得到的字 ...
- [BZOJ]1011 遥远的行星(HNOI2008)
由eps引发的血案. Description 直线上N颗行星,X=i处有行星i,行星J受到行星I的作用力,当且仅当i<=A*J.此时J受到作用力的大小为 Fi->j=Mi*Mj/(j-i) ...
- bzoj1069 [SCOI2007]最大土地面积 旋转卡壳
1069: [SCOI2007]最大土地面积 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 3767 Solved: 1501[Submit][Sta ...
- qemu 模拟-arm-mini2440开发板-启动u-boot,kernel和nfs文件系统【转】
转自:http://www.cnblogs.com/riskyer/p/3366001.html qemu 本文介绍了如何编译u-boot.linux kernel,然后用qemu启动u-boot和l ...
- OCP 认证考试报名费技巧题库051052053解析合格线
本人于2017年4月22日通过参加OCP考试,第一次参加,一天之内考了三门,三门一次性通过,052 - 95% ,053 - 86% ,051 - 100% 一.关于考试考试报名费: 052:158$ ...
- Thinkphp中的 I 函数(Thinkphp3.2.3版本)
I 函数的作用是获取系统变量,必要时还可以对变量值进行过滤及强制转化,I 函数的语法格式: I('变量类型.变量名/修饰符',['默认值'],['过滤方法或正则'],['额外数据源']) 一.获取变量 ...
- Linux中/etc/fstab /etc/mtab /proc/mounts这三个文件的分析与比较 分区表位置
本文主要讲解Linux中/etc/fstab /etc/mtab /proc/mounts这三个文件的作用以及不同之处. 转自http://haohaozhang.blog.51cto.com/917 ...