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
onSuccess
回调再次触发,他永远无法前往主要活动。答案 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() 由与链接匹配的意图过滤器启动的活动。