Reputation: 33
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
implementation platform('com.google.firebase:firebase-bom:29.0.3')
implementation 'com.google.firebase:firebase-dynamic-links'
implementation 'com.google.firebase:firebase-analytics'
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();
}
}
https://example.com/product/?apn=com.myapp.myapp&link=https://example.com/product/?productSKU=SKU-0004
<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>
//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);
}
});
}
}
}
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"
2022-01-03 17:29:15.141 12914-12914/com.myapp.myapp D/Product_Details: handleFirebaseDynamicLink: pendingDynamicLinkData null
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.
Upvotes: 1
Views: 1026
Reputation: 33
After some more research and little bit of debugging it looks like multiple parameters in URL are being removed by intent in Android during testing through ADB. 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.
The generated dynamic link if sent through email works fine.
Removing multiple parameters by intent in android is explained by the following thread:
Upvotes: 1