본문 바로가기

안드로이드

[안드로이드] Firebase Dynamic Link로 앱 열기

Firebase Dynamic Link란?

딥링크 문제를 보완한 링크

(※ Firebase 동적 링크는 앱 설치 여부에 관계없이 여러 플랫폼에서 원하는 대로 작동하는 링크입니다.)

 

Firebase가 제공하는 Dynamic Link 사용 방법을 알아보기 앞서 딥링크에 대해서 알아보자

딥링크?

일반적으로 웹에서 딥링크란 특정 페이지에 도달할 수 있는 링크를 말한다. 

 

모바일 딥링크도 위와 같이 앱의 특정 페이지에 도달할 수 있는 링크를 뜻한다.

어떻게 구현할까?

구현 방법은 크게 2가지로 나눌 수 있다.

 

URI 스키마를 사용하는 방식과 웹페이지 주소를 사용하는 방식이다.

URI 스키마

URI 스키마는 Scheme와 Path로 이루어져있다.

 

링크는 Scheme://Path와 같으며, 아래 그림과 같이 각 페이지에 대해 스키마와 경로를 지정할 수 있다.

 

ViewActivity의 딥링크는 deeplink://view이다

URI 스키마는 가장 단순한 방법이지만, 다른 앱과 스키마가 같을 경우, 아래 그림과 같이 실행할 앱을 선택해야하는 문제가 있다.

웹페이지 주소

고유한 스키마를 가지는 딥링크와는 달리, 웹페이지 주소는 기존 웹에서 사용하던 http/https를 사용하여 앱 페이지로 이동한다.

 

안드로이드에선 이를 앱 링크라 하고 IOS에선 유니버셜 링크라 한다.

 

안드로이드 앱 링크 설정

하지만 이 방법도 문제가 있다. 앱 링크는 구글에서 만든 앱에서만 동작하고 유니버셜 링크는 애플에서 만든 앱에서만 동작한다.

 

Firebase가 제공하는 Dynamic Link를 사용하면 이러한 문제를 해결할 수 있다.

Dynamic Link 사용하기

구현 경로는 다음과 같다

 

1. Firebase 및 동적 링크 SDK 생성

2. 동적링크 만들기

3. 앱에서 동적링크 처리

4. 애널리틱스 데이터 보기

1. 도메인 등록

우선 딥링크에 사용될 도메인을 등록해야한다.

 

사용할 도메인을 입력한다.

원하는 프리픽스를 입력한다

원하는 prefx를 작성한다

도메인 소유 여부를 인증하면 다음 화면으로 넘어간다

2. 앱 등록

앱 추가를 클릭한다

앱 정보를 작성한 다음, google-services.json을 다운받고, 프로젝트의 app 디렉토리 안에 복사한다

프로젝트 수준의 build.gradle의 dependencies에 classpath com.google.gms:google-services:4.3.4를 작성한다.

// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
    ext.kotlin_version = "1.3.72"
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath "com.android.tools.build:gradle:4.0.0"
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath 'com.google.gms:google-services:4.3.4'
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

 

앱 수준의 build.gradle에 apply plugin: 'com.google.gms.google-services를 작성하고

 

dependencies 블록엔

implementation platform('com.google.firebase:firebase-bom:25.12.0')
implementation 'com.google.firebase:firebase-dynamic-links-ktx'
implementation 'com.google.firebase:firebase-analytics-ktx'

를 작성한다.

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'

android {
    compileSdkVersion 30
    buildToolsVersion "30.0.0"

    defaultConfig {
        applicationId "com.example.deeplink"
        minSdkVersion 16
        targetSdkVersion 30
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: "libs", include: ["*.jar"])
    implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
    implementation 'androidx.core:core-ktx:1.3.0'
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'

    // Import the BoM for the Firebase platform
    implementation platform('com.google.firebase:firebase-bom:25.12.0')

    // Declare the dependencies for the Dynamic Links and Analytics libraries
    // When using the BoM, you don't specify versions in Firebase library dependencies
    implementation 'com.google.firebase:firebase-dynamic-links-ktx'
    implementation 'com.google.firebase:firebase-analytics-ktx'
}

apply plugin: 'com.google.gms.google-services'

3. 앱에서 딥링크에 대한 인텐트 필터 추가

액티비티를 생성하고, AndroidManifest.xml에서 아래와 같이 작성한다.

<activity android:name=".ViewActivity">
	<intent-filter android:autoVerify="true">
		<action android:name="android.intent.action.VIEW" />

		<category android:name="android.intent.category.DEFAULT" />
		<category android:name="android.intent.category.BROWSABLE" />

		<data
			android:host="deeplink.puroong.me"
			android:scheme="https"
			android:path="/link" />
	</intent-filter>
</activity>

액티비티의 onCreate에 다음과 같은 코드를 작성한다.

package com.example.deeplink

// import packages ...

class ViewActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
		// do other works ...
        Firebase.dynamicLinks
            .getDynamicLink(intent)
            .addOnSuccessListener(this) { pendingDynamicLinkData ->  
                var deeplink: Uri? = null
                if(pendingDynamicLinkData != null) {
                    deeplink = pendingDynamicLinkData.link
                }

                if(deeplink != null) {
                    val tvDeepLinkContent = findViewById<TextView>(R.id.tv_deeplink_content)
                    tvDeepLinkContent.text = deeplink.toString()
                }
                else {
                    Log.d(TAG, "getDynamicLink: no link found")
                }
            }
            .addOnFailureListener(this) { e -> Log.w(TAG, "getDynamicLink:onFailure", e) }
    }
}

4. 동적 링크 생성하기

딥링크 주소는 AndroidManifest.xml에서 작성한 것과 동일하게 작성한다.

앞서 등록한 앱을 선택하고, 앱이 설치되지 않은 경우 플레이스토어로 리다이렉트 하도록 설정한다.

만들기를 클릭하면 아래와 같은 화면을 확인할 수 있다

이제 모바일에 앱을 설치하면, https://deeplink.puroong.me/link/Eit5를 클릭했을때, 앱의 ViewActivity로 이동하는 것을 확인할 수 있다.

(앱이 설치되지 않았을 경우, 구글 플레이 스토어로 리다이렉트 되지만 앱을 업로드 하지 않아서 아무 화면도 뜨지 않는다.)

 

여기에 소스코드를 올렸으니 필요하시면 참고하시기 바랍니다.

 

[출처]

https://en.wikipedia.org/wiki/Deep_linking

 

Deep linking - Wikipedia

In the context of the World Wide Web, deep linking is the use of a hyperlink that links to a specific, generally searchable or indexed, piece of web content on a website (e.g. "http://example.com/path/page"), rather than the website's home page (e.g., "htt

en.wikipedia.org

https://help.adbrix.io/hc/ko/articles/360039757433-%EB%94%A5%EB%A7%81%ED%81%AC-Deeplink-URI%EC%8A%A4%ED%82%B4-%EC%9C%A0%EB%8B%88%EB%B2%84%EC%85%9C-%EB%A7%81%ED%81%AC-%EC%95%B1%EB%A7%81%ED%81%AC-%EA%B5%AC%EB%B6%84%EA%B3%BC-%EC%9D%B4%ED%95%B4

https://firebase.google.com/docs/dynamic-links?hl=ko

 

Firebase 동적 링크

Firebase 동적 링크는 앱 설치 여부에 관계없이 여러 플랫폼에서 원하는 대로 작동하는 링크입니다.

firebase.google.com

https://firebase.google.com/docs/dynamic-links/android/receive?authuser=0

 

Android에서 Firebase 동적 링크 수신하기

생성한 Firebase 동적 링크를 수신하려면 앱에 동적 링크 SDK를 포함하고, 앱이 로드될 때 FirebaseDynamicLinks.getDynamicLink() 메서드를 호출하여 동적 링크로 전달된 데이터를 가져와야 합니다. Firebase 및

firebase.google.com