androidfirebasegoogle-cloud-platformfirebase-cloud-messaginggcmlistenerservice

How to avoid delay in sending upstream message via FCM


I am trying to send upstream message using FCM as shown in code-1 below. When I send a message from FCM to the App, it arrives to the App accompanied by and delayed upstream message, what I mean be delayed upstream message is, when the App already sends an Upstream message but the App user never get notified about its status e.g onMessageSent"

what I am trying to achieve is, to get notified immediately via "FirebaseMessagingService" when I send an upstream message. to solve this issue, I used setTtl with small values and with large values between 1 to 100000 but nothing changes, I still get notified about the upstream messages only when there is a downstream message from FCM to the App

please let me know why the callbacks in "FirebaseMessagingService" do not report the status of the sent messages from the App to the server immediately

code-1:send Upstream message:

mBtnSendUpstreamMsg = (Button) findViewById(R.id.btn_send_upstream_message);
    mBtnSendUpstreamMsg.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            FirebaseMessaging fm = FirebaseMessaging.getInstance();
            fm.send(new RemoteMessage.Builder("13xxxxx" + "@gcm.googleapis.com")
                    .setMessageId("2")
                    .addData("my_message", "Hello World")
                    .addData("my_action","SAY_HELLO").setTtl(1000000)
                    .build());
        }
    });

MyAndroidFirebaseMsgService.java

public class MyAndroidFirebaseMsgService extends FirebaseMessagingService {
private final static String TAG = MyAndroidFirebaseMsgService.class.getSimpleName();

@Override
public void onMessageSent(String s) {
    super.onMessageSent(s);
    Log.d(TAG, "onMessageSent: upstream message");
}

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    Log.d(TAG, "onMessageReceived: downstream message");
    //Log data to Log Cat
    Log.d(TAG, "onMessageReceived->From: " + remoteMessage.getFrom() +" | "+ remoteMessage.getTo());
    Log.d(TAG, "onMessageReceived->Notification Message Body: " + remoteMessage.getNotification().getBody());
    //create notification
    createNotification(remoteMessage.getNotification().getBody());

    /*
    FirebaseMessaging fm = FirebaseMessaging.getInstance();
    fm.send(new RemoteMessage.Builder("135855xx" + "@gcm.googleapis.com")
            .setMessageId("2")
            .addData("my_message", "Hello World")
            .addData("my_action","SAY_HELLO").setTtl(1000000)
            .build());
            */
}

Solution

  • You ask: ...why the callbacks in "FirebaseMessagingService" do not report the status of the sent messages from the App to the server immediately

    The documentation explains:

    To optimize network usage, FCM batches responses to onMessageSent and onSendError, so the acknowledgement may not be immediate for each message

    As I noted in my comments to the previous version of this question, I have observed a delay of about 20 minutes for onMessageSent() to be called.