Android 当应用程序在前台时,GCM 消息不调用 onMessageReceived
Android GCM message not calling onMessageReceived when the app is in the foreground
If you want foregrounded apps to receive notification messages or data messages, you’ll need to write code to handle the onMessageReceived callback.
我有:
- 我有扩展
FirebaseMessagingService
的服务并且
如文档中所述覆盖 onMessageReceived
(也在清单和权限中)。
- 消息的有效负载在
data
映射中,并且
notification
对象为空(如预期)。
- 应用程序在后台时的通知按预期工作。
- 消息由 SendBird(第 3 方聊天解决方案)发送。在他们端和我们的其他客户端实现中一切都很好(ios、网络和后端)
问题是当应用程序在前台时,onMessageReceived
回调没有被调用。
我错过了什么吗?不应该是同一服务处理收到的消息的应用程序的两个应用程序状态吗?我不想在应用程序已打开时显示系统栏通知,但我确实想更改 UI 中的某些元素以指示用户他们在聊天中有新消息。
代码如下:
服务:
class ChatMessagingService : FirebaseMessagingService() {
override fun onMessageReceived(remoteMessage: RemoteMessage) {
super.onMessageReceived(remoteMessage)
Timber.e("----------- MESSAGE RECEIVED -------------")
displayNotification(remoteMessage)
sendBroadcastToActivity(remoteMessage) //
}
}
在清单中
<service
android:name=".notifications.ChatMessagingService"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
firebase 和 gcm 版本
implementation 'com.google.android.gms:play-services-gcm:17.0.0'
implementation 'com.google.firebase:firebase-core:17.2.1'
implementation 'com.google.firebase:firebase-messaging:20.1.0'
提前谢谢大家
编辑,这个方法是评论者要求的,但它甚至没有达到,所以我认为这不是问题所在。
private fun sendBroadcastToActivity(remoteMessage: RemoteMessage) {
val messageData = Gson().fromJson<SendBirdPNPayload>(remoteMessage.data["sendbird"], SendBirdPNPayload::class.java)
Intent(MESSAGE_RECEIVED_ACTION).also { intent ->
intent.putExtra("message_id", messageData.messageId)
LocalBroadcastManager.getInstance(this).sendBroadcast(intent)
}
}
扩展 FirebaseMessagingService 的 class 名称与您在 Manifest.xml 中定义的服务名称不同。试试这个:
<service
android:name = ".ChatMessagingService"
android:stopWithTask="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
感谢大家的帮助。我们发现这不是我们代码的问题,一切都如预期的那样。如果用户连接到聊天(通过 sdk)并且应用程序在前台,则聊天的第 3 方解决方案不会发送推送通知。所以我们必须使用另一种技术来更改 UI.
再次感谢大家的帮助!
If you want foregrounded apps to receive notification messages or data messages, you’ll need to write code to handle the onMessageReceived callback.
我有:
- 我有扩展
FirebaseMessagingService
的服务并且 如文档中所述覆盖onMessageReceived
(也在清单和权限中)。 - 消息的有效负载在
data
映射中,并且notification
对象为空(如预期)。 - 应用程序在后台时的通知按预期工作。
- 消息由 SendBird(第 3 方聊天解决方案)发送。在他们端和我们的其他客户端实现中一切都很好(ios、网络和后端)
问题是当应用程序在前台时,onMessageReceived
回调没有被调用。
我错过了什么吗?不应该是同一服务处理收到的消息的应用程序的两个应用程序状态吗?我不想在应用程序已打开时显示系统栏通知,但我确实想更改 UI 中的某些元素以指示用户他们在聊天中有新消息。
代码如下:
服务:
class ChatMessagingService : FirebaseMessagingService() {
override fun onMessageReceived(remoteMessage: RemoteMessage) {
super.onMessageReceived(remoteMessage)
Timber.e("----------- MESSAGE RECEIVED -------------")
displayNotification(remoteMessage)
sendBroadcastToActivity(remoteMessage) //
}
}
在清单中
<service
android:name=".notifications.ChatMessagingService"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
firebase 和 gcm 版本
implementation 'com.google.android.gms:play-services-gcm:17.0.0'
implementation 'com.google.firebase:firebase-core:17.2.1'
implementation 'com.google.firebase:firebase-messaging:20.1.0'
提前谢谢大家
编辑,这个方法是评论者要求的,但它甚至没有达到,所以我认为这不是问题所在。
private fun sendBroadcastToActivity(remoteMessage: RemoteMessage) {
val messageData = Gson().fromJson<SendBirdPNPayload>(remoteMessage.data["sendbird"], SendBirdPNPayload::class.java)
Intent(MESSAGE_RECEIVED_ACTION).also { intent ->
intent.putExtra("message_id", messageData.messageId)
LocalBroadcastManager.getInstance(this).sendBroadcast(intent)
}
}
扩展 FirebaseMessagingService 的 class 名称与您在 Manifest.xml 中定义的服务名称不同。试试这个:
<service
android:name = ".ChatMessagingService"
android:stopWithTask="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
感谢大家的帮助。我们发现这不是我们代码的问题,一切都如预期的那样。如果用户连接到聊天(通过 sdk)并且应用程序在前台,则聊天的第 3 方解决方案不会发送推送通知。所以我们必须使用另一种技术来更改 UI.
再次感谢大家的帮助!