NotificationManager

public class NotificationManager : NSObject, UNUserNotificationCenterDelegate

通知管理器

Accessor

  • 单例模式

    Declaration

    Swift

    public static let shared: NotificationManager
  • 通知代理,设置后优先调用

    Declaration

    Swift

    public weak var delegate: UNUserNotificationCenterDelegate?

Authorize

  • 授权选项,默认[.badge, .sound, .alert]

    Declaration

    Swift

    public var authorizeOptions: UNAuthorizationOptions { get set }
  • 异步查询通知权限状态,当前线程回调

    Declaration

    Swift

    public func authorizeStatus(_ completion: ((AuthorizeStatus) -> Void)?)
  • 执行通知权限授权,主线程回调

    Declaration

    Swift

    public func requestAuthorize(_ completion: ((AuthorizeStatus) -> Void)?)

Badge

Handler

  • 设置远程推送处理句柄,参数为userInfo和原始通知对象

    Declaration

    Swift

    public var remoteNotificationHandler: (([AnyHashable : Any]?, Any) -> Void)?
  • 设置本地推送处理句柄,参数为userInfo和原始通知对象

    Declaration

    Swift

    public var localNotificationHandler: (([AnyHashable : Any]?, Any) -> Void)?
  • 注册通知处理器,iOS10+生效,iOS10以下详见UIApplicationDelegate

    Declaration

    Swift

    public func registerNotificationHandler()
  • 处理远程推送通知,支持NSDictionary|UNNotification|UNNotificationResponse

    Declaration

    Swift

    public func handleRemoteNotification(_ notification: Any)
  • 处理本地通知,支持NSDictionary|UNNotification|UNNotificationResponse

    Declaration

    Swift

    public func handleLocalNotification(_ notification: Any)

Local

  • 注册本地通知,badge为0时不改变,sound为default时为默认声音,timeInterval为触发时间间隔(0为立即触发),block为自定义内容句柄,iOS15+支持时效性通知,需entitlements配置开启

    Declaration

    Swift

    public func registerLocalNotification(_ identifier: String, title: String?, subtitle: String?, body: String?, userInfo: [AnyHashable : Any]?, badge: Int, sound: Any?, timeInterval: TimeInterval, repeats: Bool, block: ((UNMutableNotificationContent) -> Void)? = nil)
  • 批量删除本地通知(未发出和已发出)

    Declaration

    Swift

    public func removeLocalNotification(_ identifiers: [String])
  • 删除所有本地通知(未发出和已发出)

    Declaration

    Swift

    public func removeAllLocalNotifications()

UNUserNotificationCenterDelegate

  • 前台收到推送,notification.request.trigger为UNPushNotificationTrigger时为远程推送,否则本地推送

    Declaration

    Swift

    public func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void)
  • 后台收到推送,response.notification.request.trigger为UNPushNotificationTrigger时为远程推送,否则本地推送

    Declaration

    Swift

    public func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void)
  • 打开推送设置

    Declaration

    Swift

    public func userNotificationCenter(_ center: UNUserNotificationCenter, openSettingsFor notification: UNNotification?)