这是我的清单
<service android:name=".fcm.PshycoFirebaseMessagingServices">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<service android:name=".fcm.PshycoFirebaseInstanceIDService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
</intent-filter>
</service>
Run Code Online (Sandbox Code Playgroud)
当应用程序处于后台并且通知到达时,默认通知将会运行并且不会运行我的代码onMessageReceived.
这是我的onMessageReceived代码.如果我的应用程序在前台运行,而不是在后台应用程序时,则调用此方法.如何在应用程序处于后台时运行此代码?
// [START receive_message]
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
// TODO(developer): Handle FCM messages here.
// If the application is in the foreground handle both data and notification messages here.
// Also if you intend on generating your own notifications as a result of a received FCM
// message, here is where that should be initiated. See …Run Code Online (Sandbox Code Playgroud) 我正在使用FirebaseMessagingService我的推送通知。它按照应用程序中的预期工作。onMessageReceived被调用,我可以获取通知标题和正文+数据有效负载,并根据有效负载数据发送反应到我Activity使用的LocalBroadcastManager.
但后台推送通知存在问题。这只会在后台显示通知,但不会创建它,onMessageReceived因为如果应用程序在后台,则无法调用此函数。
根据文档,推送通知的数据部分存储extras在 Activity 恢复时可以找到。我已经有这个功能并且它正在工作。但问题是我没有标题和消息,因为它没有发送到onMessageReceived,我无法捕获此信息。
有什么方法可以获取吗?因为我需要在应用程序内的对话框窗口中显示它。推送通知不仅仅是文本和标题,也不仅仅是向用户提供的信息,而是会执行一些操作。
接收通知标题、正文和负载FirebaseMessagingService:
override fun onMessageReceived(msg: RemoteMessage?) {
val pNotification = msg?.notification
val payload = msg?.data
if (pNotification != null){
val ntitle = pNotification.title
val nMsg = pNotification.body
}
//working with payload - creating notification and Intent for LocalBroadcastmanager
}
Run Code Online (Sandbox Code Playgroud)
从内部的额外部分捕获有效负载onResume()
private fun maybeFindBackgroundPayload(extras: Bundle?){
if (extras != null){
extras.keySet().forEach { key->
val keyValue = extras[key]
App.log("BackgroundKeyValues: $keyValue")
if …Run Code Online (Sandbox Code Playgroud) android android-service firebase firebase-cloud-messaging android-push-notification