19

I have developed an android app locally on my device (app not yet on android play store). I have the following logic to get deep link in MainActivity.

GoogleApiClient mGoogleApiClient = new GoogleApiClient.Builder(this)
            .enableAutoManage(this, null)
            .addApi(AppInvite.API)
            .build();

    // Check if this app was launched from a deep link. Setting autoLaunchDeepLink to true
    // would automatically launch the deep link if one is found.
    boolean autoLaunchDeepLink = false;
    AppInvite.AppInviteApi.getInvitation(mGoogleApiClient, this, autoLaunchDeepLink)
            .setResultCallback(
                    new ResultCallback<AppInviteInvitationResult>() {
                        @Override
                        public void onResult(@NonNull AppInviteInvitationResult result) {
                            if (result.getStatus().isSuccess()) {
                                // Extract deep link from Intent
                                Intent intent = result.getInvitationIntent();
                                String deepLink = AppInviteReferral.getDeepLink(intent);

                                Toast.makeText(getApplicationContext(), deepLink, Toast.LENGTH_LONG).show();
                                // Handle the deep link. For example, open the linked
                                // content, or apply promotional credit to the user's
                                // account.

                                // ...
                            } else {
                                Log.d(TAG, "getInvitation: no deep link found.");
                            }
                        }
                    });

I built some dynamic links using Firebase console and open in mobile browser. But it is not opening my app and reaching to line String deepLink = AppInviteReferral.getDeepLink(intent);

Instead it is opening the URL in mobile browser itself.

How to open the app and handle deep link in activity while using firebase dynamic link??

Edit:

I have intent filter in the manifest file.

<activity android:name="MainActivity"
        android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
            <action android:name="android.intent.action.VIEW"/>
            <category android:name="android.intent.category.DEFAULT"/>
            <category android:name="android.intent.category.BROWSABLE"/>
            <data android:host="example.com" android:scheme="http"/>
            <data android:host="example.com" android:scheme="https"/>
        </intent-filter>
    </activity>
GôTô
  • 7,974
  • 3
  • 32
  • 43
user3559471
  • 563
  • 2
  • 6
  • 17
  • 3
    Did you declare the intent filter in the manifest? Also, did you set up the console correctly with your SHA-1 fingerprint? – NSimon Jun 03 '16 at 09:42
  • @NicolasSimon, yes i have intent filter in manifest. and setup console correctly. Updated detail in question. – user3559471 Jun 03 '16 at 09:45
  • Just a side note - be mindfull to put the android:host under (just like stated above) otherwise link will not open correctly. this was my issue. – ilibilibom Jan 08 '20 at 09:10

6 Answers6

13

Action View and Action Main both are of different Intent category. So, you need to put them in different blocks like this:

 <activity android:name=".DynamicLinkActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        <intent-filter>
            <action android:name="android.intent.action.VIEW" />

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

            <data
                android:host="example.com"
                android:scheme="http" />
            <data
                android:host="example.com"
                android:scheme="https" />
        </intent-filter>
    </activity>
9

