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.总结
初次这样写,测试是成功的,如果你在使用中遇到什么问题,请留言,让我们一起进步。