Skip to content

Commit

Permalink
Twilio android sdk 5 bugfixes (#205)
Browse files Browse the repository at this point in the history
* feat: Unregistration fix for android and ios
* fix: issue with clearing heads-up notifications on android
  • Loading branch information
ro-mgh authored and fabriziomoscon committed Aug 2, 2021
1 parent 8e04e0b commit 9f27251
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,7 @@ TwilioVoice.getCallInvite()
}
})

// Unregister device with Twilio (iOS only)
// Unregister device with Twilio
TwilioVoice.unregister()
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -93,6 +94,7 @@ public class TwilioVoiceModule extends ReactContextBaseJavaModule implements Act
static Map<String, Integer> callNotificationMap;

private RegistrationListener registrationListener = registrationListener();
private UnregistrationListener unregistrationListener = unregistrationListener();
private Call.Listener callListener = callListener();

private CallInvite activeCallInvite;
Expand Down Expand Up @@ -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() {
/*
Expand Down Expand Up @@ -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<InstanceIdResult>() {
@Override
public void onComplete(@NonNull Task<InstanceIdResult> 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()");
Expand Down Expand Up @@ -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();
Expand All @@ -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;
}
Expand Down
4 changes: 1 addition & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,7 @@ const Twilio = {
}
},
unregister() {
if (Platform.OS === IOS) {
TwilioVoice.unregister()
}
TwilioVoice.unregister()
},
// getAudioDevices returns all audio devices connected
// {
Expand Down
2 changes: 2 additions & 0 deletions ios/RNTwilioVoice/RNTwilioVoice.m
Original file line number Diff line number Diff line change
Expand Up @@ -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.");
}
}];
Expand Down Expand Up @@ -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.");
}
}];
Expand Down

0 comments on commit 9f27251

Please sign in to comment.