Firebase

Android Firebase Dynamic Links

1.Firebase與專案連結

2.點擊Dynamic Links將library與google-services.json導入進專案

3.Firebase控制台創建短網址

4.Firebase控制台新增動態連結

深層連結網址 連接到APP時使用這個連結
動態連結名稱 使用GA可以分析連結點擊數

5.設定APP裡接收的Scheme

<activity android:name=".MainActivity">
    <intent-filter>
        <action android:name="android.intent.action.VIEW"/>
        <category android:name="android.intent.category.DEFAULT"/>
        <category android:name="android.intent.category.BROWSABLE"/>
        <data android:host="myFlyApp.com"
            android:scheme="https"/>
    </intent-filter>
</activity>

6.程式碼新增動態連結

FirebaseDynamicLinks.getInstance().createDynamicLink()
    .setLink(Uri.parse("https://myFlyApp.com?account=toy234&password=qwe123"))
    .setDomainUriPrefix("https://flyapp111.page.link")
    .setAndroidParameters(DynamicLink.AndroidParameters.Builder().build())
    .buildShortDynamicLink(ShortDynamicLink.Suffix.SHORT)
    .addOnCompleteListener{
        if (it.isSuccessful) {
           val shortLink = it.result?.shortLink
           Log.e("Dynamic Links", shortLink.toString())
        }
    }

6.取得Dynamic Links的網址並取出傳來的參數

 FirebaseDynamicLinks.getInstance()
      .getDynamicLink(intent)
      .addOnSuccessListener {
         val deepLink: Uri?
         if (it != null) {
            deepLink = it.link
            //傳來網址
            Log.e("Dynamic Links", deepLink.toString())
            //帳號
            Log.e("Dynamic Links account", deepLink.toString().split("?")[1]
                  .split("&")[0]
                  .split("=")[1])
            //密碼
            Log.e("Dynamic Links password", deepLink.toString().split("?")[1]
                  .split("&")[1]
                   .split("=")[1])
            //傳來時間
            Log.e("Dynamic Links", SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Date(it.clickTimestamp)).toString())
          }
       }
       .addOnFailureListener {
            Log.e("Dynamic Links", "讀取失敗")
       }

8.效果展示

發表迴響