Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

startActivity not working inside onMessageReceived in fcm

I know there are many thread discussing this problem. I have tried almost all of solutions on that threads, but unfortunately that didn't work in my case.

Basically, what I want to do is, I want to start an activity immediately after onMessageReceived is called without issuing notification.

This is my code:

override fun onMessageReceived(p0: RemoteMessage) {
    super.onMessageReceived(p0)
    "new notif".ea()
    val data = p0.data
    val title = data["title"]
    val body = data["body"]
    val type = data["type"]

    if (type == NEW_ORDER) {
        val order = data["data"]!!.fromJsonObject(OrderModel::class.java)
        NewOrderActivity.open(this, order)
    }
...
class NewOrderActivity : BaseActivity() {

    companion object {
        fun open(c: Context, order: OrderModel) = c.startActivity(Intent(c, NewOrderActivity::class.java).apply {
            addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
            putExtra("order", order.toJsonObject())
        })
    }
...

The problem is, if the app is in foreground, NewOrderActivity is opened. But if the app is in background, NewOrderActivity is not opened. I have tried using BroadcastReceiver but that also does not work. Current compileSdkVersion and targetSdkVersion is set to 29. I have changed it to 28 but also does not work

like image 515
ib_ganz Avatar asked Oct 28 '25 02:10

ib_ganz


1 Answers

This is because SYSTEM_ALERT_WINDOW permission is not granted by the user. In case someone needs to know how to grant the SYSTEM_ALERT_WINDOW persmission, here is the link SYSTEM_ALERT_WINDOW - How to get this permission automatically on Android 6.0 and targetSdkVersion 23

like image 139
ib_ganz Avatar answered Oct 29 '25 18:10

ib_ganz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!