@@ -673,14 +673,18 @@ public class SwiftTwilioVoicePlugin: NSObject, FlutterPlugin, FlutterStreamHand
673
673
let content = UNMutableNotificationContent ( )
674
674
var userName : String ?
675
675
if var from = from{
676
- from = from. replacingOccurrences ( of: " client: " , with: " " )
676
+ if ( from. contains ( " client: " ) ) {
677
+ from = from. replacingOccurrences ( of: " client: " , with: " " )
678
+ userName = self . clients [ from] ;
679
+ } else {
680
+ userName = from
681
+ }
677
682
content. userInfo = [ " type " : " twilio-missed-call " , " From " : from]
678
683
if let to = to{
679
684
content. userInfo [ " To " ] = to
680
685
}
681
- userName = self . clients [ from]
682
686
}
683
-
687
+
684
688
let title = userName ?? self . clients [ " defaultCaller " ] ?? self . defaultCaller
685
689
content. title = title
686
690
content. body = NSLocalizedString ( " notification_missed_call_body " , comment: " " )
@@ -689,49 +693,49 @@ public class SwiftTwilioVoicePlugin: NSObject, FlutterPlugin, FlutterStreamHand
689
693
let request = UNNotificationRequest ( identifier: UUID ( ) . uuidString,
690
694
content: content,
691
695
trigger: trigger)
692
-
696
+
693
697
notificationCenter. add ( request) { ( error) in
694
698
if let error = error {
695
699
print ( " Notification Error: " , error)
696
700
}
697
701
}
698
-
702
+
699
703
}
700
704
}
701
705
}
702
-
706
+
703
707
// MARK: TVOCallDelegate
704
708
public func callDidStartRinging( call: Call ) {
705
709
let direction = ( self . callOutgoing ? " Outgoing " : " Incoming " )
706
710
let from = ( call. from ?? self . identity)
707
711
let to = ( call. to ?? self . callTo)
708
712
self . sendPhoneCallEvents ( description: " Ringing| \( from) | \( to) | \( direction) " , isError: false )
709
-
713
+
710
714
//self.placeCallButton.setTitle("Ringing", for: .normal)
711
715
}
712
-
716
+
713
717
public func callDidConnect( call: Call ) {
714
718
let direction = ( self . callOutgoing ? " Outgoing " : " Incoming " )
715
719
let from = ( call. from ?? self . identity)
716
720
let to = ( call. to ?? self . callTo)
717
721
self . sendPhoneCallEvents ( description: " Connected| \( from) | \( to) | \( direction) " , isError: false )
718
-
722
+
719
723
if let callKitCompletionCallback = callKitCompletionCallback {
720
724
callKitCompletionCallback ( true )
721
725
}
722
-
726
+
723
727
toggleAudioRoute ( toSpeaker: false )
724
728
}
725
-
729
+
726
730
public func call( call: Call , isReconnectingWithError error: Error ) {
727
731
self . sendPhoneCallEvents ( description: " Reconnecting " , isError: false )
728
-
732
+
729
733
}
730
-
734
+
731
735
public func callDidReconnect( call: Call ) {
732
736
self . sendPhoneCallEvents ( description: " Reconnected " , isError: false )
733
737
}
734
-
738
+
735
739
public func callDidFailToConnect( call: Call , error: Error ) {
736
740
self . sendPhoneCallEvents ( description: " LOG|Call failed to connect: \( error. localizedDescription) " , isError: false )
737
741
self . sendPhoneCallEvents ( description: " Call Ended " , isError: false )
@@ -743,47 +747,47 @@ public class SwiftTwilioVoicePlugin: NSObject, FlutterPlugin, FlutterStreamHand
743
747
if let completion = self . callKitCompletionCallback {
744
748
completion ( false )
745
749
}
746
-
747
-
750
+
751
+
748
752
callKitProvider. reportCall ( with: call. uuid!, endedAt: Date ( ) , reason: CXCallEndedReason . failed)
749
753
callDisconnected ( )
750
754
}
751
-
755
+
752
756
public func callDidDisconnect( call: Call , error: Error ? ) {
753
757
self . sendPhoneCallEvents ( description: " Call Ended " , isError: false )
754
758
if let error = error {
755
759
self . sendPhoneCallEvents ( description: " Call Failed: \( error. localizedDescription) " , isError: true )
756
760
}
757
-
761
+
758
762
if !self . userInitiatedDisconnect {
759
763
var reason = CXCallEndedReason . remoteEnded
760
764
self . sendPhoneCallEvents ( description: " LOG|User initiated disconnect " , isError: false )
761
765
if error != nil {
762
766
reason = . failed
763
767
}
764
-
768
+
765
769
self . callKitProvider. reportCall ( with: call. uuid!, endedAt: Date ( ) , reason: reason)
766
770
}
767
-
771
+
768
772
callDisconnected ( )
769
773
}
770
-
774
+
771
775
func callDisconnected( ) {
772
776
self . sendPhoneCallEvents ( description: " LOG|Call Disconnected " , isError: false )
773
777
if ( self . call != nil ) {
774
-
778
+
775
779
self . sendPhoneCallEvents ( description: " LOG|Setting call to nil " , isError: false )
776
780
self . call = nil
777
781
}
778
782
if ( self . callInvite != nil ) {
779
783
self . callInvite = nil
780
784
}
781
-
785
+
782
786
self . callOutgoing = false
783
787
self . userInitiatedDisconnect = false
784
-
788
+
785
789
}
786
-
790
+
787
791
func isSpeakerOn( ) -> Bool {
788
792
// Source: https://stackoverflow.com/a/51759708/4628115
789
793
let currentRoute = AVAudioSession . sharedInstance ( ) . currentRoute
@@ -820,37 +824,37 @@ public class SwiftTwilioVoicePlugin: NSObject, FlutterPlugin, FlutterStreamHand
820
824
}
821
825
audioDevice. block ( )
822
826
}
823
-
827
+
824
828
// MARK: CXProviderDelegate
825
829
public func providerDidReset( _ provider: CXProvider ) {
826
830
self . sendPhoneCallEvents ( description: " LOG|providerDidReset: " , isError: false )
827
831
audioDevice. isEnabled = false
828
832
}
829
-
833
+
830
834
public func providerDidBegin( _ provider: CXProvider ) {
831
835
self . sendPhoneCallEvents ( description: " LOG|providerDidBegin " , isError: false )
832
836
}
833
-
837
+
834
838
public func provider( _ provider: CXProvider , didActivate audioSession: AVAudioSession ) {
835
839
self . sendPhoneCallEvents ( description: " LOG|provider:didActivateAudioSession: " , isError: false )
836
840
audioDevice. isEnabled = true
837
841
}
838
-
842
+
839
843
public func provider( _ provider: CXProvider , didDeactivate audioSession: AVAudioSession ) {
840
844
self . sendPhoneCallEvents ( description: " LOG|provider:didDeactivateAudioSession: " , isError: false )
841
845
audioDevice. isEnabled = false
842
846
}
843
-
847
+
844
848
public func provider( _ provider: CXProvider , timedOutPerforming action: CXAction ) {
845
849
self . sendPhoneCallEvents ( description: " LOG|provider:timedOutPerformingAction: " , isError: false )
846
850
}
847
-
851
+
848
852
public func provider( _ provider: CXProvider , perform action: CXStartCallAction ) {
849
853
self . sendPhoneCallEvents ( description: " LOG|provider:performStartCallAction: " , isError: false )
850
-
851
-
854
+
855
+
852
856
provider. reportOutgoingCall ( with: action. callUUID, startedConnectingAt: Date ( ) )
853
-
857
+
854
858
self . performVoiceCall ( uuid: action. callUUID, client: " " ) { ( success) in
855
859
if ( success) {
856
860
self . sendPhoneCallEvents ( description: " LOG|provider:performAnswerVoiceCall() successful " , isError: false )
@@ -861,26 +865,26 @@ public class SwiftTwilioVoicePlugin: NSObject, FlutterPlugin, FlutterStreamHand
861
865
}
862
866
action. fulfill ( )
863
867
}
864
-
868
+
865
869
public func provider( _ provider: CXProvider , perform action: CXAnswerCallAction ) {
866
870
self . sendPhoneCallEvents ( description: " LOG|provider:performAnswerCallAction: " , isError: false )
867
-
868
-
871
+
872
+
869
873
self . performAnswerVoiceCall ( uuid: action. callUUID) { ( success) in
870
874
if success {
871
875
self . sendPhoneCallEvents ( description: " LOG|provider:performAnswerVoiceCall() successful " , isError: false )
872
876
} else {
873
877
self . sendPhoneCallEvents ( description: " LOG|provider:performAnswerVoiceCall() failed: " , isError: false )
874
878
}
875
879
}
876
-
880
+
877
881
action. fulfill ( )
878
882
}
879
-
883
+
880
884
public func provider( _ provider: CXProvider , perform action: CXEndCallAction ) {
881
885
self . sendPhoneCallEvents ( description: " LOG|provider:performEndCallAction: " , isError: false )
882
-
883
-
886
+
887
+
884
888
if ( self . callInvite != nil ) {
885
889
self . sendPhoneCallEvents ( description: " LOG|provider:performEndCallAction: rejecting call " , isError: false )
886
890
self . callInvite? . reject ( )
@@ -891,7 +895,7 @@ public class SwiftTwilioVoicePlugin: NSObject, FlutterPlugin, FlutterStreamHand
891
895
}
892
896
action. fulfill ( )
893
897
}
894
-
898
+
895
899
public func provider( _ provider: CXProvider , perform action: CXSetHeldCallAction ) {
896
900
self . sendPhoneCallEvents ( description: " LOG|provider:performSetHeldAction: " , isError: false )
897
901
if let call = self . call {
@@ -901,32 +905,32 @@ public class SwiftTwilioVoicePlugin: NSObject, FlutterPlugin, FlutterStreamHand
901
905
action. fail ( )
902
906
}
903
907
}
904
-
908
+
905
909
public func provider( _ provider: CXProvider , perform action: CXSetMutedCallAction ) {
906
910
self . sendPhoneCallEvents ( description: " LOG|provider:performSetMutedAction: " , isError: false )
907
-
911
+
908
912
if let call = self . call {
909
913
call. isMuted = action. isMuted
910
914
action. fulfill ( )
911
915
} else {
912
916
action. fail ( )
913
917
}
914
918
}
915
-
919
+
916
920
// MARK: Call Kit Actions
917
921
func performStartCallAction( uuid: UUID , handle: String ) {
918
922
let callHandle = CXHandle ( type: . generic, value: handle)
919
923
let startCallAction = CXStartCallAction ( call: uuid, handle: callHandle)
920
924
let transaction = CXTransaction ( action: startCallAction)
921
-
925
+
922
926
callKitCallController. request ( transaction) { error in
923
927
if let error = error {
924
928
self . sendPhoneCallEvents ( description: " LOG|StartCallAction transaction request failed: \( error. localizedDescription) " , isError: false )
925
929
return
926
930
}
927
-
931
+
928
932
self . sendPhoneCallEvents ( description: " LOG|StartCallAction transaction request successful " , isError: false )
929
-
933
+
930
934
let callUpdate = CXCallUpdate ( )
931
935
callUpdate. remoteHandle = callHandle
932
936
callUpdate. localizedCallerName = self . clients [ handle] ?? self . clients [ " defaultCaller " ] ?? self . defaultCaller
@@ -935,17 +939,25 @@ public class SwiftTwilioVoicePlugin: NSObject, FlutterPlugin, FlutterStreamHand
935
939
callUpdate. supportsGrouping = false
936
940
callUpdate. supportsUngrouping = false
937
941
callUpdate. hasVideo = false
938
-
942
+
939
943
self . callKitProvider. reportCall ( with: uuid, updated: callUpdate)
940
944
}
941
945
}
942
-
946
+
943
947
func reportIncomingCall( from: String , uuid: UUID ) {
944
948
let callHandle = CXHandle ( type: . generic, value: from)
945
-
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
+
946
958
let callUpdate = CXCallUpdate ( )
947
959
callUpdate. remoteHandle = callHandle
948
- callUpdate. localizedCallerName = clients [ from ] ?? self . clients [ " defaultCaller " ] ?? defaultCaller
960
+ callUpdate. localizedCallerName = callerName ?? self . clients [ " defaultCaller " ] ?? defaultCaller
949
961
callUpdate. supportsDTMF = true
950
962
callUpdate. supportsHolding = true
951
963
callUpdate. supportsGrouping = false
0 commit comments