博客> swift极光推送
swift极光推送
2017-12-11 12:18 评论:0 阅读:379 bboy阿林
ios 极光推送

下面是swift极光推送关键代码,用于参考

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?  

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {  
    if #available(iOS 10.0, *){  
        let entiity = JPUSHRegisterEntity()  
        entiity.types = Int(UNAuthorizationOptions.alert.rawValue |  
                            UNAuthorizationOptions.badge.rawValue |  
                            UNAuthorizationOptions.sound.rawValue)  
        JPUSHService.register(forRemoteNotificationConfig: entiity, delegate: self)  
    } else if #available(iOS 8.0, *) {  
        let types = UIUserNotificationType.badge.rawValue |  
                    UIUserNotificationType.sound.rawValue |  
                    UIUserNotificationType.alert.rawValue  
        JPUSHService.register(forRemoteNotificationTypes: types, 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: "4adfb75ea2e6b055ccb04891",  
                       channel: "app store",  
                       apsForProduction: false)  
    return true  
}  

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {  
    JPUSHService.registerDeviceToken(deviceToken)  
}  

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {  
    JPUSHService.handleRemoteNotification(userInfo)  
    completionHandler(.newData)  

}  

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {  
    JPUSHService.handleRemoteNotification(userInfo)  
}  

}

extension AppDelegate : JPUSHRegisterDelegate{
func jpushNotificationCenter(_ center: UNUserNotificationCenter!, willPresent notification: UNNotification!, withCompletionHandler completionHandler: ((Int) -> Void)!) {
print(">JPUSHRegisterDelegate jpushNotificationCenter willPresent");
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三种类型可以选择设置
}

func jpushNotificationCenter(_ center: UNUserNotificationCenter!, didReceive response: UNNotificationResponse!, withCompletionHandler completionHandler: (() -> Void)!) {  
    print(">JPUSHRegisterDelegate jpushNotificationCenter didReceive");  
    let userInfo = response.notification.request.content.userInfo  
    if (response.notification.request.trigger?.isKind(of: UNPushNotificationTrigger.self))!{  
        JPUSHService.handleRemoteNotification(userInfo)  
    }  
    completionHandler()  
}  

}

收藏
0
sina weixin mail 回到顶部