Swift_集成极光推送(基础版本)
2017-10-09 本文已影响98人
Mccc_
一. 集成
pod 'JPush'
二. 在极光官网上创建应用,获取key并上传推送证书. 送你一步,去查看官网提供的步骤
三. 工程配置
- 开启推送 开启推送1.jpg
四.代码部分
- 初始化Jpush SDK.
在APPDelegate的application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?)
方法里面调用下面方法
func setting_Jpush(application:UIApplication,launchOptions:[UIApplicationLaunchOptionsKey: Any]?) {
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)
}
// 根据环境的不同,设置不同的apsForProduction值,具体看下面参数说明
JPUSHService.setup(withOption: launchOptions,
appKey: "你的JPush key",
channel: "app store",
apsForProduction: false)
}
部分参数说明
channel
指明应用程序包的下载渠道,为方便分渠道统计,具体值由你自行定义,如:App Store。
apsForProduction
1.3.1版本新增,用于标识当前应用所使用的APNs证书环境。
0 (默认值)表示采用的是开发证书,1 表示采用生产证书发布应用。
注:此字段的值要与Build Settings的Code Signing配置的证书环境一致。
- 相关实现
// 注册deviceToken
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)
}
// 进入前台的时候,清空角标
func applicationWillEnterForeground(_ application: UIApplication) {
UIApplication.shared.applicationIconBadgeNumber = 0
}
- Jpush的代理方法
extension AppDelegate : JPUSHRegisterDelegate{
@available(iOS 10.0, *)
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三种类型可以选择设置
}
@available(iOS 10.0, *)
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()
}
}