how to set dynamic link to a particular activity in android app

5k views Asked by At

I am implementing a shopping app.In that when a product is shared it should send a dynamic link so that when a person clicks it , opens in app only.Finally i achieved this feature.But when i am opening app from a shared dynamic link i am getting home page rather than product page.I need help regarding getting specific product page rather opening home page of shopping. Here i am including code. AndroidManifest.xml

<activity android:name=".Postdetails">
 <meta-data android:name="android.support.PARENT_ACTIVITY" android:value="MainActivity" />
        <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="postdetailsnew.com" android:scheme="https" 
             android:pathPattern=".*"/>
        </intent-filter>
</activity>

PostDetails.java Here PostDetails page is the single product design page or activity .In this i wrote following code.

sharebutton.setOnClickListener(new View.OnClickListener() {
 @Override
 public void onClick(View v) {
 s="https://app_code.app.goo.gl/?link="+ProductLinkInBrowser+"&apn=com.example.maneesh.shop";
   Intent sendIntent = new Intent();
   sendIntent.setAction(Intent.ACTION_SEND);
   sendIntent.putExtra(Intent.EXTRA_TEXT, s);
   sendIntent.setType("text/plain");
   startActivity(sendIntent);
   }
}
FirebaseDynamicLinks.getInstance().getDynamicLink(getIntent())
    .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();
   FirebaseAppInvite invite=FirebaseAppInvite.getInvitation(pendingDynamicLinkData);
                        if(invite!=null){
                            String invitationId=invite.getInvitationId();
                        }
                    }
                }
            })
            .addOnFailureListener(this, new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {
                    Log.w("Heyy", "getDynamicLink:onFailure", e);
                }
            });

So here finally app main page is opened with dynamic link but not the specific product page which is shared.Please guide me to achieve this.

1

There are 1 answers

0
Ian Barber On BEST ANSWER

You need to parse the URL in the dynamic link, and fire an intent to send the user to the right part of your app. Generally you'll have the Dynamic Links listener set up in your main activity, and will route from there to any specific needs.

The deepLink variable in your code will be the URL your passed in the dynamic link, like http://yourshop.com/product/12345. You could call deepLink.getPath() which would return you product/12345, and then fire and intent for that, e.g:

String path = deepLink.getPath();
String[] parts = path.split("/")
if (parts[0] == "product") {
  Intent intent = new Intent(this, PostDetails.class);
  intent.putExtra("productid", parts[1]);
  startActivity(intent);
}