Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,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 @@ -52,6 +52,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.HashMap;
Expand Down Expand Up @@ -118,6 +119,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 @@ -225,6 +227,26 @@ public void onError(RegistrationException error, String accessToken, String fcmT
};
}

private UnregistrationListener unregistrationListener() {
return new UnregistrationListener() {
@Override
public void onUnregistered(String accessToken, String fcmToken) {
if (BuildConfig.DEBUG) {
Log.d(TAG, "Successfully unregistered FCM");
}
eventManager.sendEvent(EVENT_DEVICE_NOT_READY, null);
}

@Override
public void onError(RegistrationException error, String accessToken, String fcmToken) {
Log.e(TAG, String.format("Unregistration Error: %d, %s", error.getErrorCode(), error.getMessage()));
WritableMap params = Arguments.createMap();
params.putString("err", error.getMessage());
eventManager.sendEvent(EVENT_DEVICE_NOT_READY, params);
}
};
}

private Call.Listener callListener() {
return new Call.Listener() {
/*
Expand Down Expand Up @@ -618,6 +640,41 @@ public void onComplete(@NonNull Task<InstanceIdResult> task) {
});
}

/*
* Unregister your android device with Twilio
*
*/

@ReactMethod
public void unregister(Promise promise) {
unregisterForCallInvites();
WritableMap params = Arguments.createMap();
params.putBoolean("initialized", false);
promise.resolve(params);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't you want to resolve the promise once onComplete is triggered in unregisterForCallInvites?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Jesse, you can fix this part, if you think it would be correct (or more accurate), as you know the library in more depth than I am.

}

private void unregisterForCallInvites() {
FirebaseInstanceId.getInstance().getInstanceId()
.addOnCompleteListener(new OnCompleteListener<InstanceIdResult>() {
@Override
public void onComplete(@NonNull Task<InstanceIdResult> task) {
if (!task.isSuccessful()) {
Log.w(TAG, "getInstanceId 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);
}
}
});
}

@ReactMethod
public void accept() {
callAccepted = true;
Expand Down
4 changes: 1 addition & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,7 @@ const Twilio = {
}
},
unregister() {
if (Platform.OS === IOS) {
TwilioVoice.unregister()
}
TwilioVoice.unregister()
},
addEventListener(type, handler) {
if (!_eventHandlers.hasOwnProperty(type)) {
Expand Down