FCM with open app does not get notifications

1

Well as the title says, my test app with FCM does not get PUSH notifications when I have the app open, if I close the app they arrive normally, which seems strange to me since most people have problems with the app closed and not open, I leave my classes:

public class MiFirebaseInstanceIdService extends FirebaseInstanceIdService {

public static  String TAG = "";

@Override
public void onTokenRefresh() {
    super.onTokenRefresh();

    String token = FirebaseInstanceId.getInstance().getToken();
    MiFirebaseInstanceIdService.TAG = token;
    Log.d(TAG, "Token: " + token);

    enviarTokenAlServidor(token);
}

private void enviarTokenAlServidor(String token) {
    // Enviar token al servidor
}

}

public class MiFirebaseMessagingService extends FirebaseMessagingService {

public static final String TAG = "NOTICIAS";

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    super.onMessageReceived(remoteMessage);

    String from = remoteMessage.getFrom();
    Log.d(TAG, "Mensaje recibido de: " + from);

    if (remoteMessage.getNotification() != null) {
        Log.d(TAG, "Notificación: " + remoteMessage.getNotification().getBody());

        mostrarNotificacion(remoteMessage.getNotification().getTitle(), remoteMessage.getNotification().getBody());
    }

    if (remoteMessage.getData().size() > 0) {
        Log.d(TAG, "Data: " + remoteMessage.getData());
    }

}

private void mostrarNotificacion(String title, String body) {

    Intent intent = new Intent(this, MainActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);

    Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setContentTitle(title)
            .setContentText(body)
            .setAutoCancel(true)
            .setSound(soundUri)
            .setContentIntent(pendingIntent);

    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(0, notificationBuilder.build());

}

}

Manifest:

<service

        android:name=".MiFirebaseInstanceIdService">
        <intent-filter>
            <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
        </intent-filter>
    </service>

Well how to reconfirm with the CLOSED app if they arrive normally, now if I have the app OPEN I do not get the notifications, Any help Thanks

    
asked by Bruno Sosa Fast Tag 05.02.2018 в 15:26
source

1 answer

2

Maybe it could be because you need to define in your AndroidManifest.xml , your service MiFirebaseMessagingService :

 <service android:name="MiFirebaseMessagingService">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT" />
  </intent-filter>
</service>
    
answered by 05.02.2018 / 20:35
source