Here are the full steps what I have done:
Have updated firebase.json with the following:
"appAssociation": "AUTO", "rewrites": [ { "source": "/product/**", "dynamicLinks": true } ]
My website domain example.com is hosted in Firebase. So, uploaded firebase.json using firebase deploy --only hosting
Added the following Dynamic URL prefix in Firebase Dynamic Links
https://example.com/product
Screenshot of Firebase Dynamic Link
- 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'
- 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();
}
}
- Dynamic Link generated from above method is:
https://example.com/product/?apn=com.myapp.myapp&link=https://example.com/product/?productSKU=SKU-0004
- 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>
- 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);
}
});
}
}
}
- 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"
- 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
- 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.
- None of the above worked. Any help will be highly appreciated.