您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 使用技巧 >> iOS 技巧大全

Swift 3.0 接入極光推送完整實戰

編輯:iOS 技巧大全
[摘要]本文記錄在 Swift 3.0 項目中集成極光推送(JPush)的完整過程,涵蓋橋接頭文件配置、AppDelegate 代理聲明、推送註冊、iOS 10 通知代理方法實現,以及應用未啟動時收到推送的處理方式。

Swift 已經成為越來越多 iOS 項目的主力語言,但客觀說,市面上大多數第三方 SDK 仍是用 Objective-C 編寫的,因此 Swift 與 OC 混編幾乎是必經之路。本文以 Swift 3.0 集成極光推送為例,把整個過程走一遍。

1. 準備工作

極光官方文檔對集成步驟說明得已經很清楚了:先在官網創建應用、下載 SDK、完成工程配置。核心一步是在橋接頭文件(Bridging Header)中引入相應頭文件:

#import "JPUSHService.h"

// iOS 10 註冊 APNs 所需頭文件
#ifdef NSFoundationVersionNumber_iOS_9_x_Max
#import <UserNotifications/UserNotifications.h>
#endif

2. Swift 3.0 集成步驟

(1)在 AppDelegate.swift 中添加代理

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, JPUSHRegisterDelegate {
    // ...
}

(2)註冊推送,並處理應用未啟動時收到的推送消息

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

    // 通知註冊實體類
    let entity = JPUSHRegisterEntity()
    entity.types = Int(JPAuthorizationOptions.alert.rawValue)
        | Int(JPAuthorizationOptions.sound.rawValue)
        | Int(JPAuthorizationOptions.badge.rawValue)
    JPUSHService.register(forRemoteNotificationConfig: entity, delegate: self)

    // 註冊極光推送
    JPUSHService.setup(withOption: launchOptions,
                       appKey: "845b93e08c7fa192df019c07",
                       channel: "Publish channel",
                       apsForProduction: false)

    // 獲取推送消息
    let remote = launchOptions?[UIApplicationLaunchOptionsKey.remoteNotification]
        as? Dictionary<String, Any>

    // 如果 remote 不為空,說明應用在未啟動狀態下就收到了推送
    if remote != nil {
        self.perform(#selector(receivePush), with: remote, afterDelay: 1.0)
    }
    return true
}

(3)實現代理方法

// MARK: - JPUSHRegisterDelegate

// iOS 10.x:應用在前台時收到推送
@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 is UNPushNotificationTrigger {
        JPUSHService.handleRemoteNotification(userInfo)
    }
    completionHandler(Int(UNNotificationPresentationOptions.alert.rawValue))
}

// iOS 10.x:用戶點擊推送
@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 is UNPushNotificationTrigger {
        JPUSHService.handleRemoteNotification(userInfo)
    }
    completionHandler()

    // 應用已打開的情況下收到推送
    NotificationCenter.default.post(
        name: NSNotification.Name(rawValue: NotificationName_ReceivePush),
        object: NotificationObject_Sueecess,
        userInfo: userInfo)
}

// 兼容舊系統的靜默推送回調
func application(_ application: UIApplication,
                 didReceiveRemoteNotification userInfo: [AnyHashable: Any],
                 fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
    JPUSHService.handleRemoteNotification(userInfo)
    completionHandler(UIBackgroundFetchResult.newData)
}

// 接收到推送後的統一處理
func receivePush(_ userInfo: Dictionary<String, Any>) {
    // 角標歸零
    UIApplication.shared.applicationIconBadgeNumber = 0
    // 其餘邏輯按業務需求自定義
    self.tabBarVC?.selectedIndex = 0
    NotificationCenter.default.post(
        name: NSNotification.Name(rawValue: NotificationName_ReceivePush),
        object: NotificationObject_Sueecess,
        userInfo: userInfo)
}

網上關於 Swift 3.0 集成極光推送的實踐文章並不多,自己摸索的過程中踩了不少坑,因此把完整流程記錄下來,希望能幫大家少浪費一些排查時間。

  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved