Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // AppDelegate+PKPushRegistryDelegate.swift
- // AddaIOS
- //
- // Created by Alif on 30/1/19.
- // Copyright © 2019 Alif. All rights reserved.
- //
- import PushKit
- import SwiftyJSON
- import UIKit
- import CallKit
- // MARK: PKPushRegistryDelegate
- extension AppDelegate: PKPushRegistryDelegate {
- func pushRegistry(_ registry: PKPushRegistry, didUpdate pushCredentials: PKPushCredentials, for type: PKPushType) {
- //let characterSet: CharacterSet = CharacterSet( charactersIn: "<>" )
- let voipToken = pushCredentials.token.map { String(format: "%02x", $0) }.joined()
- voipPushToken = voipToken
- PerfectLocalAuth.voipToken = voipToken
- if !voipPushToken.isEmpty {
- NSLog("VoIP token: \(voipToken)")
- AppUserDefaults.save(value: voipToken, forKey: .voIPToken)
- }
- }
- func pushRegistry(_ registry: PKPushRegistry, didInvalidatePushTokenFor type: PKPushType) {
- log.error(#line, "invalid \(self)")
- }
- @available(iOS, introduced: 8.0, deprecated: 11.0)
- func pushRegistry(
- _ registry: PKPushRegistry,
- didReceiveIncomingPushWith payload: PKPushPayload,
- for type: PKPushType) {
- // guard type == .voIP, let aps = payload.dictionaryPayload["aps"] as? JSONDictionary else {
- // log.error(#line, "aps missing \(self)")
- // return
- // }
- //
- // let callManager = CallManager()
- // self.providerDelegate = ProviderDelegate(callManager: callManager)
- //
- // let extra = aps["extra"] as? [String: Any]
- // let dateString = extra?["date"] as? String
- //
- // let callerString = aps["alert"] as? String
- // guard let dic = convertJsonStringToDictionary(callerString!),
- // let receiverID = dic["receiverID"] as? String,
- // let callerID = dic["callerID"] as? String,
- // let mobileNumber = dic["mobileNumber"] as? String,
- // let userName = dic["userName"] as? String,
- // let uuidString = dic["uuid"] as? String,
- // let hasVideoAndDataChannel = dic["hasVideoAndDataChannel"] as? Bool,
- // let selfID = dic["receiverID"] as? String
- //
- // else {
- // log.error(#line, "missing partner id \(self)")
- // return
- // }
- //
- // let receiverAvatar = dic["receiverAvatar"] as? String ?? PerfectLocalAuth.avatarUrl
- //
- // DispatchQueue.main.async {
- // guard selfID == PerfectLocalAuth.userID else {
- // // checking if this user id and device token are
- // log.error(#line, "call not for you \(self)")
- // return
- // }
- // }
- //
- // if let timeInterval = Double(dateString!), (timeInterval + 65) < Date().timeIntervalSince1970 {
- // log.info(#line, "Call Expired")
- // log.info(#line, "\((timeInterval + 65)) < \(Date().timeIntervalSince1970)")
- // return
- // }
- //
- // let uuid = UUID(uuidString: uuidString)
- //
- // 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)
- //
- // let call = Call(uuid: uuid!, opponentUser: profile, isOutgoing: false, isVideoCall: hasVideoAndDataChannel)
- //
- // switch UIApplication.shared.applicationState {
- // case .active:
- // // foreground
- // printTypeOf(self)
- // log.info(#line, "appState active")
- // incomingCallWhenAppStateActive(call, callManager)
- // case .inactive, .background:
- // // backgound
- // printTypeOf(self)
- // log.info(#line, "appState on background")
- // incomingCallWhenAppStateBackground(call, callManager)
- // default:
- // break
- // }
- }
- @available(iOS 11.0, *)
- func pushRegistry(
- _ registry: PKPushRegistry,
- didReceiveIncomingPushWith payload: PKPushPayload,
- for type: PKPushType, completion: @escaping () -> Void) {
- // guard type == .voIP, let aps = payload.dictionaryPayload["aps"] as? JSONDictionary else {
- // log.error(#line, "aps missing \(self)")
- // return
- // }
- //
- // let extra = aps["extra"] as? [String: Any]
- // let dateString = extra?["date"] as? String
- //
- // let callerString = aps["alert"] as? String
- // guard let dic = convertJsonStringToDictionary(callerString!),
- // let receiverID = dic["receiverID"] as? String,
- // let callerID = dic["callerID"] as? String,
- // let mobileNumber = dic["mobileNumber"] as? String,
- // let userName = dic["userName"] as? String,
- // let uuidString = dic["uuid"] as? String,
- // let hasVideoAndDataChannel = dic["hasVideoAndDataChannel"] as? Bool,
- // let selfID = dic["receiverID"] as? String
- //
- // else {
- // log.error(#line, "missing partner id \(self)")
- // return
- // }
- //
- // let receiverAvatar = dic["receiverAvatar"] as? String ?? PerfectLocalAuth.avatarUrl
- //
- // DispatchQueue.main.async {
- // guard selfID == PerfectLocalAuth.userID else {
- // // checking if this user id and device token are
- // log.error(#line, "call not for you \(self)")
- // return
- // }
- // }
- //
- // if let timeInterval = Double(dateString!), (timeInterval + 65) < Date().timeIntervalSince1970 {
- // log.info(#line, "Call Expired")
- // log.info(#line, "\((timeInterval + 65)) < \(Date().timeIntervalSince1970)")
- // return
- // }
- //
- // let uuid = UUID(uuidString: uuidString)
- //
- // 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)
- //
- // let call = Call(uuid: uuid!, opponentUser: profile, isOutgoing: false, isVideoCall: hasVideoAndDataChannel)
- //
- // let callManager = CallManager()
- // callManager.isVideoCall = call.isVideoCall
- // self.providerDelegate = ProviderDelegate(callManager: callManager)
- //
- // switch UIApplication.shared.applicationState {
- // case .active:
- // // foreground
- // printTypeOf(self)
- // log.info(#line, "appState active")
- // incomingCallWhenAppStateActive(call, callManager)
- // case .inactive, .background:
- // // backgound
- // printTypeOf(self)
- // log.info(#line, "appState on background")
- // incomingCallWhenAppStateBackground(call, callManager)
- // default:
- // break
- // }
- if type == .voIP {
- print(#line, "lets fire PKPushPayload voIP")
- callManager = CallManager()
- guard let callManager = callManager else {
- debugPrint(#line, "callManager is nil from APD")
- return
- }
- self.providerDelegate = ProviderDelegate(callManager: callManager)
- // Extract the call information from the push notification payload
- if let handle = payload.dictionaryPayload["aps"] as? JSONDictionary {
- let extra = handle["extra"] as? [String: Any]
- let dateString = extra?["date"] as? String
- let callerString = handle["alert"] as? String
- guard let dic = convertJsonStringToDictionary(callerString!),
- let receiverID = dic["receiverID"] as? String,
- let callerID = dic["callerID"] as? String,
- let mobileNumber = dic["mobileNumber"] as? String,
- let userName = dic["userName"] as? String,
- let uuidString = dic["uuid"] as? String,
- let callUUID = UUID(uuidString: uuidString),
- let hasVideoAndDataChannel = dic["hasVideoAndDataChannel"] as? Bool
- else {
- log.error(#line, "missing partner id \(self)")
- return
- }
- let receiverAvatar = dic["receiverAvatar"] as? String ?? PerfectLocalAuth.avatarUrl
- 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)
- let call = VoipCall(uuid: UUID(uuidString: uuidString)!, opponentUser: profile, isOutgoing: false, isVideoCall: hasVideoAndDataChannel)
- callManager.call = call
- print(#line, callUUID)
- // Configure the call information data structures.
- let callUpdate = CXCallUpdate()
- let phoneNumber = CXHandle(type: .phoneNumber, value: mobileNumber)
- callUpdate.remoteHandle = phoneNumber
- callUpdate.supportsHolding = true
- callUpdate.hasVideo = hasVideoAndDataChannel
- if let timeInterval = Double(dateString!), (timeInterval + 45) < Date().timeIntervalSince1970 {
- log.info(#line, "Call Expired")
- log.info(#line, "\((timeInterval + 65)) < \(Date().timeIntervalSince1970)")
- return
- }
- callManager.add(call: call)
- callManager.isVideoCall = call.isVideoCall
- switch UIApplication.shared.applicationState {
- case .active:
- // foreground
- print(#line, "active")
- DispatchQueue.main.async {
- if let callManager = self.callManager { callManager.acceptCurrentCall(opponentUser: call.opponentUser) }
- }
- case .inactive, .background:
- // backgound
- print(#line, ".inactive, .background")
- configureAudioSession()
- self.providerDelegate.callProvider.reportNewIncomingCall(with: callUUID,
- update: callUpdate, completion: { (error) in
- if error == nil {
- // If the system allows the call to proceed, make a data record for it.
- }
- // Tell PushKit that the notification is handled.
- completion()
- })
- default:
- break
- }
- // Asynchronously register with the telephony server and
- // process the call. Report updates to CallKit as needed.
- //establishConnection(for: callUUID)
- callManager.connect { }
- }
- }
- }
- func endAllCalls() {
- // if #available(iOS 10.0, *) {
- // callManager!.calls.forEach({ call in
- // callManager!.end(call: call)
- // })
- // }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement