Skip to content

Commit c5f6277

Browse files
committed
feat: parse caller Id (client/number) in reportingIncomingCall
1 parent e5a70c0 commit c5f6277

File tree

1 file changed

+57
-49
lines changed

1 file changed

+57
-49
lines changed

ios/Classes/SwiftTwilioVoicePlugin.swift

Lines changed: 57 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -693,49 +693,49 @@ public class SwiftTwilioVoicePlugin: NSObject, FlutterPlugin, FlutterStreamHand
693693
let request = UNNotificationRequest(identifier: UUID().uuidString,
694694
content: content,
695695
trigger: trigger)
696-
696+
697697
notificationCenter.add(request) { (error) in
698698
if let error = error {
699699
print("Notification Error: ", error)
700700
}
701701
}
702-
702+
703703
}
704704
}
705705
}
706-
706+
707707
// MARK: TVOCallDelegate
708708
public func callDidStartRinging(call: Call) {
709709
let direction = (self.callOutgoing ? "Outgoing" : "Incoming")
710710
let from = (call.from ?? self.identity)
711711
let to = (call.to ?? self.callTo)
712712
self.sendPhoneCallEvents(description: "Ringing|\(from)|\(to)|\(direction)", isError: false)
713-
713+
714714
//self.placeCallButton.setTitle("Ringing", for: .normal)
715715
}
716-
716+
717717
public func callDidConnect(call: Call) {
718718
let direction = (self.callOutgoing ? "Outgoing" : "Incoming")
719719
let from = (call.from ?? self.identity)
720720
let to = (call.to ?? self.callTo)
721721
self.sendPhoneCallEvents(description: "Connected|\(from)|\(to)|\(direction)", isError: false)
722-
722+
723723
if let callKitCompletionCallback = callKitCompletionCallback {
724724
callKitCompletionCallback(true)
725725
}
726-
726+
727727
toggleAudioRoute(toSpeaker: false)
728728
}
729-
729+
730730
public func call(call: Call, isReconnectingWithError error: Error) {
731731
self.sendPhoneCallEvents(description: "Reconnecting", isError: false)
732-
732+
733733
}
734-
734+
735735
public func callDidReconnect(call: Call) {
736736
self.sendPhoneCallEvents(description: "Reconnected", isError: false)
737737
}
738-
738+
739739
public func callDidFailToConnect(call: Call, error: Error) {
740740
self.sendPhoneCallEvents(description: "LOG|Call failed to connect: \(error.localizedDescription)", isError: false)
741741
self.sendPhoneCallEvents(description: "Call Ended", isError: false)
@@ -747,47 +747,47 @@ public class SwiftTwilioVoicePlugin: NSObject, FlutterPlugin, FlutterStreamHand
747747
if let completion = self.callKitCompletionCallback {
748748
completion(false)
749749
}
750-
751-
750+
751+
752752
callKitProvider.reportCall(with: call.uuid!, endedAt: Date(), reason: CXCallEndedReason.failed)
753753
callDisconnected()
754754
}
755-
755+
756756
public func callDidDisconnect(call: Call, error: Error?) {
757757
self.sendPhoneCallEvents(description: "Call Ended", isError: false)
758758
if let error = error {
759759
self.sendPhoneCallEvents(description: "Call Failed: \(error.localizedDescription)", isError: true)
760760
}
761-
761+
762762
if !self.userInitiatedDisconnect {
763763
var reason = CXCallEndedReason.remoteEnded
764764
self.sendPhoneCallEvents(description: "LOG|User initiated disconnect", isError: false)
765765
if error != nil {
766766
reason = .failed
767767
}
768-
768+
769769
self.callKitProvider.reportCall(with: call.uuid!, endedAt: Date(), reason: reason)
770770
}
771-
771+
772772
callDisconnected()
773773
}
774-
774+
775775
func callDisconnected() {
776776
self.sendPhoneCallEvents(description: "LOG|Call Disconnected", isError: false)
777777
if (self.call != nil) {
778-
778+
779779
self.sendPhoneCallEvents(description: "LOG|Setting call to nil", isError: false)
780780
self.call = nil
781781
}
782782
if (self.callInvite != nil) {
783783
self.callInvite = nil
784784
}
785-
785+
786786
self.callOutgoing = false
787787
self.userInitiatedDisconnect = false
788-
788+
789789
}
790-
790+
791791
func isSpeakerOn() -> Bool {
792792
// Source: https://stackoverflow.com/a/51759708/4628115
793793
let currentRoute = AVAudioSession.sharedInstance().currentRoute
@@ -824,37 +824,37 @@ public class SwiftTwilioVoicePlugin: NSObject, FlutterPlugin, FlutterStreamHand
824824
}
825825
audioDevice.block()
826826
}
827-
827+
828828
// MARK: CXProviderDelegate
829829
public func providerDidReset(_ provider: CXProvider) {
830830
self.sendPhoneCallEvents(description: "LOG|providerDidReset:", isError: false)
831831
audioDevice.isEnabled = false
832832
}
833-
833+
834834
public func providerDidBegin(_ provider: CXProvider) {
835835
self.sendPhoneCallEvents(description: "LOG|providerDidBegin", isError: false)
836836
}
837-
837+
838838
public func provider(_ provider: CXProvider, didActivate audioSession: AVAudioSession) {
839839
self.sendPhoneCallEvents(description: "LOG|provider:didActivateAudioSession:", isError: false)
840840
audioDevice.isEnabled = true
841841
}
842-
842+
843843
public func provider(_ provider: CXProvider, didDeactivate audioSession: AVAudioSession) {
844844
self.sendPhoneCallEvents(description: "LOG|provider:didDeactivateAudioSession:", isError: false)
845845
audioDevice.isEnabled = false
846846
}
847-
847+
848848
public func provider(_ provider: CXProvider, timedOutPerforming action: CXAction) {
849849
self.sendPhoneCallEvents(description: "LOG|provider:timedOutPerformingAction:", isError: false)
850850
}
851-
851+
852852
public func provider(_ provider: CXProvider, perform action: CXStartCallAction) {
853853
self.sendPhoneCallEvents(description: "LOG|provider:performStartCallAction:", isError: false)
854-
855-
854+
855+
856856
provider.reportOutgoingCall(with: action.callUUID, startedConnectingAt: Date())
857-
857+
858858
self.performVoiceCall(uuid: action.callUUID, client: "") { (success) in
859859
if (success) {
860860
self.sendPhoneCallEvents(description: "LOG|provider:performAnswerVoiceCall() successful", isError: false)
@@ -865,26 +865,26 @@ public class SwiftTwilioVoicePlugin: NSObject, FlutterPlugin, FlutterStreamHand
865865
}
866866
action.fulfill()
867867
}
868-
868+
869869
public func provider(_ provider: CXProvider, perform action: CXAnswerCallAction) {
870870
self.sendPhoneCallEvents(description: "LOG|provider:performAnswerCallAction:", isError: false)
871-
872-
871+
872+
873873
self.performAnswerVoiceCall(uuid: action.callUUID) { (success) in
874874
if success {
875875
self.sendPhoneCallEvents(description: "LOG|provider:performAnswerVoiceCall() successful", isError: false)
876876
} else {
877877
self.sendPhoneCallEvents(description: "LOG|provider:performAnswerVoiceCall() failed:", isError: false)
878878
}
879879
}
880-
880+
881881
action.fulfill()
882882
}
883-
883+
884884
public func provider(_ provider: CXProvider, perform action: CXEndCallAction) {
885885
self.sendPhoneCallEvents(description: "LOG|provider:performEndCallAction:", isError: false)
886-
887-
886+
887+
888888
if (self.callInvite != nil) {
889889
self.sendPhoneCallEvents(description: "LOG|provider:performEndCallAction: rejecting call", isError: false)
890890
self.callInvite?.reject()
@@ -895,7 +895,7 @@ public class SwiftTwilioVoicePlugin: NSObject, FlutterPlugin, FlutterStreamHand
895895
}
896896
action.fulfill()
897897
}
898-
898+
899899
public func provider(_ provider: CXProvider, perform action: CXSetHeldCallAction) {
900900
self.sendPhoneCallEvents(description: "LOG|provider:performSetHeldAction:", isError: false)
901901
if let call = self.call {
@@ -905,32 +905,32 @@ public class SwiftTwilioVoicePlugin: NSObject, FlutterPlugin, FlutterStreamHand
905905
action.fail()
906906
}
907907
}
908-
908+
909909
public func provider(_ provider: CXProvider, perform action: CXSetMutedCallAction) {
910910
self.sendPhoneCallEvents(description: "LOG|provider:performSetMutedAction:", isError: false)
911-
911+
912912
if let call = self.call {
913913
call.isMuted = action.isMuted
914914
action.fulfill()
915915
} else {
916916
action.fail()
917917
}
918918
}
919-
919+
920920
// MARK: Call Kit Actions
921921
func performStartCallAction(uuid: UUID, handle: String) {
922922
let callHandle = CXHandle(type: .generic, value: handle)
923923
let startCallAction = CXStartCallAction(call: uuid, handle: callHandle)
924924
let transaction = CXTransaction(action: startCallAction)
925-
925+
926926
callKitCallController.request(transaction) { error in
927927
if let error = error {
928928
self.sendPhoneCallEvents(description: "LOG|StartCallAction transaction request failed: \(error.localizedDescription)", isError: false)
929929
return
930930
}
931-
931+
932932
self.sendPhoneCallEvents(description: "LOG|StartCallAction transaction request successful", isError: false)
933-
933+
934934
let callUpdate = CXCallUpdate()
935935
callUpdate.remoteHandle = callHandle
936936
callUpdate.localizedCallerName = self.clients[handle] ?? self.clients["defaultCaller"] ?? self.defaultCaller
@@ -939,17 +939,25 @@ public class SwiftTwilioVoicePlugin: NSObject, FlutterPlugin, FlutterStreamHand
939939
callUpdate.supportsGrouping = false
940940
callUpdate.supportsUngrouping = false
941941
callUpdate.hasVideo = false
942-
942+
943943
self.callKitProvider.reportCall(with: uuid, updated: callUpdate)
944944
}
945945
}
946-
946+
947947
func reportIncomingCall(from: String, uuid: UUID) {
948948
let callHandle = CXHandle(type: .generic, value: from)
949-
949+
950+
var callerName: String?;
951+
if(from.contains("client:")) {
952+
var clientName = from.replacingOccurrences(of: "client:", with: "")
953+
callerName = self.clients[clientName];
954+
} else {
955+
callerName = from
956+
}
957+
950958
let callUpdate = CXCallUpdate()
951959
callUpdate.remoteHandle = callHandle
952-
callUpdate.localizedCallerName = clients[from] ?? self.clients["defaultCaller"] ?? defaultCaller
960+
callUpdate.localizedCallerName = callerName ?? self.clients["defaultCaller"] ?? defaultCaller
953961
callUpdate.supportsDTMF = true
954962
callUpdate.supportsHolding = true
955963
callUpdate.supportsGrouping = false

0 commit comments

Comments
 (0)