你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> swift3.0 集成極光推送(v2.2)iOS10.0最新寫法

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

編輯:IOS開發綜合
//
//  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 
#endif
// 如果需要使用idfa功能所需要引入的頭文件(可選)
#import 

#endif /* bridge_h */

\

 

 

5、打開相關Capabilities

\

\

6、寫代碼(見上)

7、真機運行

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