FirebaseMessaging Android some messages are lost
<p>I use FirebaseMessaging in an Android Application for an instant messaging.</p>

<p>For messages from server to device Android, there is no loss of message.</p>

<p>But from mobile to server some messages are lost...</p>

<p>The server receives the ACK messages, but in the other direction (mobile to server) the mobile does not receive the ACK.</p>

<p>The consequence is that 30% approximately of messages are lost. If I send multiple messages quickly, there will be more loss (40-60%). If I send multiple message slowly, there are 10% lost messages...</p>

<p>When I send messages mobile to server, I noticed that in my FirebaseMessagingService, the method "onMessageSent" is called by vague of 10 messages. Is it normal ? 
Why "onMessageSent" is not called once as soon as message was sent...</p>

<pre><code>public class MyFirebaseMessagingService extends FirebaseMessagingService {

private static final String TAG = "MyFirebaseMsgService";
/** constante pour renvoyer la position GPS */
public static final String BROADCAST_FIREBASE_FILTER= "android.intent.action.MY_APP_FIREBASE";

private DatabaseHelper helper;

@Override
public void onCreate() {
    super.onCreate();
}

/**
 * Called when message is received.
 *
 * @param remoteMessage Object representing the message received from Firebase Cloud Messaging.
 */
// [START receive_message]
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    Notification method below.       
    DebugLog.logw(TAG,"MESSAGE RECEIVED");
    String clickAction = remoteMessage.getNotification().getClickAction();        
    [..]

}
// [END receive_message]

/**
 * Create and show a simple notification containing the received FCM message.
 *
 * @param messageBody FCM message body received.
 */
private void sendNotification(String title, String messageBody,Intent intent2) {

    intent2.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent2,
            PendingIntent.FLAG_ONE_SHOT);

    Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle(title)
            .setContentText(messageBody)
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent);

    NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());

}

@Override
public void onDeletedMessages() {
    super.onDeletedMessages();
    DebugLog.logi(TAG, "onDeletedMessages");
}

@Override
public void onMessageSent(String s) {
    super.onMessageSent(s);
    DebugLog.logi(TAG, "onMessageSent = " + s);
}

@Override
public void onSendError(String s, Exception e) {
    super.onSendError(s, e);
    DebugLog.logi(TAG, "onSendError = " + s);
}
</code></pre>

<p>}</p>

<p>My method in my FragmentMessaging for send an message : </p>

<pre><code>public void sendMessage(Message msg){
    DebugLog.logd("FragmentMessagerie","sendMessage msg = " + msg.getMsg());
    if(textMessage.getText().length()&gt;0) {
        final Message message = msg;        

        AtomicInteger atomicInteger = new AtomicInteger();
        FirebaseMessaging fm = FirebaseMessaging.getInstance();

        RemoteMessage rm = new RemoteMessage.Builder(senderID)
                .setMessageId("myApp_" +  atomicInteger.incrementAndGet() + System.currentTimeMillis())
                .addData("action", "chat")
                .addData("destinataire", String.valueOf(contact.getId()))
                .addData("emetteur",username)
                .addData("texte",msg.getMsg())
                .setTtl(0)
                .build();                    
        fm.send(rm); // appel à fireBase pour l'envoi du message  
        [..]

        textMessage.setText("");
    } else {
        [..]
    }
}
</code></pre>

<p>}</p>

<p>My questions are :</p>

<p>When sending a message (firebaseMessaging.send(remoteMessage)) Firebase is supposed to handle the sending of messages to server.</p>

<p>Is it Android that sends the messages incorrectly to Firebase ?</p>

<p>Or is it Firebase that sends the messages badly to my server ?</p>

<p>On the server, I receive the ACK messages sent to mobiles.</p>

<p>On my device Android, I don't receive the ACK message, only the method "onMessageSent" was called 10 times when 10 messages were sent...  Is this normal ?</p>

<p>How can I receive ACK for messages device Android to server ? from my device ?</p>

<p>Thanks in advance for your help !</p>