Skip to content

Commit 396e369

Browse files
authored
Twilio android sdk 5 bugfixes (#205)
* feat: Unregistration fix for android and ios * fix: issue with clearing heads-up notifications on android
1 parent c31506a commit 396e369

File tree

6 files changed

+78
-4
lines changed

6 files changed

+78
-4
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -565,7 +565,7 @@ TwilioVoice.getCallInvite()
565565
}
566566
})
567567

568-
// Unregister device with Twilio (iOS only)
568+
// Unregister device with Twilio
569569
TwilioVoice.unregister()
570570
```
571571

android/src/main/java/com/hoxfon/react/RNTwilioVoice/Constants.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ public class Constants {
2626
public static final String ACTION_FCM_TOKEN = "ACTION_FCM_TOKEN";
2727
public static final String ACTION_CLEAR_MISSED_CALLS_COUNT = "CLEAR_MISSED_CALLS_COUNT";
2828
public static final String ACTION_OPEN_CALL_IN_PROGRESS = "CALL_IN_PROGRESS";
29+
public static final String ACTION_JS_ANSWER = "ACTION_JS_ANSWER";
30+
public static final String ACTION_JS_REJECT = "ACTION_JS_REJECT";
2931

3032
public static final String CALL_SID = "call_sid";
3133
public static final String CALL_STATE = "call_state";

android/src/main/java/com/hoxfon/react/RNTwilioVoice/IncomingCallNotificationService.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,14 @@ public int onStartCommand(Intent intent, int flags, int startId) {
6363
handleCancelledCall(intent);
6464
break;
6565

66+
case Constants.ACTION_JS_ANSWER:
67+
endForeground();
68+
break;
69+
70+
case Constants.ACTION_JS_REJECT:
71+
endForeground();
72+
break;
73+
6674
default:
6775
break;
6876
}
@@ -246,6 +254,7 @@ private void accept(CallInvite callInvite, int notificationId) {
246254
}
247255

248256
private void reject(CallInvite callInvite, int notificationId) {
257+
SoundPoolManager.getInstance(this).stopRinging();
249258
endForeground();
250259
callInvite.reject(getApplicationContext());
251260
}

android/src/main/java/com/hoxfon/react/RNTwilioVoice/TwilioVoiceModule.java

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
import com.twilio.voice.LogLevel;
5151
import com.twilio.voice.RegistrationException;
5252
import com.twilio.voice.RegistrationListener;
53+
import com.twilio.voice.UnregistrationListener;
5354
import com.twilio.voice.Voice;
5455

5556
import 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
}

index.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,7 @@ const Twilio = {
8686
}
8787
},
8888
unregister() {
89-
if (Platform.OS === IOS) {
90-
TwilioVoice.unregister()
91-
}
89+
TwilioVoice.unregister()
9290
},
9391
// getAudioDevices returns all audio devices connected
9492
// {

ios/RNTwilioVoice/RNTwilioVoice.m

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ - (void)dealloc {
154154
if (error) {
155155
NSLog(@"An error occurred while unregistering: %@", [error localizedDescription]);
156156
} else {
157+
[[NSUserDefaults standardUserDefaults] setValue:@"" forKey:kCachedDeviceToken];
157158
NSLog(@"Successfully unregistered for VoIP push notifications.");
158159
}
159160
}];
@@ -278,6 +279,7 @@ - (void)pushRegistry:(PKPushRegistry *)registry didInvalidatePushTokenForType:(P
278279
if (error) {
279280
NSLog(@"An error occurred while unregistering: %@", [error localizedDescription]);
280281
} else {
282+
[[NSUserDefaults standardUserDefaults] setValue:@"" forKey:kCachedDeviceToken];
281283
NSLog(@"Successfully unregistered for VoIP push notifications.");
282284
}
283285
}];

0 commit comments

Comments
 (0)