2017-07-26 73 views
1

我試圖發送消息。與firebase xmpp服務器連接完成。我正在使用最新的smack 4.2。java - 使用smack4.2發送消息錯誤firebase xmpp

public myClient(String senderId, String serverKey) throws IOException, NotConnectedException, InterruptedException { 
     Roster.setRosterLoadedAtLoginDefault(false); 
     Jid jid ; 
     jid = JidCreate.from(HOST); 
     final XMPPTCPConnectionConfiguration conf = XMPPTCPConnectionConfiguration.builder() 
       .setCompressionEnabled(false) 
       .setSendPresence(false) 
       .setConnectTimeout(10000) 
       .setHost(HOST) 
       .setDebuggerEnabled(false) 
       .setPort(PORT) 
       .setXmppDomain((DomainBareJid) jid) 
       .setSocketFactory(SSLSocketFactory.getDefault()) 
       .setUsernameAndPassword(senderId + "@gcm.googleapis.com", serverKey) 
       .build(); 

     this.conn = new MyXMPPTCPConnection(conf); 
     try { 
       conn.connect(); 

      conn.login(); 

     } catch (XMPPException | InterruptedException | SmackException e) { 
      throw new IOException(e); 
     } 



     Roster roster = Roster.getInstanceFor(conn); 
     Collection<RosterEntry> entries = roster.getEntries(); 
     for (RosterEntry entry : entries) { 
      System.out.println(entry); 
     } 
      Entity jEntity ; 

      Jid jid1 = JidCreate.from("text"); 

      EntityBareJid jid21 = JidCreate.entityBareFrom("[email protected]"); 


      Chat chat = ChatManager.getInstanceFor(conn) 
      .createChat(jid21); 

      Message newMessage = new Message(); 
      String t = "hello this is a test message"; 
      newMessage.setBody(t.toString()); 
      /* ((Object) chat).send(newMessage);*/ 
      chat.sendMessage(newMessage); 

     StanzaFilter filter = new AndFilter(new StanzaTypeFilter(Message.class)); 

     //PacketCollector myCollector = conn2.createPacketCollector(filter); 
     // Normally, you'd do something with the collector, like wait for new packets. 

     StanzaListener myListener = new StanzaListener() { 
      @Override 
      public void processPacket(Stanza packet) throws SmackException.NotConnectedException { 
       System.out.println("packet = [" + packet + "]"); 
      } 
     }; 
     conn.addAsyncStanzaListener(myListener, filter); 

     while (true) { 
     } 
     //conn2.disconnect(); 
    } 

當我運行這種方法得到了以下錯誤

包= [你好 這是一個測試 message25667738-6dd1-4c74-a64b-2fdbac74339bInvalidJson: MissingPayload] 07月26 2017 12:52:46 PM

org.jivesoftware.smack.tcp.XMPPTCPConnection $ PacketReader parsePackets INFO:MyXMPPTCPConnection [[email protected]/41CA77 F0] (0)收到結束元素。服務器想要終止 連接,調用disconnect()

請檢查並建議。 在此先感謝。

+0

[GCM無效JSON丟失負載]的可能重複(https://stackoverflow.com/questions/30393534/gcm-invalid-json-missing-payload) – Antoniossss

回答

1

FCM XMPP服務器使用JSON語法。因此,您需要使用this documentation中FCM有效載荷所述的systax。由於XMPP使用XML,你需要嵌入JSON有效載荷在名爲GCM的XML節點如下:

<gcm xmlns:google:mobile:data> 
    JSON payload 
</gcm> 

對於例如,你的JSON有效載荷可能是這樣的:

{ 
    "to":"APA91bEXJvW4jUdxxxxx-xxxxxxxx", 
    "data":{"message":"Hello"}, 
    "priority":"high" 
} 

有關完整有關使用GCM/FCM使用XMPP連接服務器的詳細信息,請參見this documentation

使用Smack,您可以使用GcmPacketExtension創建一個類,以使用json有效內容創建gcm元素。