Skip to content

Commit

Permalink
feat: Handle Branch Deeplinks from Braze Push Notification
Browse files Browse the repository at this point in the history
Fixes: LEARNER-10054
  • Loading branch information
HamzaIsrar12 committed Jun 25, 2024
1 parent 948277a commit 2acaf9c
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 21 deletions.
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.BrazeBranchDeeplinkHandler
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(BrazeBranchDeeplinkHandler())
}
}
}
}
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

class BrazeBranchDeeplinkHandler : 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)
}
}
}

0 comments on commit 2acaf9c

Please sign in to comment.