这是我的清单
<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) 我在Android应用中使用Firebase作为我们的消息服务.我已经对Firebase进行了相当多的研究,我理解无论应用程序是在前台运行还是现在运行都会改变onMessageReceived方法中收到的数据类型.
我想要完成的是解析来自Remotemessage的传入数据,并根据自定义标记执行与它不同的操作.IE,如果数据映射包含一个名为"My_Custom_Tag"的字段,我想完全覆盖弹出的标准firebase消息并编写一个自定义消息.
问题是,当应用程序在后台运行时,我放入onMessageReceived的任何代码都不会被触发.当应用程序在前台运行时,它可以正常工作,但如果应用程序在后台,则无法检测/接收任何内容.
以下是一些示例代码:
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
Map<String, String> aMap = remoteMessage.getData();
//Custom object here for easier parsing
PushNotificationsPojo pojo = this.convertResponseToPojo(aMap);
if(pojo == null){
passToFirebase(remoteMessage);
return;
}
if(pojo.getData().getCustomTag().equals(PushNotificationsPojo.ID_TAG_CHAT_MESSAGE)) {
createCustomNotification1(pojo);
} else if (pojo.getData().getCustomTag().equals(PushNotificationsPojo.ID_TAG_SOME_OTHER_MESSAGE)){
createCustomNotification2(pojo);
} else if (pojo.getData().getCustomTag().equals(PushNotificationsPojo.ID_TAG_STUFF_AND_WHATNOT)) {
createCustomNotification3(pojo);
} else {
passToFirebase(remoteMessage);
}
}
Run Code Online (Sandbox Code Playgroud)
我的问题是,如何编辑MyFirebaseMessagingService以便我可以检查传入的数据,确定标记信息,并决定将其传递给firebase,以便它可以使用标准处理或不将其传递给firebase并编写我的我的应用程序在后台运行时,我自己的自定义显示通知?
谢谢大家!