极光推送集成总结

2017-10-13  本文已影响0人  晨曦中的花豹

最近项目上集成了极光推送,这里做一下集成过程中的步骤及出现的问题(集成框架借鉴极光官方文档).
具体推送的原理就不说了,我们的项目是swift,这里就拿swift代码举例说明.
步骤:
1.获取极光推送的appKey(找极光去注册)
2.初始化APNs(苹果推送的核心,极光已经集成好了,一句代码就可以设置好APNs)

if #available(iOS 10.0, *){
            let entiity = JPUSHRegisterEntity()
            entiity.types = Int(UNAuthorizationOptions.alert.rawValue |
                UNAuthorizationOptions.badge.rawValue |
                UNAuthorizationOptions.sound.rawValue)
            JPUSHService.register(forRemoteNotificationConfig: entiity, 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)
        }else {
            let type = UIRemoteNotificationType.badge.rawValue |
                UIRemoteNotificationType.sound.rawValue |
                UIRemoteNotificationType.alert.rawValue
            JPUSHService.register(forRemoteNotificationTypes: type, categories: nil)
        }

3.初始化JPush

let advertisingId = ASIdentifierManager.shared().advertisingIdentifier.uuidString
JPUSHService.setup(withOption: launchOptions, appKey: "极光注册的appKey", channel: "这个随便填写一个没什么用(目前没发现有什么用)", apsForProduction: false, advertisingIdentifier: advertisingId)

以上2,3步都要写到application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool里面
4.APNs获取deviceToken成功后,需要告诉极光,所以回调方法

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
        JPUSHService.registerDeviceToken(deviceToken)
    }

5.实现JPush回调方法,这里极光封装了回调方法

@available(iOS 10.0, *)
    func jpushNotificationCenter(_ center: UNUserNotificationCenter!, willPresent notification: UNNotification!, withCompletionHandler completionHandler: ((Int) -> Void)!) {
        
        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)!) {
        let userInfo = response.notification.request.content.userInfo
        if (response.notification.request.trigger?.isKind(of: UNPushNotificationTrigger.self))!{
            JPUSHService.handleRemoteNotification(userInfo)
        }
        completionHandler()
    }
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {
        //ios7-
        JPUSHService.handleRemoteNotification(userInfo)
    }
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
        //ios7+
        JPUSHService.handleRemoteNotification(userInfo)
        completionHandler(.newData)
    }
let remoteNotification = launchOptions?[UIApplicationLaunchOptionsKey.remoteNotification]
if remoteNotification != nil {
            print("根据通知做处理")
        }

6.添加别名(对特定的账号进行推送使用),我们公司已注册账号的电话号码作为别名,在用户登陆成功后调用这个接口

let alias = DataManger.Shared.userInfo?["telephone"]
      //获取标签
JPUSHService.setAlias(alias, completion: { (success, alias, seq) in
        print("JPush别名注册,响应结果:\(success)")
}, seq: 100)

好了,以上就是极光推送的简单的集成过程,

下面说说集成极光推送过程中可能遇到的问题

2C7D4875-B790-4E5E-9328-80F473A6199A.png F046D91E-FB13-4906-ABC1-9C4D650560F4.png

这里就可以看到后台发送的通知是在什么环境下了

注册通知

NOTIFICATIONCENTER.addObserver(self, selector: #selector(networkDidReceiveMessage(notification:)), name: NSNotification.Name.jpfNetworkDidReceiveMessage, object: nil)

实现通知方法

func networkDidReceiveMessage(notification:NSNotification){
        let userInfo = notification.userInfo
    }
        let urlStr:URL?
        if type == "1" {
            //订单
            urlStr = URL(string: Bundle.main.path(forResource: "dingdantishi.mp3", ofType: nil)!)!
        } else if type == "2" {
            //退款
            urlStr = URL(string: Bundle.main.path(forResource: "tuikuantishi.mp3", ofType: nil)!)!
        } else {
            urlStr = nil
        }
        
        do {
            avPlayer = try AVAudioPlayer(contentsOf: urlStr!)
            avPlayer.play()
        } catch let err {
            print("播放失败:\(err.localizedDescription)")
        }

以上就是我集成极光推送的过程中,集成过程及遇到的问题,如有不足请补充,我的qq:1459709117,欢迎打扰!!!

上一篇下一篇

猜你喜欢

热点阅读