Johannes Niederländer Johannes Niederländer - 4 years ago 227
Android Question

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 + "").setMessageId(id).setData(data).build());

Why is not working?

Answer Source

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

    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:
        // [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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download