按照極光后臺pod,代碼沒什么難度,方便之前沒用過的人復(fù)制粘貼一下...
注意確保注冊成功后再設(shè)置別名,否則無效.
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) {
//環(huán)信
// 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) {
//角標(biāo)清空
JPUSHService.setBadge(0)
UIApplication.shared.applicationIconBadgeNumber = 0
JPUSHService.handleRemoteNotification(userInfo)
completionHandler(UIBackgroundFetchResult.newData)
if application.applicationState == UIApplicationState.active {
//應(yīng)用活躍狀態(tài)
print("userInfo信息\(userInfo as NSDictionary)")
}else{
//其他狀態(tài)根據(jù)需要區(qū)分
}
}
iOS10極光封裝系統(tǒng)的方法接收消息:
@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))// 需要執(zhí)行這個方法,選擇是否提醒用戶,有Badge、Sound痰哨、Alert三種類型可以選擇設(shè)置
}
@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("打印極光推送消息內(nèi)容\(userInfo as NSDictionary)")
if (response.notification.request.trigger?.isKind(of: UNPushNotificationTrigger.self))!{
JPUSHService.handleRemoteNotification(userInfo)
}
//角標(biāo)清空
JPUSHService.setBadge(0)
UIApplication.shared.applicationIconBadgeNumber = 0
completionHandler()
//自定義跳轉(zhuǎn)界面方法,也可以根據(jù)需要在首頁做監(jiān)聽事件
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
}
}
}
添加監(jiān)聽 注冊成功后設(shè)置別名(極光只接受string類型別名,如果iResCode出現(xiàn)6003等,可以考慮是否傳了string類型之外別名導(dǎo)致錯誤)
Snip20170517_1.png
//極光監(jiān)聽登錄狀態(tài)
NotificationCenter.default.addObserver(self, selector: #selector(networkDidLogin), name:NSNotification.Name.jpfNetworkDidLogin, object: nil)
//極光登錄成功后設(shè)置別名
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("設(shè)置別名成功\(UserDefaults.standard.value(forKey: "userId") as! String)___狀態(tài)碼\(iResCode)(如果是0代表成功)")
})
}
NotificationCenter.default.removeObserver(self, name: NSNotification.Name.jpfNetworkDidLogin, object: nil)
}
上線前記得提醒后臺切換環(huán)境,切換到生產(chǎn)環(huán)境
EF3B0720-C0A8-4755-84DE-AFC0EB60A578.png