swift 极光推送

匿名 (未验证) 提交于 2019-12-03 00:32:02

极光推送有两种形式: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);


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) {

userInfo = notification.userInfo;

if userInfo!["content"] != nil && "\(userInfo!["content"]!)".components(separatedBy: "statu").> 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作为唯一标识

.setAlias("userId"in

print("别名设置"

1)






文章来源: swift 极光推送
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!