AppDelegate+UM.swift 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. //
  2. // AppDelegate+UM.swift
  3. // RainbowPlanet
  4. //
  5. // Created by 南鑫林 on 2018/8/1.
  6. // Copyright © 2018年 南鑫林. All rights reserved.
  7. //
  8. import Foundation
  9. import AVFoundation
  10. var _entity : UMessageRegisterEntity?
  11. extension AppDelegate {
  12. /// 友盟初始化
  13. func initUM(launchOptions:[UIApplication.LaunchOptionsKey: Any]?) -> Void {
  14. //公共
  15. common()
  16. //推送
  17. push(launchOptions: launchOptions)
  18. //分享
  19. UMManager.shared().share()
  20. }
  21. ///公共
  22. func common() -> Void {
  23. //将自动采集页面信息
  24. // MobClick.setAutoPageEnabled(true)
  25. //开发者需要显式的调用此函数,日志系统才能工作
  26. UMCommonLogManager.setUp()
  27. //打开加密传输
  28. UMConfigure.setEncryptEnabled(true)
  29. //设置打开日志
  30. UMConfigure.setLogEnabled(true)
  31. //设置Key
  32. UMConfigure.initWithAppkey(kUMengAppKey, channel: "App Store")
  33. //开启Crash收集
  34. MobClick.setCrashReportEnabled(true)
  35. //默认为普通应用场景,目前还支持游戏统计场景
  36. MobClick.setScenarioType(eScenarioType.E_UM_NORMAL)
  37. //获得集成测试需要device_id
  38. let deice_id = UMConfigure.deviceIDForIntegration()
  39. if deice_id != nil {
  40. print("服务器端成功返回deviceID:\(deice_id!)");
  41. }else {
  42. print("服务器端还没有返回deviceID");
  43. }
  44. }
  45. ///推送
  46. func push(launchOptions:[UIApplication.LaunchOptionsKey: Any]?) -> Void {
  47. _entity = UMessageRegisterEntity.init()
  48. //type是对推送的几个参数的选择,可以选择一个或者多个。默认是三个全部打开,即:声音,弹窗,角标
  49. _entity?.types = Int(UInt8(UMessageAuthorizationOptions.badge.rawValue)|UInt8(UMessageAuthorizationOptions.alert.rawValue)|UInt8(UMessageAuthorizationOptions.sound.rawValue))
  50. if #available(iOS 8.0, *) {
  51. if #available(iOS 10.0, *) {
  52. let action1_ios10 = UNNotificationAction(identifier: "action1_identifier", title: "打开应用", options: UNNotificationActionOptions.foreground)
  53. let action2_ios10 = UNNotificationAction(identifier: "action2_identifier", title: "忽略", options: UNNotificationActionOptions.foreground)
  54. let category1_ios10 = UNNotificationCategory(identifier: "category1", actions: [action1_ios10,action2_ios10], intentIdentifiers: [], options: UNNotificationCategoryOptions.customDismissAction)
  55. //UNNotificationCategoryOptionNone
  56. //UNNotificationCategoryOptionCustomDismissAction 清除通知被触发会走通知的代理方法
  57. //UNNotificationCategoryOptionAllowInCarPlay 适用于行车模式
  58. let categories = NSSet(objects: category1_ios10)
  59. _entity?.categories = (categories as! Set<AnyHashable>)
  60. UNUserNotificationCenter.current().delegate = self
  61. } else {
  62. let action1 = UIMutableUserNotificationAction.init()
  63. action1.identifier = "action1_identifier"
  64. action1.title = "打开应用"
  65. action1.activationMode = UIUserNotificationActivationMode.foreground;//当点击的时候启动程序
  66. let action2 = UIMutableUserNotificationAction.init()
  67. action2.identifier = "action2_identifier"
  68. action2.title = "忽略"
  69. action2.activationMode = UIUserNotificationActivationMode.background;//当点击的时候不启动程序,在后台处理
  70. action2.isAuthenticationRequired = true;//需要解锁才能处理,如果action.activationMode = UIUserNotificationActivationModeForeground;则这个属性被忽略;
  71. action2.isDestructive = true;
  72. let actionCategory1 = UIMutableUserNotificationCategory.init()
  73. actionCategory1.identifier = "category1"//这组动作的唯一标示
  74. actionCategory1.setActions([action1,action2], for: UIUserNotificationActionContext.default)
  75. let categories = NSSet(objects: actionCategory1)
  76. _entity?.categories = (categories as! Set<AnyHashable>)
  77. }
  78. }
  79. UMessage.registerForRemoteNotifications(launchOptions: launchOptions, entity: _entity) { (granted, error) in
  80. if granted {
  81. }else {
  82. }
  83. }
  84. UMessage.setBadgeClear(true)//设置是否允许SDK自动清空角标
  85. }
  86. }
  87. @available(iOS 10.0, *)
  88. // MARK: - UNUserNotificationCenterDelegate
  89. extension AppDelegate:UNUserNotificationCenterDelegate {
  90. func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
  91. let userInfo = notification.request.content.userInfo
  92. if (notification.request.trigger?.isKind(of: UNPushNotificationTrigger.self))! {
  93. UMessage.setAutoAlert(true)
  94. //应用处于前台时的远程推送接受
  95. //必须加这句代码
  96. UMessage.didReceiveRemoteNotification(userInfo)
  97. }else {
  98. //应用处于前台时的本地推送接受
  99. }
  100. completionHandler(UNNotificationPresentationOptions(rawValue: UNNotificationPresentationOptions.sound.rawValue|UNNotificationPresentationOptions.alert.rawValue|UNNotificationPresentationOptions.badge.rawValue))
  101. }
  102. func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
  103. let userInfo = response.notification.request.content.userInfo
  104. if (response.notification.request.trigger?.isKind(of: UNPushNotificationTrigger.self))! {
  105. UMessage.setAutoAlert(true)
  106. //应用处于前台时的远程推送接受
  107. //必须加这句代码
  108. UMessage.didReceiveRemoteNotification(userInfo)
  109. }else {
  110. //应用处于前台时的本地推送接受
  111. }
  112. }
  113. }
  114. // MARK: - 接受通知
  115. extension AppDelegate {
  116. //iOS10以下使用这两个方法接收通知,
  117. func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
  118. //关闭友盟自带的弹出框
  119. UMessage.setAutoAlert(true)
  120. if #available(iOS 9.0, *) {
  121. if #available(iOS 10.0, *){
  122. }else {
  123. UMessage.didReceiveRemoteNotification(userInfo)
  124. completionHandler(UIBackgroundFetchResult.newData);
  125. }
  126. }
  127. }
  128. func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) {
  129. //关闭友盟自带的弹出框
  130. UMessage.setAutoAlert(true)
  131. if #available(iOS 9.0, *) {
  132. if #available(iOS 10.0, *){
  133. }else {
  134. UMessage.didReceiveRemoteNotification(userInfo)
  135. }
  136. }
  137. }
  138. /// 获取设备的 DeviceToken
  139. func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
  140. NXLLog(deviceToken.description.replacingOccurrences(of: "<", with: "").replacingOccurrences(of: ">", with: "").replacingOccurrences(of: " ", with: ""))
  141. }
  142. }