一 APNS推送機制
1笆制、 官方解釋如下圖
圖中鞋屈,Provider是指某個iPhone軟件的Push服務(wù)器。
APNS 是Apple Push Notification Service(Apple Push服務(wù)器)的縮寫稠屠,是蘋果的服務(wù)器只盹。
上圖可以分為三個階段。
第一階段:服務(wù)器把要發(fā)送的消息驼侠、目的iPhone的標(biāo)識打包姿鸿,發(fā)給APNS。
第二階段:APNS在自身的已注冊Push服務(wù)的iPhone列表中倒源,查找有相應(yīng)標(biāo)識的iPhone苛预,并把消息發(fā)到iPhone。
第三階段:iPhone把發(fā)來的消息傳遞給相應(yīng)的應(yīng)用程序笋熬, 并且按照設(shè)定彈出Push通知热某。
2、 詳細(xì)工作流程
根據(jù)圖片我們可以概括一下:
a胳螟、應(yīng)用程序注冊APNS消息推送昔馋。
b、iOS從APNS Server獲取devicetoken糖耸,應(yīng)用程序接收device token秘遏。
c、應(yīng)用程序?qū)evice token發(fā)送給程序的PUSH服務(wù)端程序嘉竟。
d邦危、服務(wù)端程序向APNS服務(wù)發(fā)送消息洋侨。
e、APNS服務(wù)將消息發(fā)送給iPhone應(yīng)用程序倦蚪。
二 注冊證書
三 SDK 集成 CocoaPods
$ cd/你的項目地址
$ open -e Podfile
target 'zanqian' do
pod 'JPush', '~> 3.0.0'
end
$ pod install
四 代碼示例
1希坚、基本配置
//appdelegate.swift 遵循 JPUSHRegisterDelegate
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// 極光
if #available(iOS 10.0, *) {
let entity = JPUSHRegisterEntity()
entity.types = Int(UNAuthorizationOptions.alert.rawValue|UNAuthorizationOptions.badge.rawValue|UNAuthorizationOptions.sound.rawValue)
JPUSHService.register(forRemoteNotificationConfig: entity, delegate: self)
}else {
let types = UIUserNotificationType.badge.rawValue |
UIUserNotificationType.sound.rawValue |
UIUserNotificationType.alert.rawValue
JPUSHService.register(forRemoteNotificationTypes: types, categories: nil)
}
JPUSHService.setup(withOption: launchOptions, appKey: "", channel: "app store", apsForProduction: false)
// 獲取遠(yuǎn)程推送消息 iOS 10 已取消
let remote = launchOptions?[UIApplicationLaunchOptionsKey.remoteNotification] as? Dictionary<String,Any>;
// 如果remote不為空,就代表應(yīng)用在未打開的時候收到了推送消息
if remote != nil {
// 收到推送消息實現(xiàn)的方法
}
//自定義消息 需要自己在receiveNotification中自己實現(xiàn)消息的展示
NotificationCenter.default.addObserver(self, selector: #selector(receiveNotification(notification:)), name: NSNotification.Name.jpfNetworkDidReceiveMessage, object: nil)
}
//注冊APNs成功并上報DeviceToken
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
JPUSHService.registerDeviceToken(deviceToken)
}
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
debugPrint("極光注冊失敗",error.localizedDescription)
}
2审丘、代理回調(diào)
// 接收到消息回調(diào)方法
@available(iOS 10.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)
}else {
//本地通知
}
//需要執(zhí)行這個方法吏够,選擇是否提醒用戶,有Badge滩报、Sound锅知、Alert三種類型可以選擇設(shè)置
completionHandler(Int(UNNotificationPresentationOptions.alert.rawValue))
}
@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 is UNPushNotificationTrigger {
JPUSHService.handleRemoteNotification(userInfo)
}else {
//本地通知
}
//處理通知 跳到指定界面等等
receivePush(userInfo as! Dictionary<String, Any>)
completionHandler()
}
// iOS 9.0
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
JPUSHService.handleRemoteNotification(userInfo);
completionHandler(UIBackgroundFetchResult.newData);
}
3、推送處理
func receivePush(_ userInfo : Dictionary<String,Any>) {
//根據(jù)服務(wù)器返回的字段判斷
let userInfo = userInfo["key"] as! String
let jsonData:Data = userInfo.data(using: .utf8)!
var dict = try! JSONSerialization.jsonObject(with: jsonData, options: .allowFragments) as! [String:AnyObject]
let messageJson = JSON(dict ["data"]!)
let message = Message.init(json: messageJson)
if message.type == 2
{
let rootView = getRootViewController()
rootView.pushViewController(MessageViewController.init(type: 2), animated: true)
}
// 角標(biāo)變0
UIApplication.shared.applicationIconBadgeNumber = 0
}
//獲取當(dāng)前頁面的是控制器
func getRootViewController () ->(UINavigationController)
{
let rootViewController = UIApplication.shared.keyWindow?.rootViewController
if (rootViewController?.isKind(of: TabBarViewController.self))!
{
//let firstController = rootViewController?.childViewControllers[tabBarController.selectedIndex]
let firstController = tabBarController.selectedViewController
if (firstController?.isKind(of: UINavigationController.self))! {
return firstController as! UINavigationController
}else
{
return UINavigationController.init(rootViewController: firstController!)
}
}else if (rootViewController?.isKind(of: UINavigationController.self))!
{
return rootViewController as! UINavigationController
}
return UINavigationController.init(rootViewController: rootViewController!)
}
func applicationDidBecomeActive(_ application: UIApplication) {
//角標(biāo)至0
UIApplication.shared.applicationIconBadgeNumber = 0
}
如有不妥脓钾,請多多指教售睹。