android firebase dynamic link PendingDynamicLinkData is null direct through app

5.2k views Asked by At

Problem: Why is "Short dynamic links" created programatically wont open/launch the app directly?

I want to launch app directly when user clicks the dynamic url created dynamically by android app.

When clicking dynamic short link created dynamically by android app the following things happen,

1.Option show two options one is through chrome other is through app

2.if i choose chrome option, browser opens, shows a loading dialog box and launch app with PendingDynamicLinkData data

3.but if i choose app option, app lauches app but PendingDynamicLinkData is lost or null.

Any Help would be great. my manifest setting is below

  <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="myapp.page.link" android:scheme="http"/>
    <data android:host="myapp.page.link" android:scheme="https"/>
  </intent-filter>
3

There are 3 answers

4
Onix On BEST ANSWER

You should handle your PendingDynamicLinkData in activity to override onCreate and onNewIntent. Like this:

@Override
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Intent intent = getIntent();
        if (intent != null) {
            handleDeepLink(intent);
        }
}

@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    if (intent != null) {
        handleDeepLink(intent);
    }
}

private void handleDeepLink(Intent intent) {
        FirebaseDynamicLinks.getInstance().getDynamicLink(intent).addOnSuccessListener(pendingDynamicLinkData -> {
            if (pendingDynamicLinkData != null) {
                Uri deepLink = pendingDynamicLinkData.getLink();
                if (deepLink != null) {
                    // todo .....
                }
            }
        });
    }
0
Umer On

I don't know the correct solution but I have found a trick that works. First make sure that you have added the following intent filter to the activity that is supposed to handle the dynamic link:

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

Source

Then in your activity, use the following code to get the link:

String link=null;
if (getIntent().getData()!=null){
    link=getIntent().getData().toString();
}
0
MiguelSlv On

In the manifest, add the following intent filter to the activity that will handle the url:

override OnStart method of the target Activity:

public override fun onStart() {
    super.onStart()
    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();
            }
            // ...
        }
    })
    .addOnFailureListener(this, new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception e) {
            Log.w(TAG, "getDynamicLink:onFailure", e);
        }
    });

The complete guide here. For some reason, i couldn't make it work in the same activity that as action MAIN.