[Bug]: In-App popup appearing constantly when closed with Android back button
What happened?
Hello, I have encountered an issue with OneSignal’s in-app messaging on Android. When an in-app message is displayed and dismissed using the Android back button, it initially disappears as expected. However, if the user then sends the app to the background and reopens it, the in-app message reappears, even though it was previously dismissed.
https://github.com/user-attachments/assets/26af9090-1f43-46cc-a94a-0f36136bd1ac
Steps to reproduce?
1. Open the app and trigger an in-app message (configured via OneSignal).
2. Close the message using the Android back button.
3. Send the app to the background.
4. Reopen the app.
What did you expect to happen?
The in-app message should remain dismissed after being closed with the back button.
OneSignal Android SDK version
OneSignal Android SDK 5.1.26, react-native-onesignal 5.2.8
Android version
15
Specific Android models
Google Pixel 7a on Android 16 beta
Relevant log output
Code of Conduct
- [x] I agree to follow this project's Code of Conduct
Hi, I'm experiencing the same issue on my side. Has anyone found a workaround or a fix for this? Any help would be greatly appreciated!
Thanks for reporting, we are looking into the issue.
In our initial investigation we didn't find any events from Android's APIs to get the back button press, due to OneSignal In-App Messages being a PopupWindow type.
We will keep you posted on our progress.
I’m also experiencing the same issue. It seems that the Dismiss is not being triggered when pressing the back button on Android.
@jkasten2
class ExampleFragment : Fragment() {
private var shouldHandleBack = true
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val callback = object : OnBackPressedCallback(shouldHandleBack) {
override fun handleOnBackPressed() {
Toast.makeText(requireContext(), "Back button pressed in Fragment", Toast.LENGTH_SHORT).show()
}
}
requireActivity().onBackPressedDispatcher.addCallback(viewLifecycleOwner, callback)
Handler(Looper.getMainLooper()).postDelayed({
shouldHandleBack = false
callback.isEnabled = shouldHandleBack
Toast.makeText(requireContext(), "Custom back handling disabled", Toast.LENGTH_SHORT).show()
}, 5000)
}
}
You can easily attach an OnBackPressedCallback to any existing Activity and enable or disable it based on a condition—or simply remove it entirely.
See also: https://developer.android.com/jetpack/androidx/releases/appcompat#1.7.0
@michael-winkler Thanks for the suggestions, however these Android APIs require extending an Activity / Fragment. This SDK uses a PopupWindow which doesn't extend from either of these.
- It's not possible to extend a class at runtime, so the SDK can't do this automatically for app developers. We thought of an option that this could be a manual step where you call this SDK from those events, however this won't be an option if another library / SDK the app uses displayed it's own
Activity.
The SDK was originally designed this way so it doesn't interfere the app's Activities, such as triggering lifecycle events. Moving away from a PopupWindow isn't off the table completely, but would be a major architectural change that would also have side-effects to apps. So the priority is to explore get the back button events without displaying an Activity.
@jkasten2
Why should it not be possible?
There is already a reference to the current activity in the code.
See here:
https://github.com/OneSignal/OneSignal-Android-SDK/blob/f7f82ed1aa69aa9f743b78c498ef8c2883c83e13/OneSignalSDK/onesignal/in-app-messages/src/main/java/com/onesignal/inAppMessages/internal/display/impl/InAppMessageView.kt#L64
Also, here is a small example how it can be done:
popupWindow?.showAtLocation(
currentActivity!!.window.decorView.rootView,
gravity,
0,
0,
)
val callback = object : OnBackPressedCallback(true) {
override fun handleOnBackPressed() {
if (popupWindow?.isShowing == true) {
popupWindow.dismiss()
isEnabled = false
}
}
}
currentActivity.onBackPressedDispatcher.addCallback(currentActivity, callback)
Hi all, we have fixed the unexpected behavior with in-app messages and the use of the back button in Release 5.1.35. The in-app message should now be correctly dismissed and not redisplay.
Please upgrade and let us know if you still run into any unexpected behavior.
This issue is occurring again. 1. An in-app message trigger is fired. 2. The in-app message is displayed. 3. The message is dismissed using the back button. 4. onDidDismiss is called.
So far, this is expected behavior. In the onDidDismiss callback, I set InAppMessages.paused = true to prevent further messages from being shown.
However, a duplicate in-app message is displayed again afterward. Upon investigation, I confirmed that no new triggers were fired. Additionally, during the lifecycle callback onWillDisplay, I logged that InAppMessages.paused was still set to true.
In other words, despite the pause being active, the in-app message is shown again.
I am using the latest version of OneSignal, on Android 15 (Galaxy S24)