美文网首页
JPush集成

JPush集成

作者: 古月思吉 | 来源:发表于2018-11-10 18:57 被阅读0次
    1. Podfile文件中添加JPush:
    # 极光推送
    pod 'JPush', '~> 3.0.7'
    
    1. 封装JPush工具类(JPushTool.swift):
    /*
     JPush的工具类
     */
    
    import UIKit
    
    class JPushTool: NSObject {
        
        //Jpush配置
        class func config(launchOptions: [UIApplicationLaunchOptionsKey: Any]?,delegate: JPUSHRegisterDelegate) {
            let jpush = JPUSHRegisterEntity()
            jpush.types = Int(JPAuthorizationOptions.alert.rawValue|JPAuthorizationOptions.badge.rawValue|JPAuthorizationOptions.sound.rawValue)
            JPUSHService.register(forRemoteNotificationConfig: jpush, delegate: delegate)
            JPUSHService.setup(withOption: launchOptions, appKey: "6e6d8c084d8ad80ecf23aead", channel: "App Store", apsForProduction: true)
            JPUSHService.registrationIDCompletionHandler { (resCode, registrationID) in
                if resCode == 0{
                    printLog("registrationID获取成功")
                }else {
                    printLog("registrationID获取失败")
                }
            }
        }
        
        //设置tags&alias
        class func setTagsWithAlias (tags:Array<String>?,alias:String?){
            if alias != nil {
                JPUSHService.setAlias(alias!, completion: { (iResCode, iAlias, seq) in
                    if iResCode == 0 {
                        printLog("alias 设置成功!")
                    }else {
                        printLog("alias 设置失败!")
                    }
                }, seq: 1)
            }
            if tags != nil {
                var tagsSet = Set<String>()
                for elementString in tags! {
                    tagsSet.insert(elementString)
                }
                JPUSHService.setTags(tagsSet, completion: { (iResCode, iTags, seq) in
                    if iResCode == 0 {
                        printLog("tags 设置成功!")
                    }else {
                        printLog("tags 设置失败!")
                    }
                }, seq: 2)
            }
        }
        
        //删除tags&alias
        class func deleteTagsWithAlias (){
            JPUSHService.deleteAlias({ (iResCode, iAlias, seq) in
                if iResCode == 0 {
                    printLog("alias 删除成功!")
                }else {
                    printLog("alias 删除失败!")
                }
            }, seq: 1)
            JPUSHService.cleanTags({ (iResCode, iTags, seq) in
                if iResCode == 0 {
                    printLog("tags 删除成功!")
                }else {
                    printLog("tags 删除失败!")
                }
            }, seq: 2)
        }
        
        /// 跳转详情页
        ///
        /// - Parameter userInfo: 传递的dict
        class func jumpToDetailViewController(userInfo:Dictionary<String, Any>?) {
            guard userInfo != nil else {
                return
            }
    //        let model = HomeEntranceModel.deserialize(from: userInfo!)
    //        JumpDetailVCTool.share.jumpDetailVCWithModel(model: model)
        }
        
    }
    
    1. AppDelegate.swift文件中插入代码:
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
       
        JPushTool.config(launchOptions: launchOptions, delegate: self)
        application.applicationIconBadgeNumber = 0//清空桌面icon的未读消息数
        
        return true
    }
    
    func applicationWillEnterForeground(_ application: UIApplication) {
        // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
        application.applicationIconBadgeNumber = 0//清空桌面icon的未读消息数
    }
    
    1. JPush相关的AppDelegate扩展:
    //MARK: - 极光推送
    extension AppDelegate: JPUSHRegisterDelegate {
        
        func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
            //注册 DeviceToken
            JPUSHService.registerDeviceToken(deviceToken)
        }
        
        //iOS7 - iOS10
        func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
            JPUSHService.handleRemoteNotification(userInfo)
            completionHandler(UIBackgroundFetchResult.newData)
            JPushTool.jumpToDetailViewController(userInfo: userInfo as? Dictionary<String, Any>)
        }
        
        //iOS6 及以下的系统版本
        func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) {
            JPUSHService.handleRemoteNotification(userInfo)
            JPushTool.jumpToDetailViewController(userInfo: userInfo as? Dictionary<String, Any>)
        }
    
        @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 is UNPushNotificationTrigger {
                JPUSHService.handleRemoteNotification(userInfo)
            }
            //系统要求执行这个方法
            completionHandler()
            JPushTool.jumpToDetailViewController(userInfo: userInfo as? Dictionary<String, Any>)
        }
        
        @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 is UNPushNotificationTrigger {
                JPUSHService.handleRemoteNotification(userInfo)
            }
            // 需要执行这个方法,选择是否提醒用户,有Badge、Sound、Alert三种类型可以选择设置
            completionHandler(Int(UNNotificationPresentationOptions.alert.rawValue))
        }
        
    }
    

    相关文章

      网友评论

          本文标题:JPush集成

          本文链接:https://www.haomeiwen.com/subject/anqtfqtx.html