首页 > 程序开发 > 移动开发 > IOS >

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

2016-11-21

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 
#endif
// 如果需要使用idfa功能所需要引入的头文件(可选)
#import 

#endif /* bridge_h */

\

5、打开相关Capabilities

\

\

6、写代码(见上)

7、真机运行

相关文章
最新文章
热点推荐