- Firebase Dynamic Link로 생성한 링크로 안드로이드 앱에 접근했을 때 모아서 처리할 수 있는 방법
SchemaActivity를 통해 dynamic link uri 에 맞는 Activity를 열 수 있다.- 참고 링크
Last active
July 24, 2020 07:39
-
-
Save og721/464dc97c0ee4877829e2988d3aa9509f to your computer and use it in GitHub Desktop.
Android Deep Link 사용
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class SchemaActivity: AppCompatActivity() { | |
| override fun onCreate(savedInstanceState: Bundle?) { | |
| super.onCreate(savedInstanceState) | |
| handleDeepLink() | |
| } | |
| private fun handleDeepLink() { | |
| FirebaseDynamicLinks.getInstance() | |
| .getDynamicLink(intent) | |
| .addOnSuccessListener { pendingDynamicLinkData -> | |
| val deepLinkIntent = pendingDynamicLinkData?.link?.let { | |
| DeepLinkInfo.invoke(it).getIntent(this, it) | |
| } ?: DeepLinkInfo.getMainIntent(this) | |
| if ( isTaskRoot ) { | |
| // Root Task 가 아닐 경우 Main Activity 를 Parent Stack 으로 추가 | |
| TaskStackBuilder.create(this).apply { | |
| if ( needAddMainForParent(deepLinkIntent) ) { | |
| addNextIntentWithParentStack(DeepLinkInfo.getMainIntent(this@SchemaActivity)) | |
| } | |
| addNextIntent(deepLinkIntent) | |
| }.startActivities() | |
| } else { | |
| startActivity(deepLinkIntent) | |
| } | |
| finish() | |
| } | |
| .addOnFailureListener { e -> | |
| Dlog.e("Error :>> ${e.message}") | |
| throw IllegalAccessError("Dynamic Link Access Error") | |
| } | |
| } | |
| private fun needAddMainForParent(intent: Intent): Boolean = | |
| when (intent.component?.className) { | |
| MainActivity::class.java.name -> false | |
| else -> true | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <application ...> | |
| <activity android:name=".MainActivity"> | |
| <intent-filter> | |
| <action android:name="android.intent.action.MAIN" /> | |
| <category android:name="android.intent.category.LAUNCHER" /> | |
| </intent-filter> | |
| </activity> | |
| <activity android:name=".TestActivity"/> | |
| <activity android:name=".SchemaActivity"> | |
| <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="@string/schema_host" | |
| android:scheme="@string/schema" /> | |
| </intent-filter> | |
| </activity> | |
| </application> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| enum class DeepLinkInfo(@StringRes val hostStringResId: Int) { | |
| MAIN(R.string.schema_main) { | |
| override fun getIntent(context: Context, deepLinkUri: Uri): Intent { | |
| return MainActivity.getIntent(context) | |
| } | |
| }, | |
| TEST(R.string.schema_test) { | |
| override fun getIntent(context: Context, deepLinkUri: Uri): Intent { | |
| return TestActivity.getIntent(context, deepLinkUri) | |
| } | |
| }; | |
| private val host: String = GlobalApplication.instance.getString(hostStringResId) | |
| abstract fun getIntent(context: Context, deepLinkUri: Uri): Intent | |
| companion object { | |
| fun getMainIntent(context: Context) = MainActivity.getIntent(context) | |
| fun invoke(uri: Uri): DeepLinkInfo { | |
| return values().find { it.host == uri.lastPathSegment } ?: run { | |
| MAIN | |
| } | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class TestActivity : AppCompatActivity() { | |
| private var id: String? = null | |
| override fun onCreate(savedInstanceState: Bundle?) { | |
| super.onCreate(savedInstanceState) | |
| setContentView(R.layout.activity_test) | |
| id = intent.getStringExtra(ID) | |
| } | |
| companion object { | |
| fun getIntent(context: Context, deepLinkUri: Uri): Intent { | |
| val id = deepLinkUri.getQueryParameter(ID) ?: run { | |
| Dlog.d("ID 없음") | |
| "" | |
| } | |
| return getIntent(context, questionId) | |
| } | |
| private fun getIntent(context: Context, id: String): Intent { | |
| return Intent(context, TestActivity::class.java).apply { | |
| addFlags( | |
| Intent.FLAG_ACTIVITY_CLEAR_TOP | |
| or Intent.FLAG_ACTIVITY_NEW_TASK // To use new bundle data | |
| ) | |
| putExtra(ID, id) | |
| } | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class MainActivity : AppCompatActivity() { | |
| override fun onCreate(savedInstanceState: Bundle?) { | |
| super.onCreate(savedInstanceState) | |
| setContentView(R.layout.activity_main) | |
| } | |
| companion object { | |
| fun getIntent(context: Context): Intent = | |
| Intent(context, MainActivity::class.java).apply { | |
| addFlags( | |
| Intent.FLAG_ACTIVITY_CLEAR_TOP | |
| or Intent.FLAG_ACTIVITY_SINGLE_TOP | |
| or Intent.FLAG_ACTIVITY_CLEAR_TASK | |
| ) | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment