Swift-極光推送整合指南 (本人親測)
專案用到了極光推送
官方文件沒有
自己百度了
測試OK
就整合進來了
1 匯入SDK
2 橋檔案匯入
// 引入JPush功能所需標頭檔案
#import "JPUSHService.h"
// iOS10註冊APNs所需標頭檔案
#import <UserNotifications/UserNotifications.h>
3 AppDelegate進行註冊
func registerJPush(launchOptions:[UIApplicationLaunchOptionsKey:Any]?) {
if (UIDevice.current.systemVersionasNSString
let entity = JPUSHRegisterEntity()
entity.types = 0|1|2
JPUSHService.register(forRemoteNotificationConfig: entity, delegate:nil)
} else {
JPUSHService.register(forRemoteNotificationTypes:0|1|2, categories:nil)
}
JPUSHService.setup(withOption: launchOptions, appKey: JPushAppKey, channel: JPushChannel, apsForProduction: isProduction)
JPUSHService.setLogOFF() //關閉日誌列印
}
4 AppDelegate代理方法
func application(_ application:UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken:Data) {
JPUSHService.registerDeviceToken(deviceToken)
}
// 前臺模式收到推送資料
func application(_ application:UIApplication, didReceiveRemoteNotification userInfo: [
JPUSHService.handleRemoteNotification(userInfo)
completionHandler(.newData)
let alertController =UIAlertController(title:"訊息通知",
message:"您有一條訊息請檢視", preferredStyle: .alert)
let okAction =UIAlertAction(title:"檢視", style: .default, handler: {
action in
print("點選了確定")
})
alertController.addAction(okAction)
UIApplication.shared.keyWindow?.rootViewController?.present(alertController, animated: true, completion: nil)
}
func application(_ application:UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable:Any]) {
JPUSHService.handleRemoteNotification(userInfo)
}
5 很重要的配置
6 清空角標
// 置空訊息未讀數量
JPUSHService.resetBadge()
UIApplication.shared.applicationIconBadgeNumber =0
7 註冊標籤 別名// 註冊極光推送別名/標籤
JPUSHService.setTags(nil, alias: idStr,callbackSelector: nil, object: JPushManager.manager)