Alternatively, you could also provide the data in your intent-filter, like stated in the "regular" deep links doc (https://developer.android.com/training/app-indexing/deep-linking.html)

The intent filter would then look like the following :

        <intent-filter>
            <action android:name="android.intent.action.VIEW"/>
            <category android:name="android.intent.category.DEFAULT"/>
            <category android:name="android.intent.category.BROWSABLE"/>
            <data
                android:host="https://XXYYZZ42.app.goo.gl/"  // <- Replace with your deep link from the console
                android:scheme="https"/>
        </intent-filter>

That said link can be obtained in your console, right here : enter image description here

EDIT : Added the picture to show where to find this link

NSimon
  • 5,212
  • 2
  • 22
  • 36
  • While creating dynamic link in Firebase console, second required field is Deep Link. Description they have given - The deep link your app will open. This link must be a valid URL, and use HTTP or HTTPS scheme. See the 'link' parameter in the documentation. But when i created dynamic link i kept some random URL "abcd.com". Is that can be the issue for not launching app? – user3559471 Jun 03 '16 at 09:55
  • 1
    I tried new dynamic link with a valid URL. But that url is getting opened in mobile browser itself. I want that url in app after launching the app. – user3559471 Jun 03 '16 at 10:00
  • From my (short) experience with firebase deep links, I don't think the URL is an issue. I added an image to explain where to get the said link I was talking about (the one to put under data in the intent-filter). Did you try with this? – NSimon Jun 03 '16 at 10:00
  • Yes i tried with what you said and with valid url. It is launching url in mobile browser. Still app is not getting launched. – user3559471 Jun 03 '16 at 10:07
3

As explained in another answer, your intent filter seems to have some problems. Also your url may have some problems. When I was playing with those, I had created faulty URL to FireBase web site without noticing it. It is possible to test you code by opening the whole url in your app. I wrote all urls to be tested to an email and sent to myself, open in the device and started clicking. After that you can create the url you want in FireBase. Below are few examples (typos and other errors possible):

If you clicked this url on your device:

https://<myapp>.app.goo.gl/?link=https://mysite.fi/112972&apn=com.mydomain.myapp

and had this in you manifest:

 <intent-filter>
        <action android:name="android.intent.action.VIEW"/>
        <category android:name="android.intent.category.DEFAULT"/>
        <category android:name="android.intent.category.BROWSABLE"/>
        <data
            android:scheme="https"
            android:host="mysite.fi"
            android:pathPattern=".*" />
    </intent-filter>

It should open https://mysite.fi/112972 in your app (com.mydomain.myapp) and if you opened the link on your PC, it would open https://mysite.fi/112972 in the browser.

If you opened this url in your device:

https://<myapp>.app.goo.gl/?link=https://mysite.fi/112972&apn=com.mydomain.myapp&al=myscheme://mydeeplink/112972&afl=http://fallback.fi

and had this in you manifest:

 <intent-filter>
        <action android:name="android.intent.action.VIEW"/>
        <category android:name="android.intent.category.DEFAULT"/>
        <category android:name="android.intent.category.BROWSABLE"/>
        <data
            android:scheme="myscheme"
            android:host="mydeeplink"
            android:pathPattern=".*" />
    </intent-filter>

It would open myscheme://mydeeplink/112972 in your app (com.mydomain.myapp). You would need to have code for handling it. If the app is not installed, it would open http://fallback.fi in your browser. On the PC it would still open https://mysite.fi/112972.

(edit 19.3.2018) It seems that Firebase does not fully support 'al=' anymore. The code works, but it is missing from the documentation and Firebase console generated urls.

diidu
  • 2,823
  • 17
  • 32
  • hello, when typed to browser your secod solution doesnt work... Same as described here https://stackoverflow.com/questions/49430492/firebase-dynamic-link-when-typed-into-android-address-bar – Kebab Krabby Nov 03 '22 at 15:06
3

Update January 2019

Ensure that you have added the SHA256 certificate fingerprint for your app into your project in the Firebase console.

Add the below code in the AndroidManifest.xml under the activity which you want the dynamic link to lunch:

    <intent-filter android:autoVerify="true">
       <action android:name="android.intent.action.VIEW"/>
       <category android:name="android.intent.category.DEFAULT"/>
       <category android:name="android.intent.category.BROWSABLE"/>
       <data android:host="mydomainname.page.link" android:scheme="http"/>
       <data android:host="mydomainname.page.link" android:scheme="https"/>
   </intent-filter>
  • Remove android:autoVerify="true" if you have Android 6.0 (API level 23) and below or the app will crash.
Leo S
  • 159
  • 9
  • 1
    DynamicLink dynamicLink = FirebaseDynamicLinks.getInstance().createDynamicLink() .setLink(Uri.parse("https://www.example.com/")) .setDomainUriPrefix("https://example.page.link") .setAndroidParameters(new DynamicLink.AndroidParameters.Builder().build()) .buildDynamicLink(); Can you tell me what to add in .setLink() and .setDomainUriPrefix() for firebase based android app – Rahul Dange Jan 06 '19 at 18:41
  • setLink(Uri.parse("https://www.example.com/") "example" can be any name even if you don't have a real website. setDomainUriPrefix("https://example.page.link") you need to get "example" from your your firebase project dynamiclink . – Leo S Jan 09 '19 at 21:30
2

I had the same problem with deep links not working and the former answers wasn't helping. What did the trick was splitting the "data" tag like this:

Instead of:

<data android:host="example.com" android:scheme="http"/>
<data android:host="example.com" android:scheme="https"/>

Do this:

<data android:host="example.com"/>
<data android:scheme="http"/>
<data android:scheme="https"/>

Hope that will help anyone else :)

Shirane85
  • 2,255
  • 1
  • 26
  • 38
1

I recently found one missing thing in all the answers above. Please make sure that the exported=true to the activity in AndroidManifest.xml. It wasted my 2 hours of time to debug the same issue.

 <activity
            android:name=".Home"
            android:configChanges="orientation"
            android:exported="true"
            android:screenOrientation="portrait"
            android:theme="@style/AppTheme.NoActionBar"
            tools:ignore="AppLinkUrlError">
            <intent-filter>
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />

                <!-- the host should contain your domain. The pattern should be same as it's mentioned without any special char -->

                <data
                    android:host="example.com"
                    android:scheme="https" />

                <data
                    android:host="example.com"
                    android:scheme="http" />


            </intent-filter>
        </activity>
Vedant Jha
  • 21
  • 3