1. 程式人生 > >swift3.0 整合極光推送(v2.2)iOS10.0最新寫法

swift3.0 整合極光推送(v2.2)iOS10.0最新寫法

//
//  AppDelegate.swift
//  1120-jiguang
//
//  Created by targetcloud on 2016/11/20.
//  Copyright © 2016年 targetcloud. All rights reserved.
//

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()
    }
}
/*
 2016-11-20 20:45:37.712478 1120-jiguang[553:149164]  | JPUSH | I - [JPUSHService]
 --------------------------- JPush Log ----------------------------
 --------------------JPush SDK Version:2.2.0--build:44----------
 -----------------AppKey:4adfb75ea2e6b055ccb04891----------------
 ----------------------------------------------------------------
 2016-11-20 20:45:37.740351 1120-jiguang[553:149164]  | JPUSH | I - [JPUSHClientController] Action - setup
 2016-11-20 20:45:37.940545 1120-jiguang[553:149163]  | JPUSH | W - [JPUSHLocationInfoController] reportWifiInfo not exist
 2016-11-20 20:45:38.367587 1120-jiguang[553:149163]  | JPUSH | I - [JPUSHSessionController] connecting with coreAddr 118.145.3.74,port 7001
 2016-11-20 20:45:38.578520 1120-jiguang[553:149166]  | JPUSH | I - [JPUSHLogin]
 ----- login result -----
 uid:7719140901
 registrationID:161a3797c80a709c926
 2016-11-20 20:45:38.590008 1120-jiguang[553:149163]  | JPUSH | I - [JPUSHDeviceTokenReport] try to upload device token:04a359d9ce5f988618db2072602c0884e8f3dcf04747ae8a20863c20eb0a8a6d
 2016-11-20 20:45:39.669897 1120-jiguang[553:149162]  | JPUSH | I - [JPUSHDeviceTokenReport] upload device token success
 */



整合步驟補充

1、下載包,拖包中的Lib到工程中

2、設定依賴包


3、根據需要加入ATS

4、建立橋接檔案並配置橋接檔案

//
//  bridge.h
//  1120-jiguang
//
//  Created by targetcloud on 2016/11/20.
//  Copyright © 2016年 targetcloud. All rights reserved.
//

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

#endif /* bridge_h */


5、開啟相關Capabilities



6、寫程式碼(見上)

7、真機執行