Swift 3.0 集成信鸽推送

1.前言

推送证书配置什么的都不多讲了,信鸽推送的开发文档里都有详细的介绍信鸽推送文档,因为官方的文档是OC版本的,我这里主要是讲解一下怎么用Swift进行集成。本篇文章也可移步简书阅览,效果更好哦!

2.配置

现在一切都已经根据他们的文档配置好了,就剩下代码转化了,
第一步:
在桥接文件xx-Bridging-Header.h里加入以下代码

#import "XGPush.h"
#import "XGSetting.h"
// iOS10注册APNs所需头文件
#ifdef NSFoundationVersionNumber_iOS_9_x_Max
#import <UserNotifications/UserNotifications.h>
#endif

第二步:
AppDelegate.swift里面的代码,加个扩展

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

        //信鸽推送部署
        XGSetting().enableDebug(true);
        XGPush.startApp(2211353219, appKey: "I1K6Q7M1D3AA")
        XGPush.isPush { (isOn) in
            PrintLog("[XGDemo] Push Is \(isOn ? "ON" : "OFF")")
        }
        self.registerAPNS()
        return true
    }

//MARK:-信鸽推送
extension AppDelegate:UNUserNotificationCenterDelegate
{
    func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {

        let deviceTokenStr = XGPush.registerDevice(deviceToken, account: "nil", successCallback: { _ in
            PrintLog("[XGDemo] register push success");

        }, errorCallback: { _ in
            PrintLog("[XGDemo] register push error")
        })

        PrintLog("XGPush deviceToken is \(deviceTokenStr)");
    }

    /**
     收到通知的回调

     @param application  UIApplication 实例
     @param userInfo 推送时指定的参数
     */

    func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) {
        PrintLog("[XGDemo] receive Notification")
        XGPush.handleReceiveNotification(userInfo, successCallback: {_ in
            PrintLog("[XGDemo] Handle receive success")
        }, errorCallback: {_ in
            PrintLog("[XGDemo] Handle receive error")
        })
    }

    /**
     收到静默推送的回调

     @param application  UIApplication 实例
     @param userInfo 推送时指定的参数
     @param completionHandler 完成回调
     */
    func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
        PrintLog("[XGDemo] receive slient Notification")
        PrintLog("[XGDemo] userinfo\(userInfo)")
        XGPush.handleReceiveNotification(userInfo, successCallback: {_ in
            PrintLog("[XGDemo] Handle receive success")
        }, errorCallback: {_ in
            PrintLog("[XGDemo] Handle receive error")
        })
        completionHandler(UIBackgroundFetchResult.newData)
    }


    // iOS 10 新增 API
    // iOS 10 会走新 API, iOS 10 以前会走到老 API
    // App 用户点击通知的回调
    // 无论本地推送还是远程推送都会走这个回调
    @available(iOS 10.0, *)
    func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
        PrintLog("[XGDemo] click notification")
    XGPush.handleReceiveNotification(response.notification.request.content.userInfo, successCallback: {_ in
         PrintLog("[XGDemo] Handle receive success")
        }, errorCallback: {_ in
         PrintLog("[XGDemo] Handle receive error")
        })
    }

    // App 在前台弹通知需要调用这个接口
    @available(iOS 10.0, *)
    func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {

        let options = UNNotificationPresentationOptions.badge.rawValue | UNNotificationPresentationOptions.sound.rawValue | UNNotificationPresentationOptions.alert.rawValue
        completionHandler(UNNotificationPresentationOptions(rawValue: options))
    }


    func registerAPNS(){
        if #available(iOS 10.0, *){
            registerPush10()
        } else if #available(iOS 8.0, *) {
            registerPush8to9()
        }else {

        }
    }

    func registerPush10(){
        if #available(iOS 10.0, *){
            let center = UNUserNotificationCenter.current();
            center.delegate = self

         //这个'|'写法比较难搞,查了好久
            let options = UNAuthorizationOptions.badge.rawValue | UNAuthorizationOptions.sound.rawValue | UNAuthorizationOptions.alert.rawValue
            center.requestAuthorization(options: UNAuthorizationOptions(rawValue: options) , completionHandler: { (granted, error) in

            })
            UIApplication.shared.registerForRemoteNotifications()
        }
    }

    func registerPush8to9(){
        let type = UIUserNotificationType.badge.rawValue | UIUserNotificationType.sound.rawValue | UIUserNotificationType.alert.rawValue
        let mySetting = UIUserNotificationSettings(types: UIUserNotificationType(rawValue: type), categories: nil)
        UIApplication.shared.registerUserNotificationSettings(mySetting)
        UIApplication.shared.registerForRemoteNotifications()
    }
}

3.总结

初次这样写,测试是成功的,如果你在使用中遇到什么问题,请留言,让我们一起进步。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值