5050import com .twilio .voice .LogLevel ;
5151import com .twilio .voice .RegistrationException ;
5252import com .twilio .voice .RegistrationListener ;
53+ import com .twilio .voice .UnregistrationListener ;
5354import com .twilio .voice .Voice ;
5455
5556import java .util .HashSet ;
@@ -93,6 +94,7 @@ public class TwilioVoiceModule extends ReactContextBaseJavaModule implements Act
9394 static Map <String , Integer > callNotificationMap ;
9495
9596 private RegistrationListener registrationListener = registrationListener ();
97+ private UnregistrationListener unregistrationListener = unregistrationListener ();
9698 private Call .Listener callListener = callListener ();
9799
98100 private CallInvite activeCallInvite ;
@@ -235,6 +237,22 @@ public void onError(@NonNull RegistrationException error,
235237 };
236238 }
237239
240+ private UnregistrationListener unregistrationListener () {
241+ return new UnregistrationListener () {
242+ @ Override
243+ public void onUnregistered (String accessToken , String fcmToken ) {
244+ if (BuildConfig .DEBUG ) {
245+ Log .d (TAG , "Successfully unregistered FCM" );
246+ }
247+ }
248+
249+ @ Override
250+ public void onError (RegistrationException error , String accessToken , String fcmToken ) {
251+ Log .e (TAG , String .format ("Unregistration Error: %d, %s" , error .getErrorCode (), error .getMessage ()));
252+ }
253+ };
254+ }
255+
238256 private Call .Listener callListener () {
239257 return new Call .Listener () {
240258 /*
@@ -659,6 +677,39 @@ private void registerForCallInvites() {
659677 Voice .register (accessToken , Voice .RegistrationChannel .FCM , fcmToken , registrationListener );
660678 }
661679
680+ /*
681+ * Unregister your android device with Twilio
682+ *
683+ */
684+
685+ @ ReactMethod //
686+ public void unregister (Promise promise ) {
687+ unregisterForCallInvites ();
688+ promise .resolve (true );
689+ }
690+
691+ private void unregisterForCallInvites () {
692+ FirebaseInstanceId .getInstance ().getInstanceId ()
693+ .addOnCompleteListener (new OnCompleteListener <InstanceIdResult >() {
694+ @ Override
695+ public void onComplete (@ NonNull Task <InstanceIdResult > task ) {
696+ if (!task .isSuccessful ()) {
697+ Log .w (TAG , "FCM unregistration failed" , task .getException ());
698+ return ;
699+ }
700+
701+ // Get new Instance ID token
702+ String fcmToken = task .getResult ().getToken ();
703+ if (fcmToken != null ) {
704+ if (BuildConfig .DEBUG ) {
705+ Log .d (TAG , "Unregistering with FCM" );
706+ }
707+ Voice .unregister (accessToken , Voice .RegistrationChannel .FCM , fcmToken , unregistrationListener );
708+ }
709+ }
710+ });
711+ }
712+
662713 public void acceptFromIntent (Intent intent ) {
663714 if (BuildConfig .DEBUG ) {
664715 Log .d (TAG , "acceptFromIntent()" );
@@ -687,6 +738,12 @@ public void accept() {
687738 if (BuildConfig .DEBUG ) {
688739 Log .d (TAG , "accept()" );
689740 }
741+
742+ Intent intent = new Intent (getReactApplicationContext (), IncomingCallNotificationService .class );
743+ intent .setAction (Constants .ACTION_JS_ANSWER );
744+
745+ getReactApplicationContext ().startService (intent );
746+
690747 AcceptOptions acceptOptions = new AcceptOptions .Builder ()
691748 .enableDscp (true )
692749 .build ();
@@ -703,6 +760,12 @@ public void reject() {
703760 params .putString (Constants .CALL_TO , activeCallInvite .getTo ());
704761 activeCallInvite .reject (getReactApplicationContext ());
705762 }
763+
764+ Intent intent = new Intent (getReactApplicationContext (), IncomingCallNotificationService .class );
765+ intent .setAction (Constants .ACTION_JS_REJECT );
766+
767+ getReactApplicationContext ().startService (intent );
768+
706769 eventManager .sendEvent (EVENT_CALL_INVITE_CANCELLED , params );
707770 activeCallInvite = null ;
708771 }
0 commit comments