从 gcm 迁移到 fcm 后未触发 FCM 回调处理程序

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

我按照指示从 GCM 迁移到 FCM here

每当我发送消息时,都不会调用 onMessageSent 方法。

我使用以下源代码发送消息:

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());

为什么不起作用?

如果你看官方网站的例子here,你会看到这个评论:

 @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.

在 onMessageReceived 开始时。我的理解是,您的消息中必须有一个数据组件,才能触发回调。 我的代码基于此并触发了回调。

是的,解决了我的问题。我把 google-services.json 放到了错误的文件夹中。下次我应该遵循 "get started guide" 来更精确地实现客户端。