这里我是通过 pod 的方式导入的极光,因为我用的电脑开发环境已经配置完了,这里就略过了,其实是我也不会。
1.pod 初始化
打开终端切换到工程目录,执行 pod init
2.配置工程下的 Podfile 文件,添加极光
3.回到终端,执行 pod install
4.打开XCode工程目录,因为使用的Swift的原因,我们需要一个Bridging Header 文件,应该是叫桥接文件吧
5.这样就会生成 Bridging Header 文件,那两个生成的oc文件没啥用可以删了,然后修改一下这个文件
//
// Use this file to import your target's public headers that you would like to expose to Swift.
//
// 引入 JPush 功能所需头文件
#import "JPUSHService.h"
// iOS10 注册 APNs 所需头文件
#ifdef NSFoundationVersionNumber_iOS_9_x_Max
#import <UserNotifications/UserNotifications.h>
#endif
6.添加极光代码,别忘了把 key 换一下
import UIKit
@main
class AppDelegate: UIResponder, UIApplicationDelegate ,JPUSHRegisterDelegate{
func jpushNotificationCenter(_ center: UNUserNotificationCenter!, willPresent notification: UNNotification!, withCompletionHandler completionHandler: ((Int) -> Void)!) {
let userInfo = notification.request.content.userInfo
let request = notification.request // 收到推送的请求
let content = request.content // 收到推送的消息内容
let badge = content.badge // 推送消息的角标
let body = content.body // 推送消息体
let sound = content.sound // 推送消息的声音
let subtitle = content.subtitle // 推送消息的副标题
let title = content.title // 推送消息的标题
print("wy________1" + title)
}
func jpushNotificationCenter(_ center: UNUserNotificationCenter!, didReceive response: UNNotificationResponse!, withCompletionHandler completionHandler: (() -> Void)!) {
let userInfo = response.notification.request.content.userInfo
let request = response.notification.request // 收到推送的请求
let content = request.content // 收到推送的消息内容
let badge = content.badge // 推送消息的角标
let body = content.body // 推送消息体
let sound = content.sound // 推送消息的声音
let subtitle = content.subtitle // 推送消息的副标题
let title = content.title // 推送消息的标题
print("wy________2" + title)
}
func jpushNotificationCenter(_ center: UNUserNotificationCenter!, openSettingsFor notification: UNNotification!) {
}
func jpushNotificationAuthorization(_ status: JPAuthorizationStatus, withInfo info: [AnyHashable : Any]!) {
}
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// 推送代码
if #available(iOS 10, *) {
let entity = JPUSHRegisterEntity()
entity.types = NSInteger(UNAuthorizationOptions.alert.rawValue) |
NSInteger(UNAuthorizationOptions.sound.rawValue) |
NSInteger(UNAuthorizationOptions.badge.rawValue)
JPUSHService.register(forRemoteNotificationConfig: entity, delegate: self)
} else if #available(iOS 8, *) {
// 可以自定义 categories
JPUSHService.register(
forRemoteNotificationTypes: UIUserNotificationType.badge.rawValue |
UIUserNotificationType.sound.rawValue |
UIUserNotificationType.alert.rawValue,
categories: nil)
} else {
// ios 8 以前 categories 必须为nil
JPUSHService.register(
forRemoteNotificationTypes: UIRemoteNotificationType.badge.rawValue |
UIRemoteNotificationType.sound.rawValue |
UIRemoteNotificationType.alert.rawValue,
categories: nil)
}
换key
JPUSHService.setup(withOption: launchOptions, appKey: " app key", channel: "App Store", apsForProduction: false)
_ = NotificationCenter.default
return true
}
func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
}
func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
}
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
print("get the deviceToken \(deviceToken)")
NotificationCenter.default.post(name: Notification.Name(rawValue: "DidRegisterRemoteNotification"), object: deviceToken)
JPUSHService.registerDeviceToken(deviceToken)
}
}