友盟推播 ( iOS 8 以上 )
-
- 友盟官方設定
-
- 實作
- 註冊友盟帳號吧~
憑證問題可以參考這篇,圖文教學非常詳細。
- 手動 到官網勾選並且下載。
- Cocoapod
pod 'UMCCommon'
pod 'UMCPush'
pod 'UMCSecurityPlugins'
更詳細的說明請看官網圖文教學
寫出來的原因是之後假如要上架或者要修改一些專案的相關變數,可以很方便地在一個 file 裡面去修改就可以了。
public final class Config {
static let share = Config()
let keyUmeng: String = ""
/// 上架記得改成 false
let isEnableUmengLog: Bool = true
let channelID: String = ""
}
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
self.setupUmeng(launchOptions: launchOptions)
return true
}
func setupUmeng(launchOptions: [UIApplicationLaunchOptionsKey: Any]?) {
/// 友盟初始化
UMConfigure.initWithAppkey(Config.share.keyUmeng, channel: Config.share.channelID)
UMConfigure.setLogEnabled(Config.share.isEnableUmengLog)
/// 友盟統計
MobClick.setScenarioType(eScenarioType.E_UM_NORMAL)
/// iOS 10 以上必須支援
if #available(iOS 10.0, *) {
UNUserNotificationCenter.current().delegate = self
}
/// 友盟推送配置
let entity = UMessageRegisterEntity.init()
entity.types = Int(UMessageAuthorizationOptions.alert.rawValue) |
Int(UMessageAuthorizationOptions.badge.rawValue) |
Int(UMessageAuthorizationOptions.sound.rawValue)
UMessage.registerForRemoteNotifications(launchOptions: launchOptions, entity: entity) { (granted, error) in
if let error = error {
print(error.localizedDescription)
}
}
UMessage.setAutoAlert(false)
}
/// 拿到 Device Token
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
gc_HighLightPrint(msg: deviceToken.hexString)
}
/// 註冊推送失敗
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
gc_NegativePrint(msg: error.localizedDescription)
}
/// 接到推送訊息
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {
UMessage.didReceiveRemoteNotification(userInfo)
}
/// iOS10 以前接收的方法
func application(_ application: UIApplication,
handleActionWithIdentifier identifier: String?,
for notification: UILocalNotification,
withResponseInfo responseInfo: [AnyHashable: Any],
completionHandler: @escaping () -> Void) {
/// 这个方法用来做action点击的统计
UMessage.sendClickReport(forRemoteNotification: responseInfo)
}
@available(iOS 10.0, *)
extension AppDelegate: UNUserNotificationCenterDelegate {
/// iOS10 新增:當 App 在**前景**模式下
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
let userInfo: [AnyHashable: Any] = notification.request.content.userInfo
/// 處理遠程推送 ( Push Notification )
if notification.request.trigger?.isKind(of: UNPushNotificationTrigger.self) ?? false {
print("App 在**前景**模式下的遠程推送")
} else {
print("App 在**前景**模式下的本地推送")
}
completionHandler([.sound, .badge])
}
/// iOS10 新增:當 App 在**背景**模式下
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
let userInfo: [AnyHashable: Any] = response.notification.request.content.userInfo
(response.notification.request.trigger?.isKind(of: UNPushNotificationTrigger.self) ?? false)
/// 處理遠程推送 ( Push Notification )
? print("App 在**背景**模式下的遠程推送")
/// 處理本地推送 ( Local Notification )
: print("App 在**背景**模式下的本地推送")
}
}