Search code examples
androidfirebasefirebase-dynamic-linksdeeplink

How to handle both Firebase DynamicLink and plain DeepLink at the same time?


I'm confused about google docs, in handling both plain deepLinks and firebase dynamic links at the same time.

To handle plain deepLinks, I had this:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Intent intent = getIntent();
    String action = intent.getAction();
    Uri data = intent.getData();

    parseDeepLink(data);
}

To add support for firebase dynamic links ( and firebase app invites), the docs suggest to use this:

FirebaseDynamicLinks.getInstance()
.getDynamicLink(getIntent())
.addOnSuccessListener(this, new OnSuccessListener<PendingDynamicLinkData>() {
    @Override
    public void onSuccess(PendingDynamicLinkData pendingDynamicLinkData) {
        if (pendingDynamicLinkData != null) {
            Uri deepLink = pendingDynamicLinkData.getLink();

            // Extract invite
            FirebaseAppInvite invite = FirebaseAppInvite.getInvitation(pendingDynamicLinkData);
            if (invite != null) {
                String invitationId = invite.getInvitationId();

                // handle invite
            }

            parseDeepLink(deepLink);
        }
    }
})
.addOnFailureListener(this, new OnFailureListener() {
    @Override
    public void onFailure(@NonNull Exception e) {
        finish();
    }
});

However, with the above updated code, I can't receive plain deepLinks as pendingDynamicLinkData is always null with plain deepLinks. Should I be using parseDeepLink(getIntent().getData()) in onFailure() method?

What is the correct way to handle both plain DeepLinks and Firebase Dynamic Links?


Solution

  • For non-dynamic links you should just use

    Uri data = intent.getData();

    As you were before. You can check that in your success callback, e.g.

    if (pendingDynamicLinkData != null) {
            Uri deepLink = pendingDynamicLinkData.getLink();
    
            // Extract invite
            FirebaseAppInvite invite = FirebaseAppInvite.getInvitation(pendingDynamicLinkData);
            if (invite != null) {
                String invitationId = invite.getInvitationId();
    
                // handle invite
            }
    
            parseDeepLink(deepLink);
        } else {
            Uri data = intent.getData();
            parseDeepLink(data);
        }