简体   繁体   中英

Firebase PendingDynamicLinkData in Android is null

Here are the full steps what I have done:

  1. Have updated firebase.json with the following:

    "appAssociation": "AUTO", "rewrites": [ { "source": "/product/**", "dynamicLinks": true } ]

  2. My website domain example.com is hosted in Firebase. So, uploaded firebase.json using firebase deploy --only hosting

  3. Added the following Dynamic URL prefix in Firebase Dynamic Links

    https://example.com/product

Screenshot of Firebase Dynamic Link

  1. Implemented bom, dynamic-links and analytics in build.gradle(:app) file
implementation platform('com.google.firebase:firebase-bom:29.0.3')

implementation 'com.google.firebase:firebase-dynamic-links'

implementation 'com.google.firebase:firebase-analytics'
  1. Created a Dynamic link using the following code:
private void handleFirebaseDynamicLink() {
        DynamicLink dynamicLink = FirebaseDynamicLinks.getInstance().createDynamicLink()
                .setLink(Uri.parse("https://example.com/product/?productSKU=" + ProductSKU))
                .setDomainUriPrefix("https://example.com/product/")
                .setAndroidParameters(
                        new DynamicLink.AndroidParameters.Builder("com.myapp.myapp").build())
                .buildDynamicLink();


        try {
            String url = URLDecoder.decode(dynamicLink.getUri().toString(), "UTF-8");
            Log.d(TAG, "handleFirebaseDynamicLink: " + url);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }
  1. Dynamic Link generated from above method is:
https://example.com/product/?apn=com.myapp.myapp&link=https://example.com/product/?productSKU=SKU-0004
  1. To receive the dynamic link have created the following intent filter to my activity in AndroidManifest.xml file:
<activity
            android:name=".Product_Details">

            <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="example.com"
                    android:pathPrefix="/product"
                    android:scheme="https" />
            </intent-filter>

</activity>
  1. A method in onCreate() by calling -
//Fetching productSKU
fetchProductSKU(getIntent());

receives the dynamic link as follows:

private void fetchProductSKU(Intent intent) {
        if (intent.getAction() != null) {
            if (intent.getAction().equals(Intent.ACTION_VIEW)) {
                FirebaseDynamicLinks.getInstance()
                        .getDynamicLink(intent)
                        .addOnSuccessListener(this, new OnSuccessListener<PendingDynamicLinkData>() {
                            @Override
                            public void onSuccess(PendingDynamicLinkData pendingDynamicLinkData) {
                                // Get deep link from result (may be null if no link is found)
                                Uri deepLink = null;
                                if (pendingDynamicLinkData != null) {
                                    deepLink = pendingDynamicLinkData.getLink();
                                    Log.d(TAG, "handleFirebaseDynamicLink: " + deepLink);
                                } else {
                                    Log.d(TAG, "handleFirebaseDynamicLink: pendingDynamicLinkData null");
                                }


                                // Handle the deep link. For example, open the linked
                                // content, or apply promotional credit to the user's
                                // account.
                                // ...

                                // ...
                            }
                        })
                        .addOnFailureListener(this, new OnFailureListener() {
                            @Override
                            public void onFailure(@NonNull Exception e) {
                                Log.w(TAG, "getDynamicLink:onFailure", e);
                            }
                        });

            }
        }
    }
  1. Using ADB Shell command on my physical mobile device I test the dynamic link using the following -
adb shell am start -a android.intent.action.VIEW -c android.intent.category.BROWSABLE -d "https://example.com/product/?apn=com.myapp.myapp&link=https://example.com/product/?productSKU=SKU-0004"
  1. This in turn launches the intended Activity. However, pendingDynamicLinkData returns null.
2022-01-03 17:29:15.141 12914-12914/com.myapp.myapp D/Product_Details: handleFirebaseDynamicLink: pendingDynamicLinkData null
  1. I have tried -

a. Using Different Dynamic Link prefixes like - /product (without the "/" at the end like /product/ ), without the query parameter /?productSKU=" + ProductSKU in the end of .setLink and .setDomainUriPrefix while building dynamic link,

b. By only converting created dynamic link using dynamicLink.getUri().toString() instead of the URLDecoder.decode(dynamicLink.getUri().toString(), "UTF-8") method.

  1. None of the above worked. Any help will be highly appreciated.

After some more research and little bit of debugging it looks like multiple parameters in URL are being removed by intent in Android. However, only the first parameter is preserved.

Hence in my case only apn parameter is saved while the link parameter is removed by intent. Hence, pendingDynamicLinkData returns null.

Removing multiple parameters by intent in android is explained by the following thread:

Android Deep Linking with multiple query parameters

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM