2016-08-17 71 views
1

我使用smack API的官方文檔中的代碼將消息發送到特定的Jabber ID。
CLick Here如何使用smack API發送一對一消息4.1.0 android

我能夠使用下面的代碼從房間接收消息。

public void joinChatRoom(){ 
     MultiUserChatManager manager = MultiUserChatManager.getInstanceFor(connection); 
     multiUserChat = manager.getMultiUserChat("[email protected]"); 
     try { 
      multiUserChat.join("user"); 
     } catch (SmackException.NoResponseException e) { 
      e.printStackTrace(); 
     } catch (XMPPException.XMPPErrorException e) { 
      e.printStackTrace(); 
     } catch (SmackException.NotConnectedException e) { 
      e.printStackTrace(); 
     } 
     ChatManager.getInstanceFor(connection).addChatListener(new ChatManagerListener() { 
      @Override 
      public void chatCreated(Chat chat, boolean createdLocally) { 
    chat.addMessageListener(new ChatMessageListener() { 
     @Override 
     public void processMessage(Chat chat, Message message) { 
      System.out.println(message.getBody()); 
     } 
    }); 
      } 
     }); 
     System.out.println("Test"); 
    } 

問題是,我如何才能將消息發送到特定的JID,因爲我不能夠甚至很多谷歌上搜索我缺少什麼後做出來。連接是好的用戶也認證,但下面的代碼現在正在發送消息。

public void sendMsg() { 
     if (connection.isConnected()) { 
      // Assume we've created an XMPPConnection name "connection"._ 
      chatmanager = ChatManager.getInstanceFor(connection); 
      newChat = chatmanager.createChat("[email protected]", new ChatMessageListener() { 
       @Override 
       public void processMessage(Chat chat, Message message) { 
        System.out.println("Received Message:"+message); 
       } 
      }); 

      try { 
       System.out.println("check the message...."); 
       newChat.sendMessage("Howdy!alksd;lsakdsa;lkdsa;lksa;lsa"); 
      } catch (SmackException.NotConnectedException e) { 
       e.printStackTrace(); 
      } 
     } 
    } 

任何幫助將不勝感激。

+0

沒有得到,您使用會議並要求一對一聊天? – Saveen

+0

好吧,我的錯誤,我正在消除會議 –

回答

2

這裏是例如從我的最後一個項目發送消息代碼:

private void sendMessage(String body, String toJid) { 
    try { 
     Jid jid = JidCreate.from(toJid + "@" + MyApplication.CHAT_DOMAIN); 
     Chat chat = ChatManager.getInstanceFor(mConnection) 
       .createChat(jid.asJidWithLocalpartIfPossible()); 
     chat.sendMessage(body); 
    } catch (Exception e) { 
    } 
} 

UPDATE:

如何收到一條消息:

當你進行身份驗證:

ChatManager.getInstanceFor(mConnection).addChatListener(YourConnectionClass.this); 

(YourConnectionClass應該實現ChatManagerListener和ChatMessageListener)

還需要實現以下方法:

@Override 
public void chatCreated(Chat chat, boolean b) { 
    chat.addMessageListener(ChatConnection.this); 
} 

@Override 
public void processMessage(Chat chat, Message message) { 
    if (message.getType().equals(Message.Type.chat) || message.getType().equals(Message.Type.normal)) { 
     if (message.getBody() != null) { 
      Jid jid = message.getFrom(); 
      Localpart localpart = jid.getLocalpartOrNull(); 
      ... 
     } 
    } 
} 
+0

好吧,讓我檢查請 –

+0

謝謝,但你也可以請告訴我我應該如何接收現在實際上當我添加在聊天實例監聽器,它給出例外 –

1

請遵循一個幾步到一個通信的XMPP: 在StanzaListener你已經得到了所有消息添加兩個人之間的連接,但如果你想特定的獲得兩人聊天,然後使用ChatMessageListener

步驟1.聲明爲全局變量

ChatManagerListener chatListener; 
ChatMessageListener messageListener; 
Chat chat; 
private Jid opt_jid; 
StanzaListener packetListener; 

步驟2.使用的OnCreate或片段

注意此代碼:請確保您有聊天服務器連接。

try { 
     String opt_jidStr = "user_" + userid; 
     try { 
      opt_jid = JidCreate.bareFrom(Localpart.from(opt_jidStr), Domainpart.from(Common.HOST)); 
      PurplkiteLogs.logError(TAG,"opt jid :" + opt_jid); 
     } catch (XmppStringprepException e) { 
      e.printStackTrace(); 
     } 
     messageListener = new ChatMessageListener() { 
      @Override 
      public void processMessage(Chat chat, Message message) { 
       AppLogs.logInfo(TAG, "chat get me something " + message.getBody()); 
      } 
     }; 

     packetListener = new StanzaListener() { 
      @Override 
      public void processPacket(Stanza packet) throws SmackException.NotConnectedException, InterruptedException { 

       if (packet instanceof Message) { 
        final Message message = (Message) packet; 

        } 
      } 
     }; 

     chatListener = new ChatManagerListener() { 

      @Override 
      public void chatCreated(Chat chatCreated, boolean local) { 
       onChatCreated(chatCreated); 
      } 
     }; 

     XMPP.getInstance().getConnection(acitiviy)).addAsyncStanzaListener(stanzaListener, null); 
     ChatManager.getInstanceFor(XMPP.getInstance().getConnection(acitiviy))) 
      .addChatListener(chatManagerListener); 
     ServiceDiscoveryManager sdm = ServiceDiscoveryManager 
      .getInstanceFor(XMPP.getInstance().getConnection(acitiviy))); 
     sdm.addFeature("jabber.org/protocol/si"); 
     sdm.addFeature("http://jabber.org/protocol/si"); 
     sdm.addFeature("http://jabber.org/protocol/disco#info"); 
     sdm.addFeature("jabber:iq:privacy"); 


     try { 
      String addr1 = XMPP.getInstance().getUserLocalPart(getActivity()); 
      String addr2 = opt_jid.toString(); 

      if (addr1.compareTo(addr2) > 0) { 
       String addr3 = addr2; 
       addr2 = addr1; 
       addr1 = addr3; 
      } 
      chat = ChatManager.getInstanceFor(
      XMPP.getInstance().getConnection(acitiviy))) 
      .getThreadChat(party1 + "-" + party2); 
      AppLogs.logInfo(TAG, "chat value single chat :" + chat + " , " + addr2 + " , " + addr1); 
      // for subscribed the user 
      Presence subscribe = new Presence(Presence.Type.subscribe); 
      subscribe.setTo(opt_jidStr); 
      XMPP.getInstance().getConnection(acitiviy)).sendStanza(packet); 
      // for subscribed the user 

      if (chat == null) { 
      chat = ChatManager.getInstanceFor(
      XMPP.getInstance().getConnection(acitiviy)) 
      .createChat(jid, party1 + "-" + party2, 
        messageListener); 
       AppLogs.logInfo(TAG, "chat value single chat 1 :" + chat); 
      } else { 
       chat.addMessageListener(messageListener); 
       AppLogs.logInfo(TAG, "chat value single chat 2:" + chat); 
      } 

     } catch (Exception e) { 
      e.printStackTrace(); 
     } 


    } catch(Exception e) { 
     e.printStackTrace(); 
    } 

步驟3.方法一對一聊天purposer

void onChatCreated(Chat chatCreated) { 
    if (chat != null) { 
     if (chat.getParticipant().getLocalpart().toString().equals(
       chatCreated.getParticipant().getLocalpart().toString())) { 
      chat.removeMessageListener(messageListener); 
      chat = chatCreated; 
      chat.addMessageListener(messageListener); 
     } 
    } else { 
     chat = chatCreated; 
     chat.addMessageListener(messageListener); 
    } 
} 


void sendMessage(String message) { 
    if (chat != null) { 
     try { 
      chat.sendMessage(message); 

     } catch (SmackException.NotConnectedException e) { 
     } catch (Exception e) { 

     } 
    } 
} 

步驟4.在破壞

XMPP.getInstance().removeChatListener(getActivity(), chatListener); 
     if (chat != null && messageListener != null) { 

      XMPP.getInstance().getConnection(acitiviy)).removeAsyncStanzaListener(stanzaListener); 
      chat.removeMessageListener(messageListener); 
     } 

希望這會幫助你,如果你想了解更多信息,看看from here。 Thankyou

+0

非常感謝:) –

相關問題