Android startActivity在fcm中接收的消息内部不工作
我知道有很多人在讨论这个问题。我已经在这个线程上尝试了几乎所有的解决方案,但不幸的是,在我的情况下,这些都不起作用 基本上,我想做的是,我想在调用Android startActivity在fcm中接收的消息内部不工作,android,firebase,kotlin,push-notification,firebase-cloud-messaging,Android,Firebase,Kotlin,Push Notification,Firebase Cloud Messaging,我知道有很多人在讨论这个问题。我已经在这个线程上尝试了几乎所有的解决方案,但不幸的是,在我的情况下,这些都不起作用 基本上,我想做的是,我想在调用onMessageReceived后立即启动一个活动,而不发出通知 这是我的代码: override fun onMessageReceived(p0: RemoteMessage) { super.onMessageReceived(p0) "new notif".ea() val data = p0.da
onMessageReceived
后立即启动一个活动,而不发出通知
这是我的代码:
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)
}
...
问题是,如果应用程序位于前台,则会打开NewOrderActivity
。但如果应用程序位于后台,则不会打开NewOrderActivity
。我尝试过使用BroadcastReceiver
,但也不起作用。当前compileSdkVersion
和targetSdkVersion
设置为29
。我已将其更改为28
,但也不起作用
有两种类型的消息数据消息和通知
信息。数据消息在onMessageReceived中处理
应用程序位于前台或后台。数据消息是
传统上与GCM一起使用。只接收通知消息
当应用程序位于前台时,此处为onMessageReceived。当
应用程序位于后台,将自动生成通知
显示。当用户点击通知时,他们将返回到
应用程序。包含通知和数据有效载荷的消息是
被视为通知消息。Firebase控制台始终发送
通知消息
示例代码:-您需要像下面这样在通知负载中指定单击操作
$noti = array
(
'icon' => 'new',
'title' => 'title',
'body' => 'new msg',
'click_action' => 'open_NewOrderActivity'
);
现在在manifest
文件中,在NewOrderActivity
activity标记中执行此操作
<activity
android:name=".NewOrderActivity">
<intent-filter>
<action android:name="open_NewOrderActivity" /> // should be same as in click action
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
//应与单击操作中的相同
这是因为用户未授予系统警报窗口
权限。如果有人需要知道如何授予SYSTEM\u ALERT\u窗口
permission,以下是链接
您是否收到ant error?没有ant error,我认为它将帮助您@JyotishBiswas正确设置有效负载(使用数据消息)。onMessageReceived总是被调用。“字段”:{“注册ID”:[“xxx”],“通知”:{“数据”:“,“类型”:“新订单”,“正文”:“xxx”,“单击操作”:“打开新订单活动”,“标题”:“xxx”}在清单中,您可以从活动标签中删除这些参数吗?android:launchMode=“singleInstance”android:showOnLockScreen=“true”删除这些参数并检查我的效果!非常感谢。Android的错误信息反馈很差,没有给你一个错误,在这种情况下,它只是默默地失败。
<activity
android:name=".NewOrderActivity">
<intent-filter>
<action android:name="open_NewOrderActivity" /> // should be same as in click action
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>