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)






  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值