swift 极光推送

1、项目配置

1.1 导入SDK

1.1.1 CocoaPods导入

# platform :ios, '9.0'
target 'JGPush' do
  use_frameworks!
  pod 'JPush'
end

1.1.2 手动导入

在极光官网下载最新SDK,将SDK包解压,在Xcode中选择“Add files to 'Your project name'...”,将解压后的lib子文件夹(包含JPUSHService.h、jpush-ios-x.x.x.a、jcore-ios-x.x.x.a)添加到你的工程目录中。

添加Framework、CFNetwork.framework、CoreFoundation.framework、CoreTelephony.framework、SystemConfiguration.framework、CoreGraphics.framework、Foundation.framework、UIKit.framework、Security.framework、libz.tbd (Xcode7以下版本是libz.dylib)、AdSupport.framework (获取IDFA需要;如果不使用IDFA,请不要添加)、UserNotifications.framework (Xcode8及以上)、libresolv.tbd (JPush 2.2.0及以上版本需要, Xcode7以下版本是libresolv.dylib)(注意:

如果集成JPush 3.0.1及以上版本, 且同时集成极光其他SDK(如:JMessage 3.0.0及以上版本) 1. Cocoapods导入,建议都更新为线上最新版本,来避免Jcore版本不一致导致的冲突。 2. 手动导入,在工程中只需保留一个最新版本的jcore-ios-x.x.x.a静态库文件。)

1.2导入SDK所需要依赖的库,添加Framework

CFNetwork.framework
CoreFoundation.framework
CoreTelephony.framework
SystemConfiguration.framework
CoreGraphics.framework
Foundation.framework
UIKit.framework
Security.framework
Xcode 7需要的是libs.tbd; Xcode 7以下版本是libs.dylib
Adsupport.framework (获取IDFA需要;如果不使用IDFA,请不要添加)
UserNotifications.framework (Xcode8及以上)
libresolv.tbd (JPush 2.2.0及以上版本需要)


framework配置.png

1.3 其他配置

Build Settings:

如果你的工程需要支持小于7.0的iOS系统,请到Build Settings 关闭 bitCode 选项,否则将无法正常编译通过。设置 Search Paths 下的 User Header Search Paths 和 Library Search Paths,比如SDK文件夹(默认为lib)与工程文件在同一级目录下,则都设置为"$(SRCROOT)/{静态库所在文件夹名称}"即可。####Capabilities:
如使用Xcode8及以上环境开发,请开启Application Target的Capabilities->Push Notifications选项,如图:


开启推送服务.png

info.plist配置:

在项目的info.plist中添加一个Key:NSAppTransportSecurity,类型为字典类型。然后给它添加一个NSExceptionDomains,类型为字典类型;把需要的支持的域添加給NSExceptionDomains。其中jpush.cn作为Key,类型为字典类型。每个域下面需要设置2个属性:NSIncludesSubdomains、NSExceptionAllowsInsecureHTTPLoads。 两个属性均为Boolean类型,值分别为YES、YES。


info.png

2、工程代码部分

目前极光没有swift版本,因此需要建立一个桥接文件,在里面添加如下代码

#import "JPUSHService.h"
// iOS10注册APNs所需头文件
#ifdef NSFoundationVersionNumber_iOS_9_x_Max
#import 
#endif
// 如果需要使用 idfa 功能所需要引入的头文件(可选)
//#import 
Bridging.png

下面是真正的代码了
appdelegate 里的 didFinishLaunchingWithOptions 方法里(不要试图用我的appKey了,我这么谨慎的人当然放的假的了,自己去极光官网上自己的项目里拿appKey 了)

//推送代码
let entity = JPUSHRegisterEntity()
entity.types = 1 << 0 | 1 << 1 | 1 << 2
JPUSHService.register(forRemoteNotificationConfig: entity, delegate: self)
 //需要IDFA 功能,定向投放广告功能
 //let advertisingId = ASIdentifierManager.shared().advertisingIdentifier.uuidString
JPUSHService.setup(withOption: launchOptions, appKey: "7a84363c8be53oba2c8d1a72", channel: "App Store", apsForProduction: false, advertisingIdentifier: nil)

在AppDelegate里添加JPUSHRegisterDelegate代理,并且实现如下代理方法

@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)
        }
        // 需要执行这个方法,选择是否提醒用户,有Badge、Sound、Alert三种类型可以选择设置
        completionHandler(Int(UNNotificationPresentationOptions.alert.rawValue))
    }
    
    @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()
    }
    
    //点推送进来执行这个方法
    func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
        JPUSHService.handleRemoteNotification(userInfo)
        completionHandler(UIBackgroundFetchResult.newData)
        
    }
    //系统获取Token
    func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
        JPUSHService.registerDeviceToken(deviceToken)
    }
    //获取token 失败
    func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) { //可选
        print("did Fail To Register For Remote Notifications With Error: \(error)")
    }
    func jpushNotificationCenter(_ center: UNUserNotificationCenter!, openSettingsFor notification: UNNotification?) {
        print("hello")
    }

推送小红角标,程序从后台进入前台的时候我们要消除它

//后台进前台
func applicationDidEnterBackground(_ application: UIApplication) {
    //销毁通知红点
    UIApplication.shared.applicationIconBadgeNumber = 0
    JPUSHService.setBadge(0)
    UIApplication.shared.cancelAllLocalNotifications()
}

你可能感兴趣的:(swift 极光推送)