极光推送集成总结

最近项目上集成了极光推送,这里做一下集成过程中的步骤及出现的问题(集成框架借鉴极光官方文档).
具体推送的原理就不说了,我们的项目是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()
    }
  • ios7以前使用
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {
        //ios7-
        JPUSHService.handleRemoteNotification(userInfo)
    }
  • ios7及以后使用
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
        //ios7+
        JPUSHService.handleRemoteNotification(userInfo)
        completionHandler(.newData)
    }
  • 在程序杀死的情况下
    在 application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool 中判断时候有通知
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)

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

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

  • 集成极光推送一定需要注意区分生产环境还是还发环境,这里我们后台开始没有区分,导致测试花了很长时间(最后还是我给他改的php代码...),如果同学们发现在测试的时候推送不通,可以去极光官网查看一下后台发送的通知是在什么环境下
极光推送集成总结_第1张图片
2C7D4875-B790-4E5E-9328-80F473A6199A.png
极光推送集成总结_第2张图片
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
    }
  • 自定义推送通知语音,这个可以设置极光推送中的sounds字段,这一字段的值为你的音频+扩展名,例:dingdantishi.mp3,你的项目里面添加了这个音频文件,推送的时候就会用你设置的这个音频,但是注意这个只适用于app在后台或者杀死的情况下,app在前台不会起作用(我的代码不会在前台播放自定义的音频,不知道其他人的怎么样,如果有大神知道请留言告诉我),如果app在前台,需要在前台推送回调的方法里,手动代码播放对应的音频
        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,欢迎打扰!!!

你可能感兴趣的:(极光推送集成总结)