Search code examples
androidgoogle-cloud-messagingmessagingfirebase-cloud-messaging

FCM callback handler are not triggered after migration from gcm to fcm


I migrated from GCM to FCM like instructed here.

Whenever I send a message, the onMessageSent method is not invoked.

I use following source code to send messages:

Map<String,String> data = new HashMap<String,String>();
        data.put(GcmConstants.ACTION, GcmConstants.ACTION_CHAT);
        data.put(Constants.CHAT_FLAG, Constants.FLAG_NEW_CHAT);

        ObjectMapper mapper = new ObjectMapper();
        String chatJsonInString = mapper.writeValueAsString(Helper.chatToJson(chat));

        data.put(Constants.CHAT_JSON, chatJsonInString);

        String receiverJsonInString = mapper.writeValueAsString(Helper.userToJson(receiver));
        data.put(Constants.RECEIVER_JSON, receiverJsonInString);

        String id = Integer.toString(getNextMsgId(ctxt));
        FirebaseMessaging fm = FirebaseMessaging.getInstance();
        fm.send(new RemoteMessage.Builder(senderId + "@gcm.googleapis.com").setMessageId(id).setData(data).build());

Why is not working?


Solution

  • If you look at the official website example here, then you will see this comment :

     @Override
        public void onMessageReceived(RemoteMessage remoteMessage) {
            // [START_EXCLUDE]
            // There are two types of messages data messages and notification messages. Data messages are handled
            // here in onMessageReceived whether the app is in the foreground or background. Data messages are the type
            // traditionally used with GCM. Notification messages are only received here in onMessageReceived when the app
            // is in the foreground. When the app is in the background an automatically generated notification is displayed.
            // When the user taps on the notification they are returned to the app. Messages containing both notification
            // and data payloads are treated as notification messages. The Firebase console always sends notification
            // messages. For more see: https://firebase.google.com/docs/cloud-messaging/concept-options
            // [END_EXCLUDE]
    
    // TODO(developer): Handle FCM messages here.
    

    at the start of onMessageReceived. My understanding from this is that you must have a data component in your message, for the callback to be triggered. I based my code on this and the callbacks are triggered.