Swift3.0整合極光推送
現在很多程式都開始使用Swift開發了,但是第三方庫大多數都是用OC寫的,所以我們要使用Swift和OC混編。今天的內容主要講Swift3.0整合極光推送。
1.準備工作
整合指南,極光上說的都很清楚,把建立應用和配置工程實現。SDK下載地址。在橋接標頭檔案中新增
#import "JPUSHService.h"
// iOS10註冊APNs所需標頭檔案
#ifdef NSFoundationVersionNumber_iOS_9_x_Max
#import <UserNotifications/UserNotifications.h>
#endif
2.Swift3.0整合
(1)AppDelegate.swift中新增代理
import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate,JPUSHRegisterDelegate {
}
(2)註冊推送及處理應用未開啟時收到推送訊息
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// 通知註冊實體類
let entity = 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: "845b93e08c7fa192df019c07", channel:"Publish channel" , apsForProduction: false);
// 獲取推送訊息
let remote = launchOptions?[UIApplicationLaunchOptionsKey.remoteNotification] as? Dictionary<String,Any>;
// 如果remote不為空,就代表應用在未開啟的時候收到了推送訊息
if remote != nil {
// 收到推送訊息實現的方法
self.perform(#selector(receivePush), with: remote, afterDelay: 1.0);
}
return true;
}
(3)實現代理方法
// MARK: -JPUSHRegisterDelegate
// iOS 10.x 需要
@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);
}
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);
}
completionHandler();
// 應用開啟的時候收到推送訊息
NotificationCenter.default.post(name: NSNotification.Name(rawValue: NotificationName_ReceivePush), object: NotificationObject_Sueecess, userInfo: userInfo)
}
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
JPUSHService.handleRemoteNotification(userInfo);
completionHandler(UIBackgroundFetchResult.newData);
}
// 接收到推送實現的方法
func receivePush(_ userInfo : Dictionary<String,Any>) {
// 角標變0
UIApplication.shared.applicationIconBadgeNumber = 0;
// 剩下的根據需要自定義
self.tabBarVC?.selectedIndex = 0;
NotificationCenter.default.post(name: NSNotification.Name(rawValue: NotificationName_ReceivePush), object: NotificationObject_Sueecess, userInfo: userInfo)
}
}
我看網上沒有人寫用Swift3.0整合極光推送,自己整合的時候遇到了很多坑,所以分享出來,希望大家可以少浪費點大腦細胞☺。
相關文章
- Flutter應用整合極光推送Flutter
- iOS —— 極光推送和極光IMiOS
- 快捷地整合極光推送(JPush)到 Laravel 專案中Laravel
- 在 Laravel 中使用 極光推送Laravel
- Laravel 極光推送驅動,使用極光不再那麼麻煩!Laravel
- 極光筆記|極光推送在APICloud平臺的使用教程筆記APICloud
- APP訊息推送 極光推送 示例程式碼APP
- .NET對接極光訊息推送
- .NET快速對接極光訊息推送
- 極光筆記丨iOS 15推送新特性筆記iOS
- 極光推送申請iOS推送證書p12及配置流程iOS
- 極光推送申請iOS推送證書p12及配置教程iOS
- [外掛擴充套件]APP極光推送外掛!套件APP
- 極光推送demo在android studio中無法執行Android
- 極光筆記 | 極光服務的信創改造實踐筆記
- 在Android裝置上使用極光推送id重複的原因分析和解決辦法Android
- 極光筆記丨Spark SQL 在極光的建設實踐筆記SparkSQL
- 極光筆記 | 極光PUSH服務助力企業提升搶單速度筆記
- 極光筆記|百億級KV儲存在極光的運維實踐之路筆記運維
- Android免費整合推送功能(MobPush)Android
- SpringBoot極簡整合ShiroSpring Boot
- 【Flutter 專題】63 圖解 Flutter 整合極光 JPush 小結|8月更文挑戰Flutter圖解
- 極光財報:2020年Q2極光營收1.308億元 同比下降55%營收
- 極光筆記|基於CMPP協議整合簡訊測試樁全流程實踐分享筆記協議
- Amazing!!CSS 也能實現極光?CSS
- GUET 2024夢極光pwn2
- 極光財報:2019年Q3極光毛利潤為5690萬元 同比增長7%
- 極光筆記 | 極光推出“運營增長”解決方案,開啟企業增長新引擎筆記
- 極光星空天文攝影LR預設
- Swift3.0 呼叫C函式-_silen_nameSwift函式
- 共赴璀璨約定!《光與夜之戀》極光測試今日開啟
- 極光筆記 | 大語言模型外掛筆記模型
- Swift2.3升級到Swift3.0小記Swift
- Jenkins環境搭建(7)-整合釘釘訊息推送Jenkins
- 二十週年專題|綠盟極光,用專注驚豔了時光
- 『技術分享』-- 使用極光 IM 構建聊天功能
- 極光HTTP之什麼是網路協議HTTP協議
- 極光筆記 | 如何在Shopify中使用EngageLab (下)筆記
- 極光筆記 | 如何在Shopify中使用Engagelab(上)筆記