Firebase动态链接,使用一次后清除它

时间:2018-01-06 14:59:21

标签: android firebase firebase-dynamic-links

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

说明

  

调用getDynamicLink()检索链接并清除该数据   它仅由您的应用处理一次。

     

您通常在主要活动中调用getDynamicLink()以及任何活动   由与链接匹配的意图过滤器启动的活动。

我从doc中复制了以下代码。

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();
                }


                // 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);
            }
        });

如果我将上述代码放在MainActivity:onCreate

  • 当应用未在后台运行时,深层链接正常
  • 当应用在后台运行时,无法识别深层链接(onSuccess回调未被调用)

如果我将上述代码放在MainActivity:onStart

  • 当app在后台运行时,深层链接正常
  • 如果用户点击深层链接,主要活动会获得它并打开approapriate活动,(工作正常)但当他尝试返回主要活动时,onSuccess回调再次触发,他永远无法前往主要活动。

3 个答案:

答案 0 :(得分:0)

MainActivity:onCreate方法内复制用MainActivity:onNewIntent方法编写的代码(整个Firebase动态链接相关代码),无论应用程序是否在后台运行,此方法均有效。

如果应用程序不在后台运行,则不会调用MainActivity:onNewIntent方法,因此不会发生重复的Firebase调用。

您的 MainActivity 应该看起来像这样

@Override
protected void onCreate(Bundle savedInstanceState) {
    //...

    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();
                    }


                    // 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<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();
                    }


                    // 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);
                }
            });
}

答案 1 :(得分:0)

我通过获取全局URL并在下次将其与新URL进行比较来保持肮脏,这并不完美,但这是我想出的最好的方法。

 var previousDeepLink:String?=null //take it as global and static


    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();
            }

                 if(previousDeepLink==deepLink?.toString()){
                        return@OnSuccessListener
                    }
                    previousDeepLink=deepLink?.toString()



            // 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);
        }
    });

                

答案 2 :(得分:-1)

答案在documents中:

您必须在每个活动中都必须调用getDynamicLink() 由链接启动,即使该链接可能可以从 使用getIntent()。getData()实现意图。调用getDynamicLink()检索 链接并清除该数据,因此您的数据只会被处理一次 应用程序。

通常,您可以在主活动以及任何其他活动中调用getDynamicLink() 由与链接匹配的意图过滤器启动的活动。