Android 如何在活动处于前台时捕获活动中的FCM通知?

Android 如何在活动处于前台时捕获活动中的FCM通知?,android,firebase,firebase-cloud-messaging,Android,Firebase,Firebase Cloud Messaging,我在android项目中设置了FCM推送通知 我有一节课 public class MyFcmListenerService extends FirebaseMessagingService { @Override public void onMessageReceived(RemoteMessage message) { // handle general push notifications } } 在我的androidmanifest.xml中,我

我在android项目中设置了FCM推送通知

我有一节课

public class MyFcmListenerService extends FirebaseMessagingService {
    @Override
    public void onMessageReceived(RemoteMessage message) {
        // handle general push notifications
    }
}
在我的androidmanifest.xml中,我有

<service
    android:name=".model.integration.fcm.MyFcmListenerService"
    android:exported="false">
    <intent-filter>
        <action android:name="com.google.firebase.MESSAGING_EVENT" />
    </intent-filter>
</service>

现在,在我的一个活动中,我希望这样做,当我的应用程序在前台时,该活动直接处理Fcm通知,而无需FirebaseMessagingService的东西做任何事情

我该怎么做

当它是gcm时,我使用了一个BroadcastReceiver,并在该特定活动中注册了BroadcastReceiver。那个BroadcastReceiver使用了一个
IntentFilter
,我可以调用
abortBroadcast()
来阻止
FirebaseMessagingService
执行其默认功能


但我在FCM案件中该怎么办?如何在活动的broadcastReceiver中捕获推送通知?

通知分为两部分。一个是“通知”块,另一个是“数据”块。如果您在“通知”块中发送数据,则您只能在应用程序位于前台时才能访问数据,但如果您在“数据”块中发送数据,则即使应用程序关闭,您也可以随时访问数据。要在“通知”块中发送数据时获取数据,只需执行以下操作:

public void onMessageReceived(RemoteMessage message) {
    // If the data is sent in "notification" block then
    message.getNotification().getTitle() //for Title
    message.getNotification().getBody() //for body
    message.getNotification().getClickAction() //for clickAction
    //etc...

    //Build your notification here using notification builder and the data you have
}
public void onMessageReceived(RemoteMessage message) {
    // If the data is sent in "data" block then
    Map<String, String> data = message.getData();

    //Now extract any data you want as it is a Map using keys that you would have sent in the "data" block

    //Build your notification here using notification builder and the data you have

}
要在“数据”块中发送数据时获取数据,只需执行以下操作:

public void onMessageReceived(RemoteMessage message) {
    // If the data is sent in "notification" block then
    message.getNotification().getTitle() //for Title
    message.getNotification().getBody() //for body
    message.getNotification().getClickAction() //for clickAction
    //etc...

    //Build your notification here using notification builder and the data you have
}
public void onMessageReceived(RemoteMessage message) {
    // If the data is sent in "data" block then
    Map<String, String> data = message.getData();

    //Now extract any data you want as it is a Map using keys that you would have sent in the "data" block

    //Build your notification here using notification builder and the data you have

}
onMessageReceived(RemoteMessage)上的公共无效信息{
//如果数据在“数据”块中发送,则
Map data=message.getData();
//现在提取您想要的任何数据,因为它是一个地图,使用您将在“数据”块中发送的键
//使用通知生成器和您拥有的数据在此处生成通知
}

通知分为两部分。一个是“通知”块,另一个是“数据”块。如果您在“通知”块中发送数据,则您只能在应用程序位于前台时才能访问数据,但如果您在“数据”块中发送数据,则即使应用程序关闭,您也可以随时访问数据。要在“通知”块中发送数据时获取数据,只需执行以下操作:

public void onMessageReceived(RemoteMessage message) {
    // If the data is sent in "notification" block then
    message.getNotification().getTitle() //for Title
    message.getNotification().getBody() //for body
    message.getNotification().getClickAction() //for clickAction
    //etc...

    //Build your notification here using notification builder and the data you have
}
public void onMessageReceived(RemoteMessage message) {
    // If the data is sent in "data" block then
    Map<String, String> data = message.getData();

    //Now extract any data you want as it is a Map using keys that you would have sent in the "data" block

    //Build your notification here using notification builder and the data you have

}
要在“数据”块中发送数据时获取数据,只需执行以下操作:

public void onMessageReceived(RemoteMessage message) {
    // If the data is sent in "notification" block then
    message.getNotification().getTitle() //for Title
    message.getNotification().getBody() //for body
    message.getNotification().getClickAction() //for clickAction
    //etc...

    //Build your notification here using notification builder and the data you have
}
public void onMessageReceived(RemoteMessage message) {
    // If the data is sent in "data" block then
    Map<String, String> data = message.getData();

    //Now extract any data you want as it is a Map using keys that you would have sent in the "data" block

    //Build your notification here using notification builder and the data you have

}
onMessageReceived(RemoteMessage)上的公共无效信息{
//如果数据在“数据”块中发送,则
Map data=message.getData();
//现在提取您想要的任何数据,因为它是一个地图,使用您将在“数据”块中发送的键
//使用通知生成器和您拥有的数据在此处生成通知
}

我最终在FCMService中创建了一些静态方法,用于在活动的onResume和onPause中设置一些变量


这样,当我收到来自FcmService的消息时,我可以有条件地跳过一些通知。

我最终在FcmService中创建了一些静态方法,以便在我的活动的onResume和onPause中设置一些变量


这样,当我从FcmService接收消息时,我可以有条件地跳过一些通知。

您调用了哪些方法?这听起来很有趣。@RichardLeMesurier对不起,伙计,我说得不够清楚。我创建了那些静态方法和静态字段。所以我可以手动决定可以跳过哪种推送通知。您调用了哪些方法?这听起来很有趣。@RichardLeMesurier对不起,伙计,我说得不够清楚。我创建了那些静态方法和静态字段。因此,我可以手动决定可以跳过哪种推送通知。