1. 程式人生 > >Swift 3.0 整合極光推送

Swift 3.0 整合極光推送

1.前言

推送證書配置什麼的都不多講了,極光推送的開發文件裡都有詳細的介紹極光推送文件,因為官方的文件是OC版本的,我這裡主要是講解一下怎麼用Swift進行整合。
本篇文章也可移步簡書閱覽,效果更好哦!

2.配置

現在一切都已經根據他們的文件配置好了,就剩下程式碼轉化了
第一步
在橋接檔案xx-Bridging-Header.h里加入以下程式碼

// 引入JPush功能所需標頭檔案
#import "JPUSHService.h"
// iOS10註冊APNs所需標頭檔案
#ifdef NSFoundationVersionNumber_iOS_9_x_Max
#import <UserNotifications/UserNotifications.h>
#endif // 如果需要使用idfa功能所需要引入的標頭檔案(可選) #import <AdSupport/AdSupport.h>

第二步

 func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

        //省略其它程式碼......

        //極光推送部署
        let entity = JPUSHRegisterEntity()
        entity.types = Int(JPAuthorizationOptions.alert.rawValue | JPAuthorizationOptions.sound.rawValue | JPAuthorizationOptions.badge.rawValue)

        JPUSHService.register(forRemoteNotificationConfig
: entity, delegate: self) JPUSHService.setup(withOption: launchOptions, appKey: "1cbb0b714502d6add4f412ff", channel: "Publish channel", apsForProduction: JPUSH_IS_PRO) JPUSHService.registrationIDCompletionHandler { (resCode, registrationID) in if resCode == 0{ PrintLog("registrationID獲取成功:\(registrationID)"
) }else { PrintLog("registrationID獲取失敗:\(registrationID)") } } return true } func applicationWillEnterForeground(_ application: UIApplication) { // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background. application.applicationIconBadgeNumber = 0 application.cancelAllLocalNotifications() } //MARK:-極光推送 AppDelegate擴充套件 extension AppDelegate:UNUserNotificationCenterDelegate,JPUSHRegisterDelegate { func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) { JPUSHService.registerDeviceToken(deviceToken) //設定tags,後臺可以根據這個來推送(本處用的是UserId) JPUSHService.setTags(["user"], aliasInbackground: UserHelper.getUserId()) PrintLog("deviceToken:\(deviceToken)") } func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) { PrintLog("did Fail To Register For Remote Notifications With Error:\(error)") } /** 收到靜默推送的回撥 @param application UIApplication 例項 @param userInfo 推送時指定的引數 @param completionHandler 完成回撥 */ func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) { JPUSHService.handleRemoteNotification(userInfo) PrintLog("iOS7及以上系統,收到通知:\(userInfo)") completionHandler(UIBackgroundFetchResult.newData) } func application(_ application: UIApplication, didReceive notification: UILocalNotification) { JPUSHService.showLocalNotification(atFront: notification, identifierKey: nil) } @available(iOS 10.0, *) func jpushNotificationCenter(_ center: UNUserNotificationCenter!, willPresent notification: UNNotification!, withCompletionHandler completionHandler: ((Int) -> Void)!) { let userInfo = notification.request.content.userInfo // let request = notification.request; // 收到推送的請求 // let content = request.content; // 收到推送的訊息內容 // // let badge = content.badge; // 推送訊息的角標 // let body = content.body; // 推送訊息體 // let sound = content.sound; // 推送訊息的聲音 // let subtitle = content.subtitle; // 推送訊息的副標題 // let title = content.title; // 推送訊息的標題 if (notification.request.trigger?.isKind(of: UNPushNotificationTrigger.self))!{ PrintLog("iOS10 前臺收到遠端通知:\(userInfo)") JPUSHService.handleRemoteNotification(userInfo) }else { // 判斷為本地通知 PrintLog("iOS10 前臺收到本地通知:\(userInfo)") } completionHandler(Int(UNAuthorizationOptions.alert.rawValue | UNAuthorizationOptions.sound.rawValue | UNAuthorizationOptions.badge.rawValue))// 需要執行這個方法,選擇是否提醒使用者,有badgesoundalert三種類型可以選擇設定 } @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))!{ PrintLog("iOS10 收到遠端通知:\(userInfo)") JPUSHService.handleRemoteNotification(userInfo) } completionHandler() } }

完成!

3.總結

本文只是簡單的集成了極光推送,親測可用,具體的細節型的功能沒有 去發掘,以後遇到了需求再進行更新,希望可以幫到你。
如果你發現第一次開啟後無法收到推送訊息,請參考【極光推送】第一次開啟app,收不到推送訊息