Swift - UserNotifications框架使用详解1(基本介绍,权限的申请与判断)
一、UserNotifications 框架介绍
1,起源
- 过去我们通过 UILocalNotification 来实现本地消息的推送通知(Local Notification),或者利用 APNS 进行通知消息的远程推送(Remote Notification)。如果我们程序同时用到了本地通知和远程通知,会发现它们的 API 都被随意地放在了 UIApplication 或者 UIApplicationDelegate 中,开发时代码十分混乱。
- 到了 iOS10,苹果新增加了一个 UserNotifications.framework(用户通知框架),目的在于统一 Remote Notification(远程通知)和 Local Notification(本地通知)。过去那些杂乱的和通知相关的 API 都被统一,同时也新增了许多新功能。
2,新特性
UserNotifications 框架除了整合通知相关的 API,还增加了很多令人惊喜的特性,让我们实现许多过去没法实现的功能。- 更加丰富的推送内容:现在可以设置推送的 title、subtitle、body 以及符合大小的图片、音频、视频等附件内容。
- 更好的通知管理:过去已发出的通知不能更新。现在可以对通知进行查看、更新、删除了(哪怕是已展示通知)。
- 更优雅的展示方式:可以设置应用在前台展示通知,自定义通知 UI。
3,使用流程
UserNotifications 框架的使用大概分为以下几个过程:
- 申请、注册通知:首先需要向用户请求通知权限,在取得权限后注册通知。
- 创建、发送通知:然后创建一个通知并发起推送。对于远程推送 APNS 而言,还需要注册 DeviceToken。
- 展示、处理通知:在接收到推送通知后可以根据 app 的运行情况决定是否展示通知,当然也可以通过一系列的回调接口对通知进行处理加工。
二、通知权限说明
1,申请权限
(1)iOS 10 统一了推送权限的申请。不管是本地推送,还是远程推送,只需要 UNUserNotificationCenter.current().requestAuthorization() 方法申请即可。(这里我们在 AppDelegate 中申请通知权限。当然写在其它地方也是可以的。)
import UIKit import UserNotifications @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { //请求通知权限 UNUserNotificationCenter.current() .requestAuthorization(options: [.alert, .sound, .badge]) { (accepted, error) in if !accepted { print("用户不允许消息通知。") } } return true } func applicationWillResignActive(_ application: UIApplication) { } func applicationDidEnterBackground(_ application: UIApplication) { } func applicationWillEnterForeground(_ application: UIApplication) { } func applicationDidBecomeActive(_ application: UIApplication) { } func applicationWillTerminate(_ application: UIApplication) { } }
(2)当第一次调用上面这个方法时,系统会弹出如下窗口询问用户是否授权。

(3)如果用户拒绝了这个请求,再次调用该方法也不会再进行弹窗,同时也就无法收到通知。这种情况如果想要应用能接收到通知的话,只能让用户自行前往系统的设置中手动为你的应用打开通知了。因此在合适的时候弹出请求窗,并预先进行说明是很重要的。
2,判断权限
(1)在有些情况下,我们可以对推送权限设置进行检查。比如在检测到用户把通知权限关闭的时候,弹出个提示框引导用户去系统设置中打开通知权限。
比如下面代码,用户如果点击了“设置”按钮,则会自动跳转到通知设置页面,方便用户设置。



UNUserNotificationCenter.current().getNotificationSettings { settings in switch settings.authorizationStatus { case .authorized: return case .notDetermined: //请求授权 UNUserNotificationCenter.current() .requestAuthorization(options: [.alert, .sound, .badge]) { (accepted, error) in if !accepted { print("用户不允许消息通知。") } } case .denied: DispatchQueue.main.async(execute: { () -> Void in let alertController = UIAlertController(title: "消息推送已关闭", message: "想要及时获取消息。点击“设置”,开启通知。", preferredStyle: .alert) let cancelAction = UIAlertAction(title:"取消", style: .cancel, handler:nil) let settingsAction = UIAlertAction(title:"设置", style: .default, handler: { (action) -> Void in let url = URL(string: UIApplicationOpenSettingsURLString) if let url = url, UIApplication.shared.canOpenURL(url) { if #available(iOS 10, *) { UIApplication.shared.open(url, options: [:], completionHandler: { (success) in }) } else { UIApplication.shared.openURL(url) } } }) alertController.addAction(cancelAction) alertController.addAction(settingsAction) self.present(alertController, animated: true, completion: nil) }) } }
(2)除了打开和关闭全部通知权限外,用户也可以限制应用只能进行哪种形式的通知显示,比如:只允许横幅,而不允许声音及通知中心显示等。这些细微的设置,我们程序也是能检测到的。

UNUserNotificationCenter.current().getNotificationSettings { settings in var message = "是否允许通知:" switch settings.authorizationStatus { case .authorized: message.append("允许") case .notDetermined: message.append("未确定") case .denied: message.append("不允许") } message.append("\n声音:") switch settings.soundSetting{ case .enabled: message.append("开启") case .disabled: message.append("关闭") case .notSupported: message.append("不支持") } message.append("\n应用图标标记:") switch settings.badgeSetting{ case .enabled: message.append("开启") case .disabled: message.append("关闭") case .notSupported: message.append("不支持") } message.append("\n在锁定屏幕上显示:") switch settings.lockScreenSetting{ case .enabled: message.append("开启") case .disabled: message.append("关闭") case .notSupported: message.append("不支持") } message.append("\n在历史记录中显示:") switch settings.notificationCenterSetting{ case .enabled: message.append("开启") case .disabled: message.append("关闭") case .notSupported: message.append("不支持") } message.append("\n横幅显示:") switch settings.alertSetting{ case .enabled: message.append("开启") case .disabled: message.append("关闭") case .notSupported: message.append("不支持") } message.append("\n显示预览:") switch settings.showPreviewsSetting{ case .always: message.append("始终(默认)") case .whenAuthenticated: message.append("解锁时") case .never: message.append("从不") } print(message) }