Advertisement
Guest User

Untitled

a guest
Oct 15th, 2019
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.26 KB | None | 0 0
  1. //
  2. // AppDelegate+PKPushRegistryDelegate.swift
  3. // AddaIOS
  4. //
  5. // Created by Alif on 30/1/19.
  6. // Copyright © 2019 Alif. All rights reserved.
  7. //
  8.  
  9. import PushKit
  10. import SwiftyJSON
  11. import UIKit
  12. import CallKit
  13.  
  14. // MARK: PKPushRegistryDelegate
  15. extension AppDelegate: PKPushRegistryDelegate {
  16.  
  17. func pushRegistry(_ registry: PKPushRegistry, didUpdate pushCredentials: PKPushCredentials, for type: PKPushType) {
  18.  
  19. //let characterSet: CharacterSet = CharacterSet( charactersIn: "<>" )
  20.  
  21. let voipToken = pushCredentials.token.map { String(format: "%02x", $0) }.joined()
  22.  
  23. voipPushToken = voipToken
  24. PerfectLocalAuth.voipToken = voipToken
  25.  
  26. if !voipPushToken.isEmpty {
  27. NSLog("VoIP token: \(voipToken)")
  28. AppUserDefaults.save(value: voipToken, forKey: .voIPToken)
  29. }
  30. }
  31.  
  32. func pushRegistry(_ registry: PKPushRegistry, didInvalidatePushTokenFor type: PKPushType) {
  33. log.error(#line, "invalid \(self)")
  34. }
  35.  
  36. @available(iOS, introduced: 8.0, deprecated: 11.0)
  37. func pushRegistry(
  38. _ registry: PKPushRegistry,
  39. didReceiveIncomingPushWith payload: PKPushPayload,
  40. for type: PKPushType) {
  41. // guard type == .voIP, let aps = payload.dictionaryPayload["aps"] as? JSONDictionary else {
  42. // log.error(#line, "aps missing \(self)")
  43. // return
  44. // }
  45. //
  46. // let callManager = CallManager()
  47. // self.providerDelegate = ProviderDelegate(callManager: callManager)
  48. //
  49. // let extra = aps["extra"] as? [String: Any]
  50. // let dateString = extra?["date"] as? String
  51. //
  52. // let callerString = aps["alert"] as? String
  53. // guard let dic = convertJsonStringToDictionary(callerString!),
  54. // let receiverID = dic["receiverID"] as? String,
  55. // let callerID = dic["callerID"] as? String,
  56. // let mobileNumber = dic["mobileNumber"] as? String,
  57. // let userName = dic["userName"] as? String,
  58. // let uuidString = dic["uuid"] as? String,
  59. // let hasVideoAndDataChannel = dic["hasVideoAndDataChannel"] as? Bool,
  60. // let selfID = dic["receiverID"] as? String
  61. //
  62. // else {
  63. // log.error(#line, "missing partner id \(self)")
  64. // return
  65. // }
  66. //
  67. // let receiverAvatar = dic["receiverAvatar"] as? String ?? PerfectLocalAuth.avatarUrl
  68. //
  69. // DispatchQueue.main.async {
  70. // guard selfID == PerfectLocalAuth.userID else {
  71. // // checking if this user id and device token are
  72. // log.error(#line, "call not for you \(self)")
  73. // return
  74. // }
  75. // }
  76. //
  77. // if let timeInterval = Double(dateString!), (timeInterval + 65) < Date().timeIntervalSince1970 {
  78. // log.info(#line, "Call Expired")
  79. // log.info(#line, "\((timeInterval + 65)) < \(Date().timeIntervalSince1970)")
  80. // return
  81. // }
  82. //
  83. // let uuid = UUID(uuidString: uuidString)
  84. //
  85. // let profile = ProfileRes(firstname: userName, picture: receiverAvatar, lastname: userName, dateOfBirth: 0, currentCityAndCountry: String.empty, ownerId: callerID, id: uuidString, gender: String.empty, mobileNumber: mobileNumber, profileOwnerID: receiverID)
  86. //
  87. // let call = Call(uuid: uuid!, opponentUser: profile, isOutgoing: false, isVideoCall: hasVideoAndDataChannel)
  88. //
  89. // switch UIApplication.shared.applicationState {
  90. // case .active:
  91. // // foreground
  92. // printTypeOf(self)
  93. // log.info(#line, "appState active")
  94. // incomingCallWhenAppStateActive(call, callManager)
  95. // case .inactive, .background:
  96. // // backgound
  97. // printTypeOf(self)
  98. // log.info(#line, "appState on background")
  99. // incomingCallWhenAppStateBackground(call, callManager)
  100. // default:
  101. // break
  102. // }
  103. }
  104.  
  105. @available(iOS 11.0, *)
  106. func pushRegistry(
  107. _ registry: PKPushRegistry,
  108. didReceiveIncomingPushWith payload: PKPushPayload,
  109. for type: PKPushType, completion: @escaping () -> Void) {
  110.  
  111. // guard type == .voIP, let aps = payload.dictionaryPayload["aps"] as? JSONDictionary else {
  112. // log.error(#line, "aps missing \(self)")
  113. // return
  114. // }
  115. //
  116. // let extra = aps["extra"] as? [String: Any]
  117. // let dateString = extra?["date"] as? String
  118. //
  119. // let callerString = aps["alert"] as? String
  120. // guard let dic = convertJsonStringToDictionary(callerString!),
  121. // let receiverID = dic["receiverID"] as? String,
  122. // let callerID = dic["callerID"] as? String,
  123. // let mobileNumber = dic["mobileNumber"] as? String,
  124. // let userName = dic["userName"] as? String,
  125. // let uuidString = dic["uuid"] as? String,
  126. // let hasVideoAndDataChannel = dic["hasVideoAndDataChannel"] as? Bool,
  127. // let selfID = dic["receiverID"] as? String
  128. //
  129. // else {
  130. // log.error(#line, "missing partner id \(self)")
  131. // return
  132. // }
  133. //
  134. // let receiverAvatar = dic["receiverAvatar"] as? String ?? PerfectLocalAuth.avatarUrl
  135. //
  136. // DispatchQueue.main.async {
  137. // guard selfID == PerfectLocalAuth.userID else {
  138. // // checking if this user id and device token are
  139. // log.error(#line, "call not for you \(self)")
  140. // return
  141. // }
  142. // }
  143. //
  144. // if let timeInterval = Double(dateString!), (timeInterval + 65) < Date().timeIntervalSince1970 {
  145. // log.info(#line, "Call Expired")
  146. // log.info(#line, "\((timeInterval + 65)) < \(Date().timeIntervalSince1970)")
  147. // return
  148. // }
  149. //
  150. // let uuid = UUID(uuidString: uuidString)
  151. //
  152. // let profile = ProfileRes(firstname: userName, picture: receiverAvatar, lastname: userName, dateOfBirth: 0, currentCityAndCountry: String.empty, ownerId: callerID, id: uuidString, gender: String.empty, mobileNumber: mobileNumber, profileOwnerID: receiverID)
  153. //
  154. // let call = Call(uuid: uuid!, opponentUser: profile, isOutgoing: false, isVideoCall: hasVideoAndDataChannel)
  155. //
  156. // let callManager = CallManager()
  157. // callManager.isVideoCall = call.isVideoCall
  158. // self.providerDelegate = ProviderDelegate(callManager: callManager)
  159. //
  160. // switch UIApplication.shared.applicationState {
  161. // case .active:
  162. // // foreground
  163. // printTypeOf(self)
  164. // log.info(#line, "appState active")
  165. // incomingCallWhenAppStateActive(call, callManager)
  166. // case .inactive, .background:
  167. // // backgound
  168. // printTypeOf(self)
  169. // log.info(#line, "appState on background")
  170. // incomingCallWhenAppStateBackground(call, callManager)
  171. // default:
  172. // break
  173. // }
  174.  
  175. if type == .voIP {
  176. print(#line, "lets fire PKPushPayload voIP")
  177. callManager = CallManager()
  178.  
  179. guard let callManager = callManager else {
  180. debugPrint(#line, "callManager is nil from APD")
  181. return
  182. }
  183.  
  184. self.providerDelegate = ProviderDelegate(callManager: callManager)
  185.  
  186. // Extract the call information from the push notification payload
  187. if let handle = payload.dictionaryPayload["aps"] as? JSONDictionary {
  188.  
  189. let extra = handle["extra"] as? [String: Any]
  190. let dateString = extra?["date"] as? String
  191.  
  192. let callerString = handle["alert"] as? String
  193. guard let dic = convertJsonStringToDictionary(callerString!),
  194. let receiverID = dic["receiverID"] as? String,
  195. let callerID = dic["callerID"] as? String,
  196. let mobileNumber = dic["mobileNumber"] as? String,
  197. let userName = dic["userName"] as? String,
  198. let uuidString = dic["uuid"] as? String,
  199. let callUUID = UUID(uuidString: uuidString),
  200. let hasVideoAndDataChannel = dic["hasVideoAndDataChannel"] as? Bool
  201.  
  202. else {
  203. log.error(#line, "missing partner id \(self)")
  204. return
  205. }
  206.  
  207. let receiverAvatar = dic["receiverAvatar"] as? String ?? PerfectLocalAuth.avatarUrl
  208. let profile = ProfileRes(firstname: userName, picture: receiverAvatar, lastname: userName, dateOfBirth: 0, currentCityAndCountry: String.empty, ownerId: callerID, id: uuidString, gender: String.empty, mobileNumber: mobileNumber, profileOwnerID: receiverID)
  209.  
  210. let call = VoipCall(uuid: UUID(uuidString: uuidString)!, opponentUser: profile, isOutgoing: false, isVideoCall: hasVideoAndDataChannel)
  211. callManager.call = call
  212.  
  213. print(#line, callUUID)
  214.  
  215. // Configure the call information data structures.
  216. let callUpdate = CXCallUpdate()
  217. let phoneNumber = CXHandle(type: .phoneNumber, value: mobileNumber)
  218. callUpdate.remoteHandle = phoneNumber
  219. callUpdate.supportsHolding = true
  220. callUpdate.hasVideo = hasVideoAndDataChannel
  221.  
  222. if let timeInterval = Double(dateString!), (timeInterval + 45) < Date().timeIntervalSince1970 {
  223. log.info(#line, "Call Expired")
  224. log.info(#line, "\((timeInterval + 65)) < \(Date().timeIntervalSince1970)")
  225. return
  226. }
  227.  
  228. callManager.add(call: call)
  229. callManager.isVideoCall = call.isVideoCall
  230.  
  231. switch UIApplication.shared.applicationState {
  232. case .active:
  233. // foreground
  234. print(#line, "active")
  235. DispatchQueue.main.async {
  236. if let callManager = self.callManager { callManager.acceptCurrentCall(opponentUser: call.opponentUser) }
  237. }
  238. case .inactive, .background:
  239. // backgound
  240. print(#line, ".inactive, .background")
  241. configureAudioSession()
  242. self.providerDelegate.callProvider.reportNewIncomingCall(with: callUUID,
  243. update: callUpdate, completion: { (error) in
  244. if error == nil {
  245. // If the system allows the call to proceed, make a data record for it.
  246.  
  247. }
  248. // Tell PushKit that the notification is handled.
  249. completion()
  250. })
  251. default:
  252. break
  253. }
  254.  
  255. // Asynchronously register with the telephony server and
  256. // process the call. Report updates to CallKit as needed.
  257. //establishConnection(for: callUUID)
  258. callManager.connect { }
  259.  
  260. }
  261.  
  262. }
  263. }
  264.  
  265. func endAllCalls() {
  266. // if #available(iOS 10.0, *) {
  267. // callManager!.calls.forEach({ call in
  268. // callManager!.end(call: call)
  269. // })
  270. // }
  271. }
  272. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement