Skip to content

Commit

Permalink
Xiaomi fix notification click not foregrounding
Browse files Browse the repository at this point in the history
This fixes a bug where tapping on a OneSignal notification on a Xiaomi
device doesn't bring the app to the foreground.
Xiaomi has very strict activity trampolining rules on notification
open. We have to start the next activity before the current activity is
finished.

Tested on Xiaomi Redmi 6A device with MIUI 11.0.8 (Android 9) as well
as an Android 14 emulator.
  • Loading branch information
jkasten2 committed Jun 17, 2024
1 parent d3b50f1 commit 8f66f8e
Showing 1 changed file with 12 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,32 +36,26 @@ import com.onesignal.notifications.internal.open.INotificationOpenedProcessor
abstract class NotificationOpenedActivityBase : Activity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
if (!OneSignal.initWithContext(applicationContext)) {
return
}

var self = this

suspendifyOnThread {
var openedProcessor = OneSignal.getService<INotificationOpenedProcessor>()
openedProcessor.processFromContext(self, intent)
}

finish()
processIntent()
}

override fun onNewIntent(intent: Intent) {
super.onNewIntent(intent)
processIntent()
}

private fun processIntent() {
if (!OneSignal.initWithContext(applicationContext)) {
return
}

var self = this
suspendifyOnThread {
var openedProcessor = OneSignal.getService<INotificationOpenedProcessor>()
openedProcessor.processFromContext(self, getIntent())
val openedProcessor = OneSignal.getService<INotificationOpenedProcessor>()
openedProcessor.processFromContext(this, intent)
// KEEP: Xiaomi Compatibility:
// Must keep this Activity alive while trampolining, that is
// startActivity() must be called BEFORE finish(), otherwise
// the app is never foregrounded.
finish()
}

finish()
}
}

0 comments on commit 8f66f8e

Please sign in to comment.