Firebase dynamic link, clear it after using once

前端 未结 3 702
孤城傲影
孤城傲影 2021-01-17 15:31

https://firebase.google.com/docs/dynamic-links/android/receive

states that

Calling getDynamicLink() retrieves the link and clears that data

3条回答
  •  广开言路
    2021-01-17 16:17

    Duplicate the code written in MainActivity:onCreate method (entire Firebase Dynamic Links related code) inside MainActivity:onNewIntent method, this works irrespective of the app is running in the background or not.

    Also MainActivity:onNewIntent method is not invoked if the app is not present in background hence no duplicate Firebase call happens.

    Your MainActivity should look like this

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        //...
    
        FirebaseDynamicLinks.getInstance()
                .getDynamicLink(getIntent())
                .addOnSuccessListener(this, new OnSuccessListener() {
                    @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();
                        }
    
    
                        // 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);
                    }
                });
    }
    
    @Override
    protected void onNewIntent(Intent intent) {
        //...
    
        FirebaseDynamicLinks.getInstance()
                .getDynamicLink(intent)
                .addOnSuccessListener(this, new OnSuccessListener() {
                    @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();
                        }
    
    
                        // 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);
                    }
                });
    }
    

提交回复
热议问题