Search code examples
androiddeep-linking

Redirect Uri not opening the Android App directly in OAuth2 - Deep link is already configured


Requirement: To open a URL and get the response back to the application and redirect the user back to my app since there are other API calls that follow.

<intent-filter
            android:autoVerify="true"
            android:order="1">
            <action android:name="android.intent.action.VIEW" />

            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.BROWSABLE" />

            <data
                android:host="ab.bc.cd"
               android:port="11001"/>
               <data android:scheme="https" />
                <data android:scheme="http" />
               <data android:pathPrefix="/abc.html" />
        </intent-filter>
    </activity>

Code to open the first api call in the browser:

if (!TextUtils.isEmpty(url)) {
        final CustomTabsIntent customTabsIntent = new CustomTabsIntent.Builder().build();
        CustomTabActivityHelper.openCustomTab(MainActivity.this, customTabsIntent, Uri.parse(url), new WebviewFallback() {
            @Override
            public void openUri(Activity activity, Uri uri) {
                    setupCustomTabs(uri);
            }
        });

Handling the Intent part here

  protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    final String schemeName = intent.getScheme();
    final Uri data = intent.getData();
    if (data != null && data.getHost() != null && data.getHost().equalsIgnoreCase("")) {
        return;
    }
    if (!TextUtils.isEmpty(schemeName) && schemeName.equalsIgnoreCase(UAEPassConstant.SCHEME)) {
        final Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.fragment_container);
        if (fragment != null && !(fragment instanceof DisplayFragment)) {
            setFragment(new DisplayFragment(), null, false);
        }
        final String encodedQuery = intent.getData().getEncodedQuery();
        if (!TextUtils.isEmpty(encodedQuery) && !encodedQuery.contains(UAEPassConstant.ERROR_DESCRIPTION)) {
            final String[] queryDataArray = encodedQuery.split("=|&"); // this is to fetch a code value which comes as a result of the first api call
            final LoginApiCall uaePassCall = new LoginApiCall(MainActivity.this, queryDataArray[1], new IUAEPassCallBack() {
                @Override
                public void onSuccessLogin(String userInfoJson, String tokenJson) {
                    Log.e("userInfo", userInfoJson);
                    Log.e("tokenJson", tokenJson);
                    final Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.fragment_container);
                    if (fragment instanceof DisplayFragment) {
                        ((DisplayFragment) fragment).LoggingData(userInfoJson);

                    }
                }

What happens: When I run this code everything works but when the first API call link opens in the browser and when I approve the login request for it, it shows the redirect URI in the browser itself and the control does not directly come back to my app. Instead, it freezes and then I need to click on "Open in chrome" (attached) and then it gives me a prompt whether I would like to open the app and on selecting that the control goes back to my app. Any pointers will be appreciated. Thanks in Advance! Edit: the code works properly during the first execution but second time I get this issue.


Solution

  • I did a lot of stuff but in the end when I changed the code to open custom tabs to :

        CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
    
                CustomTabsIntent customTabsIntent = builder.build();
                customTabsIntent.launchUrl(this, Uri.parse(url));
    

    Now, It works fine.

    NOTE: This alone doesnot work. I have checked almost every possible link related to this issue but turns out, the deep link I was using was wrong. Please be sure that you are setting the scheme and host properly. I have spent 4 days to figure out this.