📅  最后修改于: 2021-01-02 04:03:13             🧑  作者: Mango
在本节中,我们将讨论如何接收动态链接。因此,要接收我们之前创建的Firebase动态链接,请在我们的应用程序中包含动态链接SDK。当我们的应用加载时,我们还需要调用FirebaseDynamicLinks.getDynamicLink()方法来获取在动态链接中传递的数据。我们必须向该活动添加一个新的Intent过滤器,以处理应用程序的深层链接。意图过滤器应捕获我们域的深层链接,因为如果安装了我们的应用程序,动态链接将重定向到我们的域。从Play商店安装/更新动态链接数据并一键单击继续按钮后,我们的应用才能接收动态链接数据。
当用户打开带有指向我们指定的方案和主机的深层链接的动态链接时,我们的应用程序将使用此意图过滤器来开始活动以处理该链接。
为了处理深层链接,我们必须在该链接可能启动的每个活动中调用getDynamicLink()方法,即使该链接可能是使用getIntent()。getData()从意图中获得的。当我们调用getDynamicLink()时,它将检索链接并清除该数据,因此我们的应用仅接收到一次。通常,我们可以在主活动以及由意图过滤器启动的与链接匹配的任何活动中调用getDynamicLink()。
FirebaseDynamicLinks.getInstance()
.getDynamicLink(intent)
.addOnSuccessListener(this) { pendingDynamicLinkData ->
// Getting deep link from result (may be null if no link is found)
var deepLink: Uri? = null
if (pendingDynamicLinkData != null) {
deepLink = pendingDynamicLinkData.link
}
// Handle the deep link. For example, opening the linked
// content, or applying promotional credit to the user's
// account.
// ...
}
.addOnFailureListener(this) { e -> Log.w(TAG, "getDynamicLink:onFailure", e) }
package com.example.dynamiclink
import android.content.Intent
import android.net.Uri
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.Toast
import com.google.firebase.dynamiclinks.DynamicLink
import com.google.firebase.dynamiclinks.FirebaseDynamicLinks
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// Creating a deep link and display it in the UI
val newDeepLink = buildDeepLink(Uri.parse(DEEP_LINK_URL))
linkViewSend.text = newDeepLink.toString()
// Share button click listener
buttonShare.setOnClickListener { shareDeepLink(newDeepLink.toString()) }
FirebaseDynamicLinks.getInstance()
.getDynamicLink(intent)
.addOnSuccessListener(this) { pendingDynamicLinkData ->
// Getting deep link from result (may be null if no link is found)
var deepLink: Uri? = null
if (pendingDynamicLinkData != null) {
deepLink = pendingDynamicLinkData.link
}
// Handle the deep link. For example, opening the linked
// content, or applying promotional credit to the user's
// account.
// ...
// Display deep link in the UI
if (deepLink != null) {
Toast.makeText(getApplicationContext(),"Found Deep Link!", Toast.LENGTH_LONG).show()
linkViewReceive.text = deepLink.toString()
} else {
Log.d(TAG, "getDynamicLink: no link found")
}
}
.addOnFailureListener(this) { e -> Log.w(TAG, "getDynamicLink:onFailure", e) }
}
fun buildDeepLink(deepLink: Uri): Uri {
val dynamicLink = FirebaseDynamicLinks.getInstance().createDynamicLink()
.setLink(Uri.parse(deepLink.toString()))
.setDomainUriPrefix("https://rastogi.page.link")
// Open links with this app on Android
.setAndroidParameters(DynamicLink.AndroidParameters.Builder().build())
// Open links with com.example.ios on iOS
.setIosParameters(DynamicLink.IosParameters.Builder("com.example.ios").build())
.buildDynamicLink()
val dynamicLinkUri = dynamicLink.uri
return dynamicLinkUri;
}
private fun shareDeepLink(deepLink: String) {
val intent = Intent(Intent.ACTION_SEND)
intent.type = "text/plain"
intent.putExtra(Intent.EXTRA_SUBJECT, "Firebase Deep Link")
intent.putExtra(Intent.EXTRA_TEXT, deepLink)
startActivity(intent)
}
companion object {
private const val TAG = "MainActivity"
private const val DEEP_LINK_URL = "https://kotlin.example.com/deeplinks"
}
}
输出量