按照极光后台pod,代码没什么难度,方便之前没用过的人复制粘贴一下...
注意确保注册成功后再设置别名,否则无效.
didFinishLaunchingWithOptions方法中注册
//极光推送
if #available(iOS 10.0, *){
let entity = JPUSHRegisterEntity()
entity.types = Int(JPAuthorizationOptions.alert.rawValue | JPAuthorizationOptions.badge.rawValue | JPAuthorizationOptions.sound.rawValue)
JPUSHService.register(forRemoteNotificationConfig: entity, 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)
}
JPUSHService.setup(withOption: launchOptions, appKey: JPUSHAPPKEY, channel: "app store", apsForProduction: false)
添加其他方法
//获取DeviceToken传递
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
//环信
// EMClient.shared().bindDeviceToken(deviceToken)
//极光
JPUSHService.registerDeviceToken(deviceToken)
}
//注册token失败
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
print("注册token失败错误信息\(error)")
}
iOS10之前收到推送的方法:
//收到推送
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
//角标清空
JPUSHService.setBadge(0)
UIApplication.shared.applicationIconBadgeNumber = 0
JPUSHService.handleRemoteNotification(userInfo)
completionHandler(UIBackgroundFetchResult.newData)
if application.applicationState == UIApplicationState.active {
//应用活跃状态
print("userInfo信息\(userInfo as NSDictionary)")
}else{
//其他状态根据需要区分
}
}
iOS10极光封装系统的方法接收消息:
@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
print("打印极光推送消息内容\(userInfo as NSDictionary)")
if (response.notification.request.trigger?.isKind(of: UNPushNotificationTrigger.self))!{
JPUSHService.handleRemoteNotification(userInfo)
}
//角标清空
JPUSHService.setBadge(0)
UIApplication.shared.applicationIconBadgeNumber = 0
completionHandler()
//自定义跳转界面方法,也可以根据需要在首页做监听事件
let userDic = userInfo as NSDictionary
if userDic["sourceType"] != nil{
if userDic["sourceType"] as! String == "2"{
//消息类型
messageCode = 2 //粉丝列表
if userDic["id"] != nil{
remotePushId = userDic["id"] as! String
}
mainVC.removeFromParentViewController()
let mainVc = MainViewController()
mainVC = mainVc
window?.rootViewController = mainVc
}
}
}
添加监听 注册成功后设置别名(极光只接受string类型别名,如果iResCode出现6003等,可以考虑是否传了string类型之外别名导致错误)
//极光监听登录状态
NotificationCenter.default.addObserver(self, selector: #selector(networkDidLogin), name:NSNotification.Name.jpfNetworkDidLogin, object: nil)
//极光登录成功后设置别名
func networkDidLogin(){
print("极光登录成功")
// print("极光登录成功\(UserDefaults.standard.value(forKey: "userId"))")
if UserDefaults.standard.value(forKey: "userId") != nil{
JPUSHService.setTags(nil, alias: UserDefaults.standard.value(forKey: "userId") as! String, fetchCompletionHandle: { (iResCode, iTags, iAlias) in
print("设置别名成功\(UserDefaults.standard.value(forKey: "userId") as! String)___状态码\(iResCode)(如果是0代表成功)")
})
}
NotificationCenter.default.removeObserver(self, name: NSNotification.Name.jpfNetworkDidLogin, object: nil)
}
上线前记得提醒后台切换环境,切换到生产环境