startActivity 在 fcm 中的 onMessageReceived 内不起作用 [英] startActivity not working inside onMessageReceived in fcm

查看:59
本文介绍了startActivity 在 fcm 中的 onMessageReceived 内不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道有很多线程在讨论这个问题.我已经尝试了该线程上的几乎所有解决方案,但不幸的是,这在我的情况下不起作用.

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.

基本上,我想做的是,我想在 onMessageReceived 被调用后立即开始一个活动,而不发出通知.

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

这是我的代码:

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())
        })
    }
...

问题是,如果应用程序在前台,NewOrderActivity 会打开.但是如果应用程序在后台,NewOrderActivity 不会打开.我曾尝试使用 BroadcastReceiver 但这也不起作用.当前 compileSdkVersiontargetSdkVersion 设置为 29.我已将其更改为 28 但也不起作用

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

推荐答案

这是因为用户没有授予 SYSTEM_ALERT_WINDOW 权限.如果有人需要知道如何授予 SYSTEM_ALERT_WINDOW 权限,这里是链接SYSTEM_ALERT_WINDOW - 如何在 Android 6.0 和 targetSdkVersion 23 上自动获得此权限

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

这篇关于startActivity 在 fcm 中的 onMessageReceived 内不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