我按照指示here从GCM迁移到FCM。
每当我发送消息时,都不会调用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());
为什么不工作?
答案 0 :(得分:2)
如果您查看官方网站示例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的开头。我的理解是,您的消息中必须有一个数据组件,才能触发回调。 我基于此编写了代码并触发了回调。
答案 1 :(得分:0)
是的,解决了我的问题。我把google-services.json放到了错误的文件夹中。下次我应该关注"get started guide"来实现客户端更精确。