美文网首页
iOS swift4 接 极光推送

iOS swift4 接 极光推送

作者: de_fly | 来源:发表于2018-05-15 14:45 被阅读75次

    1. 按照说明 导出 push 的证书 上传到 极光后台

    2. get appKey . import jPush sdk to project

    3. project-Bridging-Header.h 里面导入 jPush SDK 的头文件

    #import "JPUSHService.h"

    4. 在 Appdelegate 里面  配置 SDK 继承协议  

     func configNotification() {

            let settings =UIUserNotificationSettings(types: [.badge, .sound, .alert] ,categories:nil)

            UIApplication.shared.registerUserNotificationSettings(settings)

            UIApplication.shared.registerUserNotificationSettings(settings)

            UIApplication.shared.registerForRemoteNotifications()

        }

     fun cconfigJPush(_launchOptions: [UIApplicationLaunchOptionsKey:Any]?) {

    //        JPUSHService.registerForRemoteNotificationTypes(7, categories: nil)

    //        JPUSHService.setupWithOption(launchOptions, appKey: Environment.JPushAppKey, channel: "Develpoment", apsForProduction: false)

            // 通知注册实体类

            letentity =JPUSHRegisterEntity();

            entity.types = Int(JPAuthorizationOptions.alert.rawValue) |  Int(JPAuthorizationOptions.sound.rawValue) |  Int(JPAuthorizationOptions.badge.rawValue);

            JPUSHService.register(forRemoteNotificationConfig: entity, delegate:self);

            // 注册极光推送

            JPUSHService.setup(withOption: launchOptions, appKey:SDKConstant.JPushKey, channel:"Publish channel", apsForProduction:false);

            // 获取推送消息

            let remote = launchOptions?[UIApplicationLaunchOptionsKey.remoteNotification] as? Dictionary;

            // 如果remote不为空,就代表应用在未打开的时候收到了推送消息

            ifremote !=nil{

                // 收到推送消息实现的方法

                self.perform(#selector(receivePush), with: remote, afterDelay: 1.0)

            }

        }

        func application(_application:UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken:Data) {

            JPUSHService.registerDeviceToken(deviceToken)

            //设置tags,后台可以根据这个来推送(本处用的是UserId)

            JPUSHService.setTags(["user"], aliasInbackground:"100")

            print("deviceToken:\(deviceToken)")

        }

        func application(_application:UIApplication, didFailToRegisterForRemoteNotificationsWithError error:Error) {

            print("didFailToRegisterForRemoteNotificationsWithError")

        }

        // despress

        func application(_application:UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable:Any]) {

            print("收到通知:")

        }

        func application(_application:UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable:Any], fetchCompletionHandler completionHandler:@escaping(UIBackgroundFetchResult) ->Void) {

             print("收到通知:")

            JPUSHService.handleRemoteNotification(userInfo);

            completionHandler(.newData)

        }

    extension AppDelegate: JPUSHRegisterDelegate {

        @available(iOS10.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);

            }

            completionHandler(Int(UNNotificationPresentationOptions.alert.rawValue))

        }

        @available(iOS10.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();

            // 应用打开的时候收到推送消息

            NotificationCenter.default.post(name:NSNotification.Name(rawValue:"NotificationName_ReceivePush"), object:"NotificationObject_Sueecess", userInfo: userInfo)

        }

        // 接收到推送实现的方法

        @objc func receivePush(_userInfo : [String:Any]) {

            // 角标变0

            UIApplication.shared.applicationIconBadgeNumber = 0;

            // 剩下的根据需要自定义

        }

    }

    相关文章

      网友评论

          本文标题:iOS swift4 接 极光推送

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