From 9f272519a1a8151b8ad5e94b722f9e521da5acb0 Mon Sep 17 00:00:00 2001 From: Roman Myagchenkov <58989752+ro-mgh@users.noreply.github.com> Date: Wed, 14 Jul 2021 01:00:06 +0700 Subject: [PATCH] Twilio android sdk 5 bugfixes (#205) * feat: Unregistration fix for android and ios * fix: issue with clearing heads-up notifications on android --- README.md | 2 +- .../hoxfon/react/RNTwilioVoice/Constants.java | 2 + .../IncomingCallNotificationService.java | 9 +++ .../RNTwilioVoice/TwilioVoiceModule.java | 63 +++++++++++++++++++ index.js | 4 +- ios/RNTwilioVoice/RNTwilioVoice.m | 2 + 6 files changed, 78 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index bf13eee5..9e30eb8e 100644 --- a/README.md +++ b/README.md @@ -565,7 +565,7 @@ TwilioVoice.getCallInvite() } }) -// Unregister device with Twilio (iOS only) +// Unregister device with Twilio TwilioVoice.unregister() ``` diff --git a/android/src/main/java/com/hoxfon/react/RNTwilioVoice/Constants.java b/android/src/main/java/com/hoxfon/react/RNTwilioVoice/Constants.java index 2a9a137a..f90b7413 100644 --- a/android/src/main/java/com/hoxfon/react/RNTwilioVoice/Constants.java +++ b/android/src/main/java/com/hoxfon/react/RNTwilioVoice/Constants.java @@ -26,6 +26,8 @@ public class Constants { public static final String ACTION_FCM_TOKEN = "ACTION_FCM_TOKEN"; public static final String ACTION_CLEAR_MISSED_CALLS_COUNT = "CLEAR_MISSED_CALLS_COUNT"; public static final String ACTION_OPEN_CALL_IN_PROGRESS = "CALL_IN_PROGRESS"; + public static final String ACTION_JS_ANSWER = "ACTION_JS_ANSWER"; + public static final String ACTION_JS_REJECT = "ACTION_JS_REJECT"; public static final String CALL_SID = "call_sid"; public static final String CALL_STATE = "call_state"; diff --git a/android/src/main/java/com/hoxfon/react/RNTwilioVoice/IncomingCallNotificationService.java b/android/src/main/java/com/hoxfon/react/RNTwilioVoice/IncomingCallNotificationService.java index 6891ceca..c78175c0 100644 --- a/android/src/main/java/com/hoxfon/react/RNTwilioVoice/IncomingCallNotificationService.java +++ b/android/src/main/java/com/hoxfon/react/RNTwilioVoice/IncomingCallNotificationService.java @@ -63,6 +63,14 @@ public int onStartCommand(Intent intent, int flags, int startId) { handleCancelledCall(intent); break; + case Constants.ACTION_JS_ANSWER: + endForeground(); + break; + + case Constants.ACTION_JS_REJECT: + endForeground(); + break; + default: break; } @@ -246,6 +254,7 @@ private void accept(CallInvite callInvite, int notificationId) { } private void reject(CallInvite callInvite, int notificationId) { + SoundPoolManager.getInstance(this).stopRinging(); endForeground(); callInvite.reject(getApplicationContext()); } diff --git a/android/src/main/java/com/hoxfon/react/RNTwilioVoice/TwilioVoiceModule.java b/android/src/main/java/com/hoxfon/react/RNTwilioVoice/TwilioVoiceModule.java index a08a7b50..e06fee70 100644 --- a/android/src/main/java/com/hoxfon/react/RNTwilioVoice/TwilioVoiceModule.java +++ b/android/src/main/java/com/hoxfon/react/RNTwilioVoice/TwilioVoiceModule.java @@ -50,6 +50,7 @@ import com.twilio.voice.LogLevel; import com.twilio.voice.RegistrationException; import com.twilio.voice.RegistrationListener; +import com.twilio.voice.UnregistrationListener; import com.twilio.voice.Voice; import java.util.HashSet; @@ -93,6 +94,7 @@ public class TwilioVoiceModule extends ReactContextBaseJavaModule implements Act static Map callNotificationMap; private RegistrationListener registrationListener = registrationListener(); + private UnregistrationListener unregistrationListener = unregistrationListener(); private Call.Listener callListener = callListener(); private CallInvite activeCallInvite; @@ -235,6 +237,22 @@ public void onError(@NonNull RegistrationException error, }; } + private UnregistrationListener unregistrationListener() { + return new UnregistrationListener() { + @Override + public void onUnregistered(String accessToken, String fcmToken) { + if (BuildConfig.DEBUG) { + Log.d(TAG, "Successfully unregistered FCM"); + } + } + + @Override + public void onError(RegistrationException error, String accessToken, String fcmToken) { + Log.e(TAG, String.format("Unregistration Error: %d, %s", error.getErrorCode(), error.getMessage())); + } + }; + } + private Call.Listener callListener() { return new Call.Listener() { /* @@ -659,6 +677,39 @@ private void registerForCallInvites() { Voice.register(accessToken, Voice.RegistrationChannel.FCM, fcmToken, registrationListener); } + /* + * Unregister your android device with Twilio + * + */ + + @ReactMethod // + public void unregister(Promise promise) { + unregisterForCallInvites(); + promise.resolve(true); + } + + private void unregisterForCallInvites() { + FirebaseInstanceId.getInstance().getInstanceId() + .addOnCompleteListener(new OnCompleteListener() { + @Override + public void onComplete(@NonNull Task task) { + if (!task.isSuccessful()) { + Log.w(TAG, "FCM unregistration failed", task.getException()); + return; + } + + // Get new Instance ID token + String fcmToken = task.getResult().getToken(); + if (fcmToken != null) { + if (BuildConfig.DEBUG) { + Log.d(TAG, "Unregistering with FCM"); + } + Voice.unregister(accessToken, Voice.RegistrationChannel.FCM, fcmToken, unregistrationListener); + } + } + }); + } + public void acceptFromIntent(Intent intent) { if (BuildConfig.DEBUG) { Log.d(TAG, "acceptFromIntent()"); @@ -687,6 +738,12 @@ public void accept() { if (BuildConfig.DEBUG) { Log.d(TAG, "accept()"); } + + Intent intent = new Intent(getReactApplicationContext(), IncomingCallNotificationService.class); + intent.setAction(Constants.ACTION_JS_ANSWER); + + getReactApplicationContext().startService(intent); + AcceptOptions acceptOptions = new AcceptOptions.Builder() .enableDscp(true) .build(); @@ -703,6 +760,12 @@ public void reject() { params.putString(Constants.CALL_TO, activeCallInvite.getTo()); activeCallInvite.reject(getReactApplicationContext()); } + + Intent intent = new Intent(getReactApplicationContext(), IncomingCallNotificationService.class); + intent.setAction(Constants.ACTION_JS_REJECT); + + getReactApplicationContext().startService(intent); + eventManager.sendEvent(EVENT_CALL_INVITE_CANCELLED, params); activeCallInvite = null; } diff --git a/index.js b/index.js index f0f29920..e472949b 100644 --- a/index.js +++ b/index.js @@ -92,9 +92,7 @@ const Twilio = { } }, unregister() { - if (Platform.OS === IOS) { - TwilioVoice.unregister() - } + TwilioVoice.unregister() }, // getAudioDevices returns all audio devices connected // { diff --git a/ios/RNTwilioVoice/RNTwilioVoice.m b/ios/RNTwilioVoice/RNTwilioVoice.m index 544c2a7f..13b2d0ad 100644 --- a/ios/RNTwilioVoice/RNTwilioVoice.m +++ b/ios/RNTwilioVoice/RNTwilioVoice.m @@ -154,6 +154,7 @@ - (void)dealloc { if (error) { NSLog(@"An error occurred while unregistering: %@", [error localizedDescription]); } else { + [[NSUserDefaults standardUserDefaults] setValue:@"" forKey:kCachedDeviceToken]; NSLog(@"Successfully unregistered for VoIP push notifications."); } }]; @@ -278,6 +279,7 @@ - (void)pushRegistry:(PKPushRegistry *)registry didInvalidatePushTokenForType:(P if (error) { NSLog(@"An error occurred while unregistering: %@", [error localizedDescription]); } else { + [[NSUserDefaults standardUserDefaults] setValue:@"" forKey:kCachedDeviceToken]; NSLog(@"Successfully unregistered for VoIP push notifications."); } }];