Swift开发实战iOS第三方SDKiOS-swift

iOS开发 swift -- JPush极光推送的使用

2017-04-11  本文已影响945人  Hayley__

一 APNS推送机制

1、 官方解释如下图


推送.jpg
图中,Provider是指某个iPhone软件的Push服务器。

APNS 是Apple Push Notification Service(Apple Push服务器)的缩写,是苹果的服务器。
上图可以分为三个阶段。
第一阶段:服务器把要发送的消息、目的iPhone的标识打包,发给APNS。
第二阶段:APNS在自身的已注册Push服务的iPhone列表中,查找有相应标识的iPhone,并把消息发到iPhone。
第三阶段:iPhone把发来的消息传递给相应的应用程序, 并且按照设定弹出Push通知。

2、 详细工作流程


推送.jpg
根据图片我们可以概括一下:

a、应用程序注册APNS消息推送。
b、iOS从APNS Server获取devicetoken,应用程序接收device token。
c、应用程序将device token发送给程序的PUSH服务端程序。
d、服务端程序向APNS服务发送消息。
e、APNS服务将消息发送给iPhone应用程序。

二 注册证书

详细步骤

三 SDK 集成 CocoaPods

$ cd/你的项目地址
$ open -e Podfile

target 'zanqian' do
  pod 'JPush', '~> 3.0.0'
 end

$ pod install

四 代码示例

1、基本配置

    //appdelegate.swift 遵循 JPUSHRegisterDelegate
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        // 极光
        if #available(iOS 10.0, *) {
            let entity = JPUSHRegisterEntity()
            entity.types = Int(UNAuthorizationOptions.alert.rawValue|UNAuthorizationOptions.badge.rawValue|UNAuthorizationOptions.sound.rawValue)
            JPUSHService.register(forRemoteNotificationConfig: entity, delegate: self)
        }else {
            let types = UIUserNotificationType.badge.rawValue |
            UIUserNotificationType.sound.rawValue |
            UIUserNotificationType.alert.rawValue
            JPUSHService.register(forRemoteNotificationTypes: types, categories: nil)
        }
        JPUSHService.setup(withOption: launchOptions, appKey: "", channel: "app store", apsForProduction: false)

        // 获取远程推送消息 iOS 10 已取消
        let remote = launchOptions?[UIApplicationLaunchOptionsKey.remoteNotification] as? Dictionary<String,Any>;
        // 如果remote不为空,就代表应用在未打开的时候收到了推送消息
        if remote != nil {
            // 收到推送消息实现的方法 
           }

         //自定义消息 需要自己在receiveNotification中自己实现消息的展示
         NotificationCenter.default.addObserver(self, selector: #selector(receiveNotification(notification:)), name: NSNotification.Name.jpfNetworkDidReceiveMessage, object: nil)
    }

    //注册APNs成功并上报DeviceToken
    func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
        JPUSHService.registerDeviceToken(deviceToken)
    }

    func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
        debugPrint("极光注册失败",error.localizedDescription)
    }

2、代理回调

    // 接收到消息回调方法
    @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)
        }else {
            //本地通知
        }
        //需要执行这个方法,选择是否提醒用户,有Badge、Sound、Alert三种类型可以选择设置
        completionHandler(Int(UNNotificationPresentationOptions.alert.rawValue))
    }
    
    @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)
        }else {
            //本地通知
        }
        //处理通知 跳到指定界面等等
        receivePush(userInfo as! Dictionary<String, Any>)
        completionHandler()
    }

    // iOS 9.0 
    func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
        JPUSHService.handleRemoteNotification(userInfo);
        completionHandler(UIBackgroundFetchResult.newData);
    }

3、推送处理

    func receivePush(_ userInfo : Dictionary<String,Any>) {
        //根据服务器返回的字段判断
        let userInfo = userInfo["key"] as! String
        let jsonData:Data = userInfo.data(using: .utf8)!
        var dict = try! JSONSerialization.jsonObject(with: jsonData, options: .allowFragments) as! [String:AnyObject]
        let messageJson = JSON(dict ["data"]!)
        let message = Message.init(json: messageJson)
        if message.type == 2
        {
            let rootView = getRootViewController()
            rootView.pushViewController(MessageViewController.init(type: 2), animated: true)
        }
        // 角标变0
        UIApplication.shared.applicationIconBadgeNumber = 0
    }


    //获取当前页面的是控制器
    func getRootViewController () ->(UINavigationController)
    {
        let rootViewController = UIApplication.shared.keyWindow?.rootViewController
        if (rootViewController?.isKind(of: TabBarViewController.self))!
        {
            //let firstController = rootViewController?.childViewControllers[tabBarController.selectedIndex]
            let firstController = tabBarController.selectedViewController
            if (firstController?.isKind(of: UINavigationController.self))! {
                return firstController as! UINavigationController
            }else
            {
                return UINavigationController.init(rootViewController: firstController!)
            }
        }else if (rootViewController?.isKind(of: UINavigationController.self))!
        {
            return rootViewController as! UINavigationController
        }
        
        return UINavigationController.init(rootViewController: rootViewController!)
    }

    func applicationDidBecomeActive(_ application: UIApplication) {
      //角标至0
      UIApplication.shared.applicationIconBadgeNumber = 0
    }

如有不妥,请多多指教。

上一篇下一篇

猜你喜欢

热点阅读