极光推送(Swift)

1、使用cocoapod集成

pod 'JPush', '~> 3.0.5'

2、注册APNs

private func Jpush(launch: [UIApplicationLaunchOptionsKey: Any]){
        //iOS8以上 注册APNS
        
        let version = (UIDevice.current.systemVersion as NSString).floatValue
        
        if version >= 10.0 {
            let entity = JPUSHRegisterEntity.init()
            entity.types = Int(JPAuthorizationOptions.alert.rawValue) + Int(JPAuthorizationOptions.sound.rawValue) + Int(JPAuthorizationOptions.badge.rawValue)
            JPUSHService.register(forRemoteNotificationConfig: entity, delegate: self)
        } else if version >= 8.0 {
            JPUSHService.register(forRemoteNotificationTypes: UInt(Int(JPAuthorizationOptions.alert.rawValue) + Int(JPAuthorizationOptions.sound.rawValue)), categories: nil)
        } else {
            JPUSHService .register(forRemoteNotificationTypes: UInt(Int(JPAuthorizationOptions.alert.rawValue) + Int(JPAuthorizationOptions.sound.rawValue) + Int(JPAuthorizationOptions.badge.rawValue)), categories: nil)
        }
        
        // Required
        JPUSHService.setup(withOption: launch, appKey: JpushAppkey, channel: "App Store", apsForProduction: isProduct, advertisingIdentifier: nil)
        
        //2.1.9版本新增获取registration id block接口。
        JPUSHService.registrationIDCompletionHandler { (resCode: Int32, registrationId: String?) in
            if(resCode == 0){
                NSLog("registrationID获取成功:%@",registrationId!);
            }
            else{
                NSLog("registrationID获取失败,code:%d",resCode);
            }
        }
    }

3、向苹果发送deviceToken

/** 推送注册成功,添加的方法 */
    func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
        JPUSHService.registerDeviceToken(deviceToken)
    }
    
    /** 推送注册失败添加的方法 */
    func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
        print("did Fail To Register For Remote Notifications With Error: %@", error)
    }

4、实现极光代理方法

    //  MARK: JPUSHRegisterDelegate
    
    // iOS 10 Support
    @available(iOS 10.0, *)
    func jpushNotificationCenter(_ center: UNUserNotificationCenter!, willPresent notification: UNNotification!, withCompletionHandler completionHandler: ((Int) -> Void)!) {
        UIApplication.shared.applicationIconBadgeNumber = 1
        let userInfo = notification.request.content.userInfo
        
        if (notification.request.trigger?.isKind(of: UNPushNotificationTrigger.self))!{
            JPUSHService.handleRemoteNotification(userInfo)
        }
        // 需要执行这个方法,选择是否提醒用户,有Badge、Sound、Alert三种类型可以选择设置
        completionHandler(Int(UNNotificationPresentationOptions.alert.rawValue) + Int(UNNotificationPresentationOptions.sound.rawValue) + Int(UNNotificationPresentationOptions.badge.rawValue))
    }

    // iOS 10 Support
    @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()
    }

可以在代理方法中实现当点击推送内容时,打开app并处理业务逻辑。
5、配置推送开关


204F542D-E316-4938-B10C-303E3E2AD66E.png

6、在开发者账号创建推送证书,并与项目进行关联,导出开发者证书,在极光服务平台,创建应用并添加推送证书

你可能感兴趣的:(极光推送(Swift))