RemoteNotificationHandling 0.0.3

RemoteNotificationHandling 0.0.3

测试测试
Lang语言 SwiftSwift
许可证 MIT
发布最近发布2018年9月
SPM支持 SPM

Yu Sugawara 维护。



RemoteNotificationHandling

RemoteNotificationHandling 执行远程通知所需的处理。灵感来自 PushNotificationHandler

功能

  • 获取 Apple 推送通知服务 (APNS) 的设备令牌
  • 针对 iOS 9 及以下版本的远程通知处理
  • 远程通知模型

安装

pod 'RemoteNotificationHandling'

使用方法

获取设备令牌

遵从协议

struct DeviceTokenHandler: RemoteNotificationDeviceTokenHandling {    
}

在UIApplicationDelegate中调用必要方法

extension AppDelegate { // RemoteNotificationDeviceTokenHandling
    func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
        RemoteNotificationManager.shared.deviceTokenHandler.application(application, didRegisterForRemoteNotificationsWithDeviceToken: deviceToken)
    }

    func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
        RemoteNotificationManager.shared.deviceTokenHandler.application(application, didFailToRegisterForRemoteNotificationsWithError: error)
    }
}

接收处理程序

回调函数
struct DeviceTokenHandler: RemoteNotificationDeviceTokenHandling {    
    func didRegisterForRemoteNotifications(result: DeviceTokenResult) {
        switch result {
        case .success(let deviceToken):
            print("Success deviceToken.string: \(deviceToken.string)")
        case .failure(let error):
            print("Failure error: \(error)")
        }
    }
}
Notification.name
extension Notification.Name {
    public struct RemoteNotificationDeviceTokenHandling {
        public static let didRegisterForRemoteNotifications = Notification.Name(rawValue: "RemoteNotificationDeviceTokenHandling.didRegisterForRemoteNotifications")
        public static let didFailToRegisterForRemoteNotifications = Notification.Name(rawValue: "RemoteNotificationDeviceTokenHandling.didFailToRegisterForRemoteNotifications")
    }    
}

接收iOS 10的远程通知

使用UNUserNotificationCenter

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    UNUserNotificationCenter.current().delegate = self
    return true
}
extension AppDelegate: UNUserNotificationCenterDelegate {
    func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Swift.Void) {
        print("Payload: \(Payload(notification)")
        completionHandler([.badge, .sound, .alert])
    }

    func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Swift.Void) {
        print("Payload: \(Payload(response)")
        completionHandler()
    }
}

接收iOS 9及以下版本的远程通知

遵守协议

class iOS9AndBelowRemoteNotificationHandler: iOS9AndBelowRemoteNotificationHandling {    
    let didReceiveRemoteNotificationHandlers = WeakContainer<iOS9AndBelowRemoteNotificationPayloadHandling>()
}

在UIApplicationDelegate中调用必需的方法

extension AppDelegate { // iOS9AndBelowRemoteNotificationHandling
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        RemoteNotificationManager.shared.iOS9AndBelowHandler?.application(application, didFinishLaunchingWithOptions: launchOptions)        
        return true
    }

    func application(_ application: UIApplication, didRegister notificationSettings: UIUserNotificationSettings) {
        RemoteNotificationManager.shared.iOS9AndBelowHandler?.application(application, didRegister: notificationSettings)
    }

    /// - note: Opening the app by tapping the icon will never give you information about previous notifications. It's only if you actually open the app via a notification that you will be able to access the notification data. (from [Stackoverflow](https://stackoverflow.com/a/13847840))
    func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) {
        RemoteNotificationManager.shared.iOS9AndBelowHandler?.application(application, didReceiveRemoteNotification: userInfo)
    }
}

接收到处理器

回调函数
iOS9AndBelowHandler.didReceiveRemoteNotificationHandlers.add(self)
extension ViewController: iOS9AndBelowRemoteNotificationPayloadHandling {
    func didReceiveRemoteNotificationPayload(_ payload: Payload) {
        print("Payload: \(payload)")
    }
}
Notification.name
extension Notification.Name {
    public struct RemoteNotificationHandling {
        @available(iOS, deprecated: 10.0)
        public static let didReceiveRemoteNotification = Notification.Name(rawValue: "RemoteNotificationHandling.didReceiveRemoteNotification")
    }
}

对应iOS方法表

iOS 10

应用状态 点击 启动选项 userNotificationCenter(_:willPresent:withCompletionHandler:) userNotificationCenter(_:didReceive:withCompletionHandler:)
未运行 应用图标 ✖️ ✖️ ✖️
未运行 横幅 ⭕️ ✖️ ⭕️
前台 - ✖️ ⭕️ ✖️
前台 横幅 ✖️ ✖️ ⭕️
后台 应用图标 ✖️ ✖️ ✖️
后台 横幅 ✖️ ✖️ ⭕️

iOS 9 及以下

应用状态 点击 启动选项 application(_:didReceiveRemoteNotification:)
未运行 应用图标 ✖️ ✖️
未运行 横幅 ⭕️ ✖️
前台 - ✖️ ⭕️
后台 应用图标 ✖️ ✖️
后台 横幅 ✖️ ⭕️

推荐调试库