0

我按照指示here從GCM遷移到FCM後不會觸發FCM回調處理程序。從gcm遷移到fcm後不會觸發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的開始。我的理解是,你必須在你的消息中有一個數據組件,才能觸發回調。 我基於此我的代碼和回調被觸發。

+0

但發送消息呢?爲什麼回調處理程序onMessageSent(String msgId)沒有被觸發,當我發送像上面的代碼示例中的消息? –

+1

對不起,我誤解了你的問題,我在onMessagSent上閱讀onMessageReceived。現在,我不認爲我的答案會以任何方式提供幫助。 – NickT

+0

沒問題。儘管如此,感謝您的評論。 –

0

是的,解決了我的問題。我把google-services.json放到了錯誤的文件夾中。下次我應該按照"get started guide"來實施客戶更多的精確度。