Android開發 Firebase動態連接打開APP

前言:json

最近項目須要實現Firebase動態連接打開APP的功能。動態連接打開APP,就是用戶在Android應用打開一個動態連接,則能夠直接轉到咱們原生應用中的連接內容,若是用戶還沒有安裝咱們的應用程序,系統會將其轉到 Play 商店或 App Store來安裝咱們的應用程序;安裝後,該應用程序就會啓動並能夠訪問該連接。網絡

思路:app

1.設置 Firebase 和 Dynamic Links SDK;
2.建立動態連接;
3.接收動態連接。ide

實現的代碼:gradle

1.Google官網註冊應用ui

   首先去網址:https://console.firebase.google.com/ 去註冊本身的應用,並下載google-services.json的文件,把它放到本身項目的app/目錄。this

2.添加依賴google

 Module的build.gradle.net

 dependencies {
    //Add Dynamic Links SDK,跟其餘Firebase的版本一致
    implementation 'com.google.firebase:firebase-invites:11.0.4'        
  }

3.在Firebase控制檯建立動態連接

3.1 設置短連接地址

3.2 設置動態連接

3.3 在Android應用打開深層連接

3.4 點擊建立,完成在Firebase控制檯建立動態連接

4.配置AndroidMenifest.xml文件

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="項目包名">

    <!--鏈接網絡權限-->
    <uses-permission android:name="android.permission.INTERNET" />
   
    <application
        android:name="Application包名"
        android:allowBackup="true"
        android:icon="@drawable/app_icon"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
         <!--首先須要配置打開某個APK的一個意圖事件,通常都是在Lanucher的Activity下配置另外個filter-->
        <activity
            android:name="Lanucher的Activity"
            android:launchMode="standard"
            android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            
            <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="動態連接域名" android:scheme="http"/>
                <data android:host="動態連接域名" android:scheme="https"/>
            </intent-filter>
        </activity>
       
    </application>

</manifest>

5.在Lanucher的Activity接收動態連接

public class LanucherActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lanucher);
        getDeepLink();//獲取動態連接
    }

    private void getDeepLink() {
        try {
            Intent intent = getIntent();
            String data = intent.getDataString();
            //判空,並處理動態連接
            if (data != null && data.equals("短連接")) {
                startActivity(new Intent(this, 要跳轉的頁面));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

6.總結

Firebase動態連接打開APP的簡單功能已經實現啦,後面還會把複雜的功能加進來的,歡迎你們圍觀喲!

轉載於:https://my.oschina.net/wupeilin/blog/1845879