因为要适配iOS 10以下的系统,所以在配置极光推送时要做一些处理。
先上代码,等会儿慢慢说,或者有问题可以在下面留言。当然我写的也有不标准的地方,还请各位多多指教。
如下代码
import UIKit
//iOS 系统版本号判断
let iOS_Version:Float = Float.init(UIDevice.current.systemVersion)!
let iOS10 = (iOS_Version >= 10.0)
let iOS8 = (iOS_Version >= 8.0)
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate{
var window: UIWindow?
let testValue = true
private let PushKey = "xxx"
private let channel = "App Store"
private let isProduction = false
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
self.window = UIWindow.init(frame: UIScreen.main.bounds)
self.window?.backgroundColor = UIColor.white
self.window?.makeKeyAndVisible()
self.registerForAPNs(launchOptions)
self.rootVCAndLoginVCSettings()
return true
}
func rootVCAndLoginVCSettings() {
let loginViewController = LoginViewController()
let rootViewController = RootViewController()
let navigationController = UINavigationController()
navigationController.pushViewController(rootViewController, animated: true)
self.window?.rootViewController = self.testValue ? navigationController:loginViewController
}
//MARK:配置极光推送
func registerForAPNs(_ launchOptions: [UIApplicationLaunchOptionsKey: Any]?) {
//适配系统版本
if #available(iOS 10.0, *) {
let entity = JPUSHRegisterEntity.init()
entity.types = Int(Double(JPAuthorizationOptions.alert.rawValue) + TimeInterval(JPAuthorizationOptions.badge.rawValue) + Double(JPAuthorizationOptions.sound.rawValue));
JPUSHService.register(forRemoteNotificationConfig: entity, delegate: self as JPUSHRegisterDelegate)
}else if #available(iOS 8.0, *){
JPUSHService.register(forRemoteNotificationTypes: UInt(Double(UIUserNotificationType.alert.rawValue) + TimeInterval(UIUserNotificationType.badge.rawValue) + Double(UIUserNotificationType.sound.rawValue)),
categories: nil)
}else{
let type = UIRemoteNotificationType.badge.rawValue |
UIRemoteNotificationType.sound.rawValue |
UIRemoteNotificationType.alert.rawValue
JPUSHService.register(forRemoteNotificationTypes: type, categories: nil)
}
JPUSHService.setup(withOption: launchOptions,
appKey: PushKey,
channel: channel,
apsForProduction: isProduction)
}
public func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
JPUSHService.registerDeviceToken(deviceToken)
}
public func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
print("Error:\(error)")
}
public func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
JPUSHService.handleRemoteNotification(userInfo)
completionHandler(UIBackgroundFetchResult.newData)
}
public func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) {
JPUSHService.handleRemoteNotification(userInfo)
}
}
extension AppDelegate : JPUSHRegisterDelegate{
@available(iOS 10.0, *)
func jpushNotificationCenter(_ center: UNUserNotificationCenter!, willPresent notification: UNNotification!, withCompletionHandler completionHandler: ((Int) -> Void)!) {
let userInfo = notification.request.content.userInfo
if (notification.request.trigger?.isKind(of: UNPushNotificationTrigger.self))!{
JPUSHService.handleRemoteNotification(userInfo)
}
completionHandler(Int(UNAuthorizationOptions.alert.rawValue))// 需要执行这个方法,选择是否提醒用户,有Badge、Sound、Alert三种类型可以选择设置
}
@available(iOS 10.0, *)
func jpushNotificationCenter(_ center: UNUserNotificationCenter!, didReceive response: UNNotificationResponse!, withCompletionHandler completionHandler: (() -> Void)!) {
let userInfo = response.notification.request.content.userInfo
if (response.notification.request.trigger?.isKind(of: UNPushNotificationTrigger.self))!{
JPUSHService.handleRemoteNotification(userInfo)
}
completionHandler()
}
}
在上面的代码中,替换AppKey之后,就能直接使用了。
我们不能直接采用class AppDelegate: UIResponder, UIApplicationDelegate,JPUSHRegisterDelegate{
的方式设置极光推送的代理,因为里面的代理方法是基于iOS 10 SDK
的,直接导入,在在低版本SDK
中就会编译不通过。所以可以稍微做一下处理,采用上面代码中的方式,对AppDelegate
进行扩展,这样就解决了问题。