iOS 10.0之前和之后的Local Notification有神马不同
来源:互联网 发布:云计算工程师要学什么 编辑:程序博客网 时间:2024/05/22 09:50
在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() }
0 0
- iOS 10.0之前和之后的Local Notification有神马不同
- ios local notification
- iOS Local Notification
- Notification和KVO有什么不同
- ios-ios 6之前和之后关于设备旋转的介绍
- ios-ios 7之前和之后关于状态栏隐藏的介绍
- iOS Local Notification学习笔记
- iOS Local Notification学习笔记
- iOS Local Notification学习笔记
- iOS Local Notification学习笔记
- iOS实现本地通知(local notification)的方法
- iOS 得到当前时间之前和之后N天的日期
- 想神马有神马
- iOS之Local Notification(本土通知)
- Push Notification、Local Notification、broadcast Notification的区别
- iOS开发之有趣的UI —— iOS8之前和之后的自定义不等高cell
- Android API Level在11前后及16之后时Notification的不同用法
- 在main()之前和之后调用自己的函数
- jquery 对象转为url参数
- logstash 多input与
- Hibernate查询、连接池、二级缓存
- io压缩流
- AFNetWorking 内存泄漏的问题 -- AFHTTPSessionManager --Instruments体检结果分享
- iOS 10.0之前和之后的Local Notification有神马不同
- hdu ---2039做题笔记
- java面向对象的三大特性的学习笔记
- 自定义微信公众号菜单一二级选项(二)
- 中文乱码问题(项目以UTF-8为例子)
- CC2530定时器配置笔记1(基本定时功能配置)
- The Dependency Inversion Principle
- 寄存器比内存访问速度快的原因
- Ajax 的POST请求...