Search code examples
javaandroidtwitter-oauthtwitter-digits

Android Digits returns nothing


Why Android Digits do not always return onSuccess or onFalure after number verification? It just finish DigitsActivity and do nothing, but sometimes its okay and I receive onSuccess.

public void initForm(){
    Digits.getSessionManager().clearActiveSession();// http://stackoverflow.com/questions/31679089/fabric-digits-returns-a-null-object-of-phonenumber-after-otp-verification-on-and
    DigitsAuthConfig.Builder digitsAuthConfigBuilder = new DigitsAuthConfig.Builder()
            .withAuthCallBack(new AuthCallback() {
                @Override
                public void success(DigitsSession digitsSession, String s) {
                    Toast.makeText(LoginActivity.this, s, Toast.LENGTH_LONG).show();
                    TwitterAuthToken token = (TwitterAuthToken)digitsSession.getAuthToken();
                    DigitsOAuthSigning oauthSigning = new DigitsOAuthSigning(SapronApplication.conf(), token);
                    Map<String, String> authHeaders = oauthSigning.getOAuthEchoHeadersForVerifyCredentials();
                    requestToken(authHeaders.get("X-Verify-Credentials-Authorization"));

                }

                @Override
                public void failure(DigitsException e) {
                    Toast.makeText(LoginActivity.this, e.toString(), Toast.LENGTH_LONG).show();
                }
            })
            .withPhoneNumber("+7");


    Digits.authenticate(digitsAuthConfigBuilder.build());
}

Solution

  • I experienced the same problem and also phoneNumber returned as null like this question. I resolved the problem while using custom button. The main problem was not clearing the digits session properly and setting new AuthCallback in withAuthCallBack() in every click. The code that resolved my problem is given below. Hope it will help someone who need to use custom button for digits.....

    findViewById(R.id.customDigitsButton).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                verifyMobileUsingDigits();
            }
        });
    
    private AuthCallback authCallback;
    public void verifyMobileUsingDigits(){
        logoutFabricDigits("verifyMobileUsingDigits");// manual deletion of all digit sessions, as only Digits.getSessionManager().clearActiveSession() did not helped me.....
        if(authCallback == null){
            authCallback = new AuthCallback() {
                @Override
                public void success(DigitsSession session, String phoneNumber) {
                    Toast.makeText(getApplicationContext(), "Authentication successful for " + phoneNumber, Toast.LENGTH_LONG).show();
                }
    
                @Override
                public void failure(DigitsException exception) {
                    Toast.makeText(getApplicationContext(), "Authentication failure", Toast.LENGTH_LONG).show();
                }
            };
        }
        DigitsAuthConfig.Builder digitsAuthConfigBuilder = new DigitsAuthConfig.Builder()
                .withAuthCallBack(authCallback)
                .withPhoneNumber("+880")
                .withEmailCollection(false)
                .withThemeResId(R.style.CustomDigitsTheme);
        Digits.authenticate(digitsAuthConfigBuilder.build());
    }
    
    public static void logoutFabricDigits(String TAG){
        try {
            Map<Long, DigitsSession> digitsSessionMap = Digits.getSessionManager().getSessionMap();
            if(digitsSessionMap != null){
                Set<Long> digitIdSet = new HashSet<>(digitsSessionMap.keySet());
                for(Long digitId : digitIdSet){
                    DigitsSession digitsSession = digitsSessionMap.get(digitId);
                    if(digitsSession != null){
                        Digits.getSessionManager().clearSession(digitsSession.getId());
                    }
                }
            }
            Digits.getSessionManager().clearActiveSession();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }