通知相关系列文章
iOS10 之前通知使用介绍
[iOS] 通知详解: UIUserNotification
iOS10 相关API
[iOS] 通知详解:iOS 10 UserNotifications API
iOS10 本地/远程通知
[iOS] 通知详解: iOS 10 UserNotifications
iOS10 通知附加包
[iOS] 通知详解: iOS 10 UserNotifications – 附加包Media Attachments
iOS10 自定义UI
[iOS] 通知详解: iOS 10 UserNotifications – 自定义通知UI
本文主要是介绍iOS10之前的推送处理相关API及示例,很多文章都是将iOS10之前的API和iOS10新出的一起介绍,个人感觉比较混乱,而且不够清晰,所以才重新总结了一些文章并结合自己的使用经验,将 iOS10 之前的使用和iOS10之后的进行归纳。
通知权限请求
不管是本地通知,还是远程通知,都需要向用户申请通知的权限
UIUserNotificationSettings (iOS 8 ---- iOS 10)
UIUserNotificationSettings 主要由一个初始化方法来完成配置:
// types:通知的类型 见 UIUserNotificationType
// categories:自定义行为按钮,见 UIUserNotificationCategory,可传nil
public convenience init(types: UIUserNotificationType, categories: Set<UIUserNotificationCategory>?)
UIUserNotificationType 通知类型
public struct UIUserNotificationType : OptionSet {
public init(rawValue: UInt)
// 角标
public static var badge: UIUserNotificationType {
get }
// 声音
public static var sound: UIUserNotificationType {
get } // the application may play a sound upon a notification being received
// 弹框
public static var alert: UIUserNotificationType {
get } // the application may display an alert upon a notification being received
}
一般使用(不带有动作按钮)
- 注册通知设置
// categories 传nil,则通知没有额外的动作按钮
let setting = UIUserNotificationSettings(types: [.sound, .alert, .badge], categories: nil)
UIApplication.shared.registerUserNotificationSettings(setting)
registerUserNotificationSettings 方法调用后,成功后会以代理的方式进行反馈
// registerUserNotificationSettings 回调代理
func application(_ application: UIApplication, didRegister notificationSettings: UIUserNotificationSettings) {
// 注册 deviceToken
application.registerForRemoteNotifications()
}
这里我们需要调用registerForRemoteNotifications,来注册通知,获取deviceToken,也会以代理的方式进行反馈
// registerForRemoteNotifications 成功的回调,获取到token
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
let token = deviceToken.map {
String(format: "%02hhx", $0) }.joined()
// Next do ...
// 一般是发送给自己的服务后台,或者第三方的推送服务器后台
}
// registerForRemoteNotifications 失败的回调
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
print(error)
}
到此,通知的权限申请及远程注册就完成了,接下来就是发送通知,本地或者远程。
此时,通知的弹框是没有其他动作按钮的,弹框下拉后,只有消息标题,内容等信息,没有额外的动作按钮

带有动作选择的通知
带有动作的通知,需要设置其 categories ,涉及到以下几个类的配置
UIUserNotificationCategory
携带动作的Category,一般是使用其可变实例 UIMutableUserNotificationCategory来添加多个行为:
// 唯一标识符
open var identifier: String?
// Sets the UIUserNotificationActions in the order to be displayed for the specified context
open func setActions(_ actions: [UIUserNotificationAction]?, for context: UIUserNotificationActionContext)
UIUserNotificationActions
动作实例,一般是使用其可变实例 UIMutableUserNotificationAction 来配置更多的行为
// 唯一标识符
open var identifier: String?
// 按钮 title
open var title: String?
// 用户点击该按钮时的行为,有两种
/*public enum UIUserNotificationActionBehavior : UInt {
// 一般的点击事件
case `default` // the default action behavior
// 点击按钮后会弹出输入框,可以输入一段文字快捷回复
case textInput // system provided action behavior, allows text input from the user
}
*/
// The behavior of this action when the user activates it.
@available(iOS 9.0, *)
open var behavior: UIUserNotificationActionBehavior
// Parameters that can be used by some types of actions.
@available(iOS 9.0, *)
open var parameters: [AnyHashable : Any]
// 按钮响应事件的模式,有两种
/*public enum UIUserNotificationActivationMode : UInt {
// 在该模式下,点击按钮后会打开app,可以在代理方法中做一些跳转操作
case foreground // activates the application in the foreground
// 在该模式下,点击后不会打开app,可以后台做一些操作
case background // activates the application in the background, unless it's already in the foreground
}
*/
// How the application should be activated in response to the action.
open var activationMode: UIUserNotificationActivationMode
// 是否需要授权
// Whether this action is secure and should require unlocking before being performed. If the activation mode is UIUserNotificationActivationModeForeground, then the action is considered secure and this property is ignored.
open var isAuthenticationRequired: Bool
// 当需要执行某些破坏性的操作时,需要醒目提醒,红色的按钮
// Whether this action should be indicated as destructive when displayed.
open var isDestructive: Bool
示例:
let ok = UIMutableUserNotificationAction()
ok.identifier = "okidentifier"
ok.activationMode = . foreground // 这里需要打开app,所以设置为foreground模式
ok.title

最低0.47元/天 解锁文章

被折叠的 条评论
为什么被折叠?



