Firebase动态链接,使用一次后将其清除 [英] Firebase dynamic link, clear it after using once
问题描述
https://firebase.google.com/docs/dynamic-links/android/receive
声明
调用getDynamicLink()检索链接并清除该数据,以便 它只会由您的应用处理一次.
Calling getDynamicLink() retrieves the link and clears that data so it is only processed once by your app.
您通常会在主活动以及任何其他活动中调用getDynamicLink() 由与链接匹配的意图过滤器启动的活动.
You normally call getDynamicLink() in the main activity as well as any activities launched by intent filters that match the link.
我从文档中复制了以下代码.
I copied the following code from the 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
If I put the above code on MainActivity:onCreate
- 当应用未在后台运行时,深层链接可以正常工作
- 当应用在后台运行时,无法识别深层链接(不会调用
onSuccess
回调)
- when app is not running in background, deep link works fine
- when app is running in background, deep link is not recognized (the
onSuccess
callback doesn't get called)
如果我将上面的代码放在MainActivity:onStart
If I put the above code on MainActivity:onStart
- 无论应用是否在后台运行,深层链接都可以正常工作
- 如果用户单击深层链接,则主要活动会获取该链接并打开适当的活动(正常),但是当他尝试返回主要活动时,
onSuccess
回调会再次触发,并且他将永远无法进入主要活动活动.
- when app is running in background or not, deep link works fine
- If user clicks deep link, main activity gets it and opens approapriate activity, (works fine) but when he tries to go back to the main activity,
onSuccess
callback fires again and he never be able to go to the main activity.
推荐答案
在MainActivity:onNewIntent
方法内部复制用MainActivity:onCreate
方法编写的代码(与Firebase Dynamic Links相关的整个代码),无论该应用程序是否在是否有背景.
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.
MainActivity:onNewIntent
方法,因此不会发生重复的Firebase调用.
Also MainActivity:onNewIntent
method is not invoked if the app is not present in background hence no duplicate Firebase call happens.
您的 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);
}
});
}
这篇关于Firebase动态链接,使用一次后将其清除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!