|
import ExpoModulesCore |
|
import Foundation |
|
import PushKit |
|
|
|
class VoipPushDelegate: NSObject, PKPushRegistryDelegate { |
|
|
|
var onTokenReceived: ((String) -> Void)? |
|
var onIncomingPush: ((PKPushPayload) -> Void)? |
|
|
|
func pushRegistry( |
|
_ registry: PKPushRegistry, didUpdate pushCredentials: PKPushCredentials, for type: PKPushType |
|
) { |
|
guard type == .voIP else { return } |
|
|
|
let tokenData = pushCredentials.token |
|
let tokenParts = tokenData.map { String(format: "%02x", $0) } |
|
let token = tokenParts.joined() |
|
|
|
onTokenReceived?(token) |
|
} |
|
|
|
func pushRegistry( |
|
_ registry: PKPushRegistry, |
|
didReceiveIncomingPushWith payload: PKPushPayload, |
|
for type: PKPushType, |
|
completion: @escaping () -> Void |
|
) { |
|
guard type == .voIP else { |
|
completion() |
|
return |
|
} |
|
|
|
onIncomingPush?(payload) |
|
|
|
completion() |
|
} |
|
} |
|
|
|
public final class ExpoVoipPushTokenModule: Module { |
|
private var voipRegistry: PKPushRegistry? |
|
private let delegate = VoipPushDelegate() |
|
private var currentVoipToken: String? |
|
|
|
private func initializePushRegistry() { |
|
let registry = PKPushRegistry(queue: .main) |
|
registry.delegate = delegate |
|
registry.desiredPushTypes = [.voIP] |
|
self.voipRegistry = registry |
|
|
|
delegate.onTokenReceived = { [weak self] token in |
|
self?.currentVoipToken = token |
|
self?.sendEvent("onRegistration", ["voipToken": token]) |
|
} |
|
|
|
delegate.onIncomingPush = { [weak self] payload in |
|
let payloadDict = payload.dictionaryPayload |
|
|
|
self?.sendEvent("notification", ["payload": payloadDict]) |
|
} |
|
} |
|
|
|
public func definition() -> ModuleDefinition { |
|
|
|
Name("ExpoVoipPushToken") |
|
|
|
Events("onRegistration", "notification") |
|
|
|
Function("registerVoipPushToken") { |
|
initializePushRegistry() |
|
} |
|
} |
|
} |