本文概述
- AndroidManifest.xml
- 处理深层链接
- activity_main.xml
- MainActivity.kt
当用户打开带有指向我们指定的方案和主机的深层链接的动态链接时, 我们的应用程序将使用此意图过滤器来开始活动以处理该链接。
AndroidManifest.xml
<
intent-filter>
<
action android:name="android.intent.action.VIEW" />
<
category android:name="android.intent.category.DEFAULT"/>
<
category android:name="android.intent.category.BROWSABLE"/>
<
dataandroid:host="kotlin.example.com"android:scheme="https"/>
<
/intent-filter>
【在Firebase中接收动态链接】
处理深层链接为了处理深层链接, 我们必须在该链接可能启动的每个活动中调用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? = nullif (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) }
activity_main.xml
MainActivity.kt
package com.example.dynamiclinkimport android.content.Intentimport android.net.Uriimport androidx.appcompat.app.AppCompatActivityimport android.os.Bundleimport android.util.Logimport android.widget.Toastimport com.google.firebase.dynamiclinks.DynamicLinkimport com.google.firebase.dynamiclinks.FirebaseDynamicLinksimport 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 UIval newDeepLink = buildDeepLink(Uri.parse(DEEP_LINK_URL))linkViewSend.text = newDeepLink.toString()// Share button click listenerbuttonShare.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? = nullif (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 UIif (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.urireturn 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"}}
输出量
推荐阅读
- 带Firebase的AdMob
- Firebase创建动态链接
- Firebase中的动态链接
- Firebase应用内消息传递设置和配置
- Firebase应用内消息传递
- 使用Firebase Cloud Messaging发送通知消息
- Firebase消息类型
- Firebase云消息传递
- Firebase Crashlytics中的崩溃报告