为什么来自 FCM 的 remoteMessage 不能在后台工作?

why remoteMessage from FCM dont work in background?

当我从服务器发送消息时,仅当应用程序在前台 运行 时才有效。 我发送带有有效负载和通知正文的消息。 如果应用程序在后台 运行,则不会调用 onMessageReceived。 什么可能会失败 我的代码 n 清单

 <application
        android:largeHeap="true"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
       android:usesCleartextTraffic="true">
      <meta-data
            android:name="com.google.firebase.messaging.default_notification_icon"
            android:resource="@drawable/logo_mediano" />
        <meta-data
            android:name="com.google.firebase.messaging.default_notification_color"
            android:resource="@color/colorAccent" />
        <meta-data
            android:name="com.google.firebase.messaging.default_notification_channel_id"
            android:value="@string/default_notification_channel_id"/>
       <service
            android:name=".mackey.FCMService"
            android:enabled="true"
            android:directBootAware="true"
            android:exported="false"

            android:resource="@drawable/ic_menu_camera">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT" />

            </intent-filter>
        </service>
        </aplication>
        <uses-permission android:name="android.permission.INTERNET" />

我的服务class:

        public class FCMService extends FirebaseMessagingService {
    private static final String LOGTAG = "android-fcm";

    public FCMService() {
    }
   @Override
    public void onNewToken(String token) {
     sendRegistrationToServer(token);
    }
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        System.out.println("remote message");
     
        if (remoteMessage.getNotification() != null) {

            Log.d(LOGTAG, "NOTIFICATION");
            Log.d(LOGTAG, "Title: " + remoteMessage.getNotification().getTitle());
            
        }

        if(remoteMessage.getData() != null) {
             Log.d(LOGTAG, "DATA");
            Log.d(LOGTAG, "Title php : " + remoteMessage.getData().get("title"));
            Log.d(LOGTAG, "Image: " + remoteMessage.getData().get("image"));
           
        }
    }

graddle 模块应用程序

implementation 'com.google.firebase:firebase-messaging:20.2.3'

Logcat 在后台:

V/FA: Connecting to remote service
D/FA: Connected to remote service
V/FA: Processing queued up service tasks: 1
I/FirebaseMessaging: Starting download of: https://image.freepik.com/vector-gratis/imagen-tridimensional-coche-taxi-aislado-fondo-blanco_53876-12108.jpg
W/FirebaseMessaging: Notification Channel set in AndroidManifest.xml has not been created by the app. Default value will be used.
V/FA: Inactivity, disconnecting from the service

Logcat 在前台:

I/System.out: remote message
D/android-fcm: NOTIFICATION
D/android-fcm: new Notification
D/android-fcm: DATA
D/android-fcm: Title php : data  php
D/android-fcm: Title php : default

当应用程序处于后台时,onMessageReceived 不会被调用,直到用户点击与推送消息关联的通知。 如果您希望系统在用户不点击通知的情况下调用 onMessageReceived,请尝试发送静默推送消息。