Skip to content

Commit f3d7cf6

Browse files
authored
fix(in_app_messaging): keep Android SDK artifacts off the app compile classpath (#18873)
* fix(in_app_messaging): keep Android SDK artifacts off the app compile classpath Exporting the versionless In-App Messaging libraries made Gradle look them up with an empty version. The display callback now lives on a private object so the app no longer needs those types to compile. * test: build in-app messaging in the smoke app The shared tests app is the Android build that covers the plugins together. It did not depend on firebase_in_app_messaging, so this empty-version failure never ran in CI. * fix(in_app_messaging): deliver custom display on the main thread The Firebase SDK calls displayMessage off the main thread. Recording the message and notifying Dart now happen together on the main thread, so engine detach cannot leave that callback stranded in the pending map.
1 parent bd8d3a3 commit f3d7cf6

3 files changed

Lines changed: 39 additions & 29 deletions

File tree

‎packages/firebase_in_app_messaging/firebase_in_app_messaging/android/build.gradle‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,8 @@ android {
7979
dependencies {
8080
api firebaseCoreProject
8181
implementation platform("com.google.firebase:firebase-bom:${getRootProjectExtOrCoreProperty("FirebaseSDKVersion", firebaseCoreProject)}")
82-
// `api` so app Java compilation can resolve FirebaseInAppMessagingDisplay,
83-
// which the plugin class implements.
84-
api 'com.google.firebase:firebase-inappmessaging'
85-
api 'com.google.firebase:firebase-inappmessaging-display'
82+
implementation 'com.google.firebase:firebase-inappmessaging'
83+
implementation 'com.google.firebase:firebase-inappmessaging-display'
8684
implementation 'androidx.annotation:annotation:1.7.0'
8785
}
8886
}

‎packages/firebase_in_app_messaging/firebase_in_app_messaging/android/src/main/kotlin/io/flutter/plugins/firebase/inappmessaging/FirebaseInAppMessagingPlugin.kt‎

Lines changed: 35 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,15 @@ import io.flutter.plugins.firebase.core.FlutterFirebasePluginRegistry
3131
import java.lang.ref.WeakReference
3232
import java.util.concurrent.ConcurrentHashMap
3333
import java.util.concurrent.Executor
34+
import kotlin.jvm.Volatile
3435

3536
/** FirebaseInAppMessagingPlugin */
3637
class FirebaseInAppMessagingPlugin :
37-
FlutterFirebasePlugin,
38-
FlutterPlugin,
39-
FirebaseInAppMessagingHostApi,
40-
FirebaseInAppMessagingDisplay {
38+
FlutterFirebasePlugin, FlutterPlugin, FirebaseInAppMessagingHostApi {
4139
private var binaryMessenger: BinaryMessenger? = null
4240
private var flutterApi: FirebaseInAppMessagingFlutterApi? = null
4341
private var application: Application? = null
44-
private var customDisplayEnabled = false
42+
@Volatile private var customDisplayEnabled = false
4543
private var lifecycleRegistered = false
4644
private var lastActivity: WeakReference<Activity>? = null
4745
private val pendingDisplays = ConcurrentHashMap<String, PendingDisplay>()
@@ -81,6 +79,36 @@ class FirebaseInAppMessagingPlugin :
8179

8280
private var eventListenersAdded = false
8381

82+
private val messageDisplay =
83+
object : FirebaseInAppMessagingDisplay {
84+
override fun displayMessage(
85+
inAppMessage: InAppMessage,
86+
callbacks: FirebaseInAppMessagingDisplayCallbacks,
87+
) {
88+
if (!customDisplayEnabled) {
89+
return
90+
}
91+
92+
val message = toDisplayMessage(inAppMessage)
93+
val actions = collectActions(inAppMessage)
94+
95+
mainHandler.post {
96+
val api = flutterApi
97+
if (api != null && customDisplayEnabled) {
98+
pendingDisplays[message.campaignMetadata.campaignId] =
99+
PendingDisplay(callbacks, actions)
100+
api.onMessageDisplay(message) {}
101+
} else {
102+
try {
103+
callbacks.messageDismissed(
104+
FirebaseInAppMessagingDisplayCallbacks.InAppMessagingDismissType.AUTO,
105+
)
106+
} catch (_: Exception) {}
107+
}
108+
}
109+
}
110+
}
111+
84112
private val lifecycleCallbacks =
85113
object : Application.ActivityLifecycleCallbacks {
86114
override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) {}
@@ -90,10 +118,7 @@ class FirebaseInAppMessagingPlugin :
90118
override fun onActivityResumed(activity: Activity) {
91119
lastActivity = WeakReference(activity)
92120
if (customDisplayEnabled) {
93-
FirebaseInAppMessaging.getInstance()
94-
.setMessageDisplayComponent(
95-
this@FirebaseInAppMessagingPlugin,
96-
)
121+
FirebaseInAppMessaging.getInstance().setMessageDisplayComponent(messageDisplay)
97122
}
98123
}
99124

@@ -130,21 +155,6 @@ class FirebaseInAppMessagingPlugin :
130155
FirebaseInAppMessagingHostApi.setUp(binding.binaryMessenger, null)
131156
}
132157

133-
override fun displayMessage(
134-
inAppMessage: InAppMessage,
135-
callbacks: FirebaseInAppMessagingDisplayCallbacks,
136-
) {
137-
if (!customDisplayEnabled) {
138-
return
139-
}
140-
141-
val message = toDisplayMessage(inAppMessage)
142-
pendingDisplays[message.campaignMetadata.campaignId] =
143-
PendingDisplay(callbacks, collectActions(inAppMessage))
144-
145-
mainHandler.post { flutterApi?.onMessageDisplay(message) {} }
146-
}
147-
148158
private fun campaignMetadata(inAppMessage: InAppMessage): FiamCampaignMetadata {
149159
val metadata = inAppMessage.campaignMetadata
150160
return FiamCampaignMetadata(
@@ -305,7 +315,7 @@ class FirebaseInAppMessagingPlugin :
305315
customDisplayEnabled = enabled
306316
val fiam = FirebaseInAppMessaging.getInstance()
307317
if (enabled) {
308-
fiam.setMessageDisplayComponent(this)
318+
fiam.setMessageDisplayComponent(messageDisplay)
309319
if (!lifecycleRegistered) {
310320
application?.registerActivityLifecycleCallbacks(lifecycleCallbacks)
311321
lifecycleRegistered = true

‎tests/pubspec.yaml‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ dependencies:
3535
firebase_database: ^13.0.1
3636
firebase_database_platform_interface: ^0.4.2+9
3737
firebase_database_web: ^0.2.9+16
38+
firebase_in_app_messaging: ^0.10.1
39+
firebase_in_app_messaging_platform_interface: ^0.2.7+30
3840
firebase_messaging: ^17.0.1
3941
firebase_messaging_platform_interface: ^4.11.1
4042
firebase_messaging_web: ^4.3.1

0 commit comments

Comments
 (0)