iOS10本地通知的探索
来源:互联网 发布:网络兼职项目 编辑:程序博客网 时间:2024/04/30 09:58
iOS10本地通知的探索
2016WWDC会上,苹果在iOS10中更新了新的通知框架(UserNotifications.framework、UserNotificationsUI.framework),今天就去探索一下,尝尝鲜。
我的探索环境采用的是Xcode8.0Beta版本,语言是Swift3.0。我会通过以前通知的使用情况和iOS10中的做出对比来介绍。Swift3.0语法变化可以参考这篇文章
一、要使用通知,首先要注册通知
1、旧版本注册本地通知的方式
// 注册通知let settings = UIUserNotificationSettings.init(types: [UIUserNotificationType.alert , UIUserNotificationType.sound , UIUserNotificationType.badge], categories: nil)UIApplication.shared().registerUserNotificationSettings(settings)2、在配置旧版通知显示信息的时候,Xcode会提示UIUserNotificationSettings已经废弃了,需要使用UNNotificationSettings来配置,但是其进去查看这个类的定义,发现并没有对应的初始化方法,并且参数都是只有get方法。无法使用,所以这里注册通知还是使用旧版的方式。
下面是UNNotificationSettings类额定义信息:
public class UNNotificationSettings : NSObject, NSCopying, NSSecureCoding { public var authorizationStatus: UNAuthorizationStatus { get } public var soundSetting: UNNotificationSetting { get } public var badgeSetting: UNNotificationSetting { get } public var alertSetting: UNNotificationSetting { get } public var notificationCenterSetting: UNNotificationSetting { get } public var lockScreenSetting: UNNotificationSetting { get } public var carPlaySetting: UNNotificationSetting { get } public var alertStyle: UNAlertStyle { get }}
二、创建通知
1、旧版通知的创建方式 --> 在使用版本API的时候,Xcode会报警告,点击进去查看新的API是一种学习新技术的方式
// 创建通知 private func createLocalNotification() { // 创建通知 let local = UILocalNotification() // 设置一些参数 local.alertBody = "alertBody" local.soundName = UILocalNotificationDefaultSoundName local.applicationIconBadgeNumber = 1 // 设置3秒钟后通知 local.fireDate = NSDate.init(timeIntervalSinceNow: 3) as Date local.userInfo = ["key": "value"] UIApplication.shared().scheduleLocalNotification(local) }2、使用新通知API,需要导入import UserNotifications框架
// UNNotificationContent 的子类 UNMutableNotificationContent let content = UNMutableNotificationContent() content.sound = UNNotificationSound.default() content.badge = 10 content.body = "通知内容" content.title = "标题" content.subtitle = "子标题" content.userInfo = ["key": "value"] // 需要使用UNNotificationTrigger的子类UNTimeIntervalNotificationTrigger来实例化 let trigger = UNTimeIntervalNotificationTrigger.init(timeInterval: 3, repeats: false) let request = UNNotificationRequest.init(identifier: "youIdentifier", content: content, trigger: trigger) let center = UNUserNotificationCenter.current() // 在代理里面接收通知信息 center.delegate = self center.add(request, withCompletionHandler: nil)三、接收通知信息
1、接收旧版本地通知的信息需要在UIApplicationDelegate的代理方法里面接收
func application(_ application: UIApplication, handleActionWithIdentifier identifier: String?, for notification: UILocalNotification, completionHandler: () -> Void) { }2、新通知框架提供了一个新的代理来处理这种情况UNUserNotificationCenterDelegate,需要在创建的时候设置代理,下面是对应的两个代理方法
extension ViewController: UNUserNotificationCenterDelegate { func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: () -> Void) { let content = response.notification.request.content print("userInfo = \(content.userInfo), subtitle = \(content.subtitle), title = \(content.title) ,body = \(content.body) ,badge = \(content.badge) ,sound = \(content.sound)") } func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: (UNNotificationPresentationOptions) -> Void) { print("willPresent") }}四、删除通知
通知的删除可以通过特定标识删除单个通知,也可以删除全部的通知
1、旧版通知的删除
// 删除通知 private func deleteLocalNotification() { // 获取所有本地通知 let localNotificationsArray = UIApplication.shared().scheduledLocalNotifications guard !localNotificationsArray!.isEmpty else { print("没有本地通知") return } // 获取本地通知并且删除 for item in localNotificationsArray! { print("item =\(item)") // 移除单个通知,可以根据userinfo设置的标识删除指定通知 UIApplication.shared().cancelLocalNotification(item) } // 删除所有本地通知// UIApplication.shared().cancelAllLocalNotifications() }2、新框架通知的删除
// 删除通知 private func deleteLocalNotification() { let center = UNUserNotificationCenter.current() // UNUserNotificationCenter getPendingNotificationRequestsWithCompletionHandler: center.getPendingNotificationRequests { (requestArray :[UNNotificationRequest]) in print("requestArray = \(requestArray)") for item in requestArray { // 根据identifiers移除指定通知 let identifiers = item.identifier if identifiers == "youIdentifier" { // 跟创建的时候对应 center.removeDeliveredNotifications(withIdentifiers: [identifiers]) } } // 或者移除全部通知 center.removeAllPendingNotificationRequests() } }
五、iOS10上面本地通知的展示效果
0 0
- iOS10本地通知的探索
- iOS10 本地通知
- iOS10 本地通知
- iOS10 本地通知
- iOS10推送 本地通知
- ios10之后本地通知
- iOS10 本地通知
- ios-ios10设置本地通知
- iOS10通知(五)--本地实现多媒体通知
- iOS10本地通知UserNotifications快速入门
- iOS10本地通知UserNotifications快速入门
- ios10下的通知更新
- 模仿iOS10的下拉通知
- iOS10更新_本地通知新框架UserNotifications
- iOS10--消息通知的基本使用
- iOS10里的通知与推送
- iOS10里的通知与推送
- iOS10--消息通知的基本使用
- PHP填坑记之FastCGI与mod_php详解
- Bash引用变量和转义
- Eclipse最实用快捷键
- Java学习笔记一:逻辑运算符
- AtomicInteger简介
- iOS10本地通知的探索
- 碎碎念5
- python 中的三引号 ''' '''
- android如何改变listview中item选中时文字的颜色
- 用MATLAB生成MIF文件
- Maven 3 简易教程
- Retrofit2的基本用法
- 剑指offer面试题20:顺时针打印矩阵(自己的算法,不是书上的)
- LeetCode 371. Sum of Two Integers不用加减实现加法