swift 极光推送
極光推送有兩種形式:1、通知消息(在手機的通知欄能看見的); 2、推送消息;開發時要注意:后臺發的的推送消息還是通知消息以便處理接收消息
一、注冊
let entity = JPUSHRegisterEntity();
entity.types = Int(JPAuthorizationOptions.alert.rawValue) | Int(JPAuthorizationOptions.badge.rawValue) | Int(JPAuthorizationOptions.sound.rawValue);
JPUSHService.register(forRemoteNotificationConfig: entity, delegate: self);
// 注?JPushIsProduction =1 生產環境? =0 測試環境
JPUSHService.setup(withOption: launchOptions, appKey: JPushAppKey, channel: JPushChannel, apsForProduction:
JPushIsProduction);
// 注冊接收極光推送
NotificationCenter.default.addObserver(self,selector:#selector(self.networkDidReceive(_:)),name: .jpfNetworkDidReceiveMessage, object: nil);
二、處理代理
// 注冊APNs成功并上報DeviceToken
func application(_application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
? ? ? ? JPUSHService.registerDeviceToken(deviceToken);
? ? }
?// 接受推送通知(前臺)
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);
? ? ? ? ? ? NotificationCenter.default.post(name: NSNotification.Name(rawValue: "userInfo"), object:nil);
? ? ? ? }
?completionHandler(Int(UNAuthorizationOptions.alert.rawValue | UNAuthorizationOptions.sound.rawValue | UNAuthorizationOptions.badge.rawValue));// 需要執行這個方法,選擇是否提醒用戶,有badge、sound、alert三種類型可以選擇設置
? ? }
? ? // 接受推送通知(后臺)
func jpushNotificationCenter(_center:UNUserNotificationCenter!,didReceiveresponse: UNNotificationResponse!, withCompletionHandler completionHandler: (() -> Void)!) {
? ? ? ? let userInfo:Dictionary = response.notification.request.content.userInfo;
? ? ? ? if response.notification.request.trigger is UNPushNotificationTrigger {
? ? ? ? ? ? JPUSHService.handleRemoteNotification(userInfo);
? ? ? ? ? ? NotificationCenter.default.post(name: NSNotification.Name(rawValue: "userInfo"), object:nil);
? ? ? ? }
? ? ? ? completionHandler();
? ? }
?? ? //接收推送消息
?@objc func networkDidReceive(_ notification: Notification) {
? ? ? ? let userInfo = notification.userInfo;
? ? if userInfo!["content"] != nil && "\(userInfo!["content"]!)".components(separatedBy: "statu").count??> 1{
? ? ? ? ? NotificationCenter.default.post(name: NSNotification.Name(rawValue: "refreshStatu"), object:nil);
? ? ? ? }else{
? ? ? ? ? ? NotificationCenter.default.post(name: NSNotification.Name(rawValue: "userInfo"), object:nil);
? ? ? ? }
? ? ? ? DNJPushManager.shared.didReceiveMessage(userInfo as! Dictionary<String, Any>);
? ? }
三、設置別名
// 注意一定要設置別名,方便一對一發送極光消息,通常會取用戶ID作為唯一標識
? ? JPUSHService.setAlias("userId", completion: { (iResCode, tags, alias)?in
? ? ? ? ? ? ? ??print("別名設置",iResCode);??
? ? ?}, seq:?1)
總結
以上是生活随笔為你收集整理的swift 极光推送的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C语言 第五章 选择结构 答案,c语言第
- 下一篇: dart --- 环境配置