Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Handle Branch Deeplinks from Braze Push Notification #353

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 18 additions & 21 deletions app/src/main/java/org/openedx/app/AppActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,20 @@ class AppActivity : AppCompatActivity(), InsetHolder, WindowSizeHolder {

private var _windowSize = WindowSize(WindowType.Compact, WindowType.Compact)

private val branchCallback =
BranchUniversalReferralInitListener { branchUniversalObject, _, error ->
if (branchUniversalObject?.contentMetadata?.customMetadata != null) {
branchLogger.i { "Branch init complete." }
branchLogger.i { branchUniversalObject.contentMetadata.customMetadata.toString() }
viewModel.makeExternalRoute(
fm = supportFragmentManager,
deepLink = DeepLink(branchUniversalObject.contentMetadata.customMetadata)
)
} else if (error != null) {
branchLogger.e { "Branch init failed. Caused by -" + error.message }
}
}

override fun onSaveInstanceState(outState: Bundle) {
outState.putInt(TOP_INSET, topInset)
outState.putInt(BOTTOM_INSET, bottomInset)
Expand Down Expand Up @@ -152,21 +166,8 @@ class AppActivity : AppCompatActivity(), InsetHolder, WindowSizeHolder {
super.onStart()

if (viewModel.isBranchEnabled) {
val callback = BranchUniversalReferralInitListener { branchUniversalObject, _, error ->
if (branchUniversalObject?.contentMetadata?.customMetadata != null) {
branchLogger.i { "Branch init complete." }
branchLogger.i { branchUniversalObject.contentMetadata.customMetadata.toString() }
viewModel.makeExternalRoute(
fm = supportFragmentManager,
deepLink = DeepLink(branchUniversalObject.contentMetadata.customMetadata)
)
} else if (error != null) {
branchLogger.e { "Branch init failed. Caused by -" + error.message }
}
}

Branch.sessionBuilder(this)
.withCallback(callback)
.withCallback(branchCallback)
.withData(this.intent.data)
.init()
}
Expand All @@ -183,13 +184,9 @@ class AppActivity : AppCompatActivity(), InsetHolder, WindowSizeHolder {

if (viewModel.isBranchEnabled) {
if (intent?.getBooleanExtra(BRANCH_FORCE_NEW_SESSION, false) == true) {
Branch.sessionBuilder(this).withCallback { referringParams, error ->
if (error != null) {
branchLogger.e { error.message }
} else if (referringParams != null) {
branchLogger.i { referringParams.toString() }
}
}.reInit()
Branch.sessionBuilder(this)
.withCallback(branchCallback)
.reInit()
}
}
}
Expand Down
7 changes: 7 additions & 0 deletions app/src/main/java/org/openedx/app/OpenEdXApp.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ package org.openedx.app
import android.app.Application
import com.braze.Braze
import com.braze.configuration.BrazeConfig
import com.braze.ui.BrazeDeeplinkHandler
import com.google.firebase.FirebaseApp
import io.branch.referral.Branch
import org.koin.android.ext.android.inject
import org.koin.android.ext.koin.androidContext
import org.koin.core.context.startKoin
import org.openedx.app.deeplink.BranchBrazeDeeplinkHandler
import org.openedx.app.di.appModule
import org.openedx.app.di.networkingModule
import org.openedx.app.di.screenModule
Expand Down Expand Up @@ -36,6 +38,7 @@ class OpenEdXApp : Application() {
Branch.enableTestMode()
Branch.enableLogging()
}
Branch.expectDelayedSessionInitialization(true)
Branch.getAutoInstance(this)
}

Expand All @@ -50,6 +53,10 @@ class OpenEdXApp : Application() {
.setIsFirebaseMessagingServiceOnNewTokenRegistrationEnabled(true)
.build()
Braze.configure(this, brazeConfig)

if (config.getBranchConfig().enabled) {
BrazeDeeplinkHandler.setBrazeDeeplinkHandler(BranchBrazeDeeplinkHandler())
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package org.openedx.app.deeplink

import android.content.Context
import android.content.Intent
import android.net.Uri
import com.braze.ui.BrazeDeeplinkHandler
import com.braze.ui.actions.UriAction
import org.openedx.app.AppActivity

internal class BranchBrazeDeeplinkHandler : BrazeDeeplinkHandler() {
override fun gotoUri(context: Context, uriAction: UriAction) {
val deeplink = uriAction.uri.toString()

if (deeplink.contains("app.link")) {
val intent = Intent(context, AppActivity::class.java).apply {
action = Intent.ACTION_VIEW
data = Uri.parse(deeplink)
flags = Intent.FLAG_ACTIVITY_NEW_TASK
putExtra("branch_force_new_session", true)
}
context.startActivity(intent)
} else {
super.gotoUri(context, uriAction)
}
}
}
Loading