123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178 |
- //
- // AppDelegate+UM.swift
- // RainbowPlanet
- //
- // Created by 南鑫林 on 2018/8/1.
- // Copyright © 2018年 南鑫林. All rights reserved.
- //
- import Foundation
- import AVFoundation
- var _entity : UMessageRegisterEntity?
- extension AppDelegate {
-
- /// 友盟初始化
- func initUM(launchOptions:[UIApplication.LaunchOptionsKey: Any]?) -> Void {
- //公共
- common()
- //推送
- push(launchOptions: launchOptions)
- //分享
- UMManager.shared().share()
-
- }
-
- ///公共
- func common() -> Void {
- //将自动采集页面信息
- // MobClick.setAutoPageEnabled(true)
- //开发者需要显式的调用此函数,日志系统才能工作
- UMCommonLogManager.setUp()
- //打开加密传输
- UMConfigure.setEncryptEnabled(true)
- //设置打开日志
- UMConfigure.setLogEnabled(true)
- //设置Key
- UMConfigure.initWithAppkey(kUMengAppKey, channel: "App Store")
- //开启Crash收集
- MobClick.setCrashReportEnabled(true)
- //默认为普通应用场景,目前还支持游戏统计场景
- MobClick.setScenarioType(eScenarioType.E_UM_NORMAL)
- //获得集成测试需要device_id
- let deice_id = UMConfigure.deviceIDForIntegration()
- if deice_id != nil {
- print("服务器端成功返回deviceID:\(deice_id!)");
- }else {
- print("服务器端还没有返回deviceID");
- }
- }
-
- ///推送
- func push(launchOptions:[UIApplication.LaunchOptionsKey: Any]?) -> Void {
-
- _entity = UMessageRegisterEntity.init()
- //type是对推送的几个参数的选择,可以选择一个或者多个。默认是三个全部打开,即:声音,弹窗,角标
- _entity?.types = Int(UInt8(UMessageAuthorizationOptions.badge.rawValue)|UInt8(UMessageAuthorizationOptions.alert.rawValue)|UInt8(UMessageAuthorizationOptions.sound.rawValue))
-
- if #available(iOS 8.0, *) {
- if #available(iOS 10.0, *) {
-
- let action1_ios10 = UNNotificationAction(identifier: "action1_identifier", title: "打开应用", options: UNNotificationActionOptions.foreground)
- let action2_ios10 = UNNotificationAction(identifier: "action2_identifier", title: "忽略", options: UNNotificationActionOptions.foreground)
-
- let category1_ios10 = UNNotificationCategory(identifier: "category1", actions: [action1_ios10,action2_ios10], intentIdentifiers: [], options: UNNotificationCategoryOptions.customDismissAction)
- //UNNotificationCategoryOptionNone
- //UNNotificationCategoryOptionCustomDismissAction 清除通知被触发会走通知的代理方法
- //UNNotificationCategoryOptionAllowInCarPlay 适用于行车模式
- let categories = NSSet(objects: category1_ios10)
- _entity?.categories = (categories as! Set<AnyHashable>)
- UNUserNotificationCenter.current().delegate = self
-
- } else {
- let action1 = UIMutableUserNotificationAction.init()
- action1.identifier = "action1_identifier"
- action1.title = "打开应用"
- action1.activationMode = UIUserNotificationActivationMode.foreground;//当点击的时候启动程序
-
- let action2 = UIMutableUserNotificationAction.init()
- action2.identifier = "action2_identifier"
- action2.title = "忽略"
- action2.activationMode = UIUserNotificationActivationMode.background;//当点击的时候不启动程序,在后台处理
- action2.isAuthenticationRequired = true;//需要解锁才能处理,如果action.activationMode = UIUserNotificationActivationModeForeground;则这个属性被忽略;
- action2.isDestructive = true;
-
- let actionCategory1 = UIMutableUserNotificationCategory.init()
- actionCategory1.identifier = "category1"//这组动作的唯一标示
- actionCategory1.setActions([action1,action2], for: UIUserNotificationActionContext.default)
- let categories = NSSet(objects: actionCategory1)
- _entity?.categories = (categories as! Set<AnyHashable>)
- }
- }
- UMessage.registerForRemoteNotifications(launchOptions: launchOptions, entity: _entity) { (granted, error) in
- if granted {
-
- }else {
-
- }
- }
- UMessage.setBadgeClear(true)//设置是否允许SDK自动清空角标
- }
- }
- @available(iOS 10.0, *)
- // MARK: - UNUserNotificationCenterDelegate
- extension AppDelegate:UNUserNotificationCenterDelegate {
- func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
- let userInfo = notification.request.content.userInfo
-
- if (notification.request.trigger?.isKind(of: UNPushNotificationTrigger.self))! {
- UMessage.setAutoAlert(true)
- //应用处于前台时的远程推送接受
- //必须加这句代码
- UMessage.didReceiveRemoteNotification(userInfo)
- }else {
- //应用处于前台时的本地推送接受
- }
- completionHandler(UNNotificationPresentationOptions(rawValue: UNNotificationPresentationOptions.sound.rawValue|UNNotificationPresentationOptions.alert.rawValue|UNNotificationPresentationOptions.badge.rawValue))
- }
-
- func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
- let userInfo = response.notification.request.content.userInfo
- if (response.notification.request.trigger?.isKind(of: UNPushNotificationTrigger.self))! {
- UMessage.setAutoAlert(true)
- //应用处于前台时的远程推送接受
- //必须加这句代码
- UMessage.didReceiveRemoteNotification(userInfo)
- }else {
- //应用处于前台时的本地推送接受
- }
- }
- }
- // MARK: - 接受通知
- extension AppDelegate {
-
- //iOS10以下使用这两个方法接收通知,
- func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
- //关闭友盟自带的弹出框
- UMessage.setAutoAlert(true)
- if #available(iOS 9.0, *) {
- if #available(iOS 10.0, *){
- }else {
- UMessage.didReceiveRemoteNotification(userInfo)
- completionHandler(UIBackgroundFetchResult.newData);
- }
- }
- }
-
- func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) {
- //关闭友盟自带的弹出框
- UMessage.setAutoAlert(true)
- if #available(iOS 9.0, *) {
- if #available(iOS 10.0, *){
- }else {
- UMessage.didReceiveRemoteNotification(userInfo)
- }
- }
- }
- /// 获取设备的 DeviceToken
- func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
- NXLLog(deviceToken.description.replacingOccurrences(of: "<", with: "").replacingOccurrences(of: ">", with: "").replacingOccurrences(of: " ", with: ""))
- }
- }
|