En mi aplicacon la funcion onremoteMessage no se llama cuando esta en segundo plano, envio desde mi servidor datos y mensaje de la notificación, solo se ejecuta en cuando la aplicacion esta abierta y ejecutándose. Lo probesin optimizacion de bateria,y sigue igual, tambien lo reince con invalidate cache y no cambia. A continuación muestro el codigo: Manifest
<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" />
clase del servicio
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, "Notificacion");
Log.d(LOGTAG, "Title: " + remoteMessage.getNotification().getTitle());
}
if(remoteMessage.getData() != null) {
Log.d(LOGTAG, "DATOS");
Log.d(LOGTAG, "Titulo: " + remoteMessage.getData().get("title"));
Log.d(LOGTAG, "Imagen: " + remoteMessage.getData().get("image"));
}
}
graddle Module App
implementation 'com.google.firebase:firebase-messaging:20.2.3'
Logcat en segundo plano:
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/imagen-coche-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 en primer Plano:
I/System.out: remote message
D/android-fcm: Notificacion
D/android-fcm: Notificatcion de Prueba
D/android-fcm: DATOS
D/android-fcm: Titulo : datos de php
D/android-fcm: Imagen : imagen