【问题标题】:smack 4.2 incoming messagessmack 4.2 传入消息
【发布时间】:2017-12-13 09:00:05
【问题描述】:

我现在可以发送按摩,下一步是接收传入的消息。我正在寻找很多方法。它们中的大多数在我的 smack 版本中已被废弃或不可用。我正在使用 smack 4.2.0。也许有人可以告诉我如何以及在哪里实现一个捕获所有传入消息的侦听器? 这是我的代码:

MainActivity.class

包 com.example.saddam.xmpp3;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import org.jivesoftware.smack.AbstractXMPPConnection;

import org.jxmpp.stringprep.XmppStringprepException;

public class MainActivity extends AppCompatActivity {
    Button b1;
    TextView chat;
    final  connectXmpp con = new connectXmpp();

    public MainActivity() throws XmppStringprepException {
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {


        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        con.execute();

        b1 = (Button) findViewById(R.id.button);
        chat = (TextView) findViewById(R.id.editText);


        b1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                AbstractXMPPConnection conn=con.getConnection();
                con.sendMessage(conn,chat);


            }
        });


    }
    }

connectXmpp.class

import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.tcp.XMPPTCPConnection;
import org.jivesoftware.smack.tcp.XMPPTCPConnectionConfiguration;
import org.jxmpp.jid.EntityBareJid;
import org.jxmpp.jid.impl.JidCreate;
import org.jxmpp.stringprep.XmppStringprepException;


import java.io.IOException;


/**
 * Created by saddam on 08.07.2017.
 */

public class connectXmpp extends AsyncTask<Void,Void,Void> {
    static AbstractXMPPConnection conn2= null;


    final EntityBareJid jid   = JidCreate.entityBareFrom("hss404@im.koderoot.net");

    public connectXmpp() throws XmppStringprepException {
    }

    @Override
    protected Void doInBackground(Void... voids) {

        XMPPTCPConnectionConfiguration config = null;
        try {
            config = XMPPTCPConnectionConfiguration.builder()
                    .setUsernameAndPassword("username", "password")
                    .setXmppDomain("dismail.de")
                    .setHost("dismail.de")
                    .setPort(5222).setKeystoreType(null)
                    .build();
        } catch (XmppStringprepException e) {
            e.printStackTrace();
        }
        conn2 = new XMPPTCPConnection(config);

        try {
            conn2.connect();
            conn2.login();


        } catch (InterruptedException e1) {
            e1.printStackTrace();
        } catch (IOException e1) {
            e1.printStackTrace();
        } catch (SmackException e1) {
            e1.printStackTrace();
        } catch (XMPPException e1) {
            e1.printStackTrace();
        }




        return null;
    }



    @Override
    protected void onProgressUpdate(Void... values) {
        super.onProgressUpdate(values);
    }

    @Override
    protected void onPostExecute(Void aVoid) {
        super.onPostExecute(aVoid);
    }

    public AbstractXMPPConnection getConnection(){
        return conn2;
    }

    public ChatManager getChatMana(AbstractXMPPConnection c){
       ChatManager chatManager = ChatManager.getInstanceFor(c);
        return chatManager;
    }

    public void sendMessage(final AbstractXMPPConnection c, TextView t){
        ChatManager chatManager = null;

        Message newMessage = null;
        chatManager = ChatManager.getInstanceFor(c);

        try {


            Chat chat = chatManager.chatWith(jid);
            newMessage = new Message();
            newMessage.setBody(t.getText().toString());
            chat.send(newMessage);


        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (SmackException.NotConnectedException e) {
            e.printStackTrace();
        }

    }

}

【问题讨论】:

    标签: android listener message smack


    【解决方案1】:

    监听传入消息的方式如下:

        //Declare your stanza listener
        private MessagePacketListener messagePacketListener;
    
        //In your cotnructor   
        messagePacketListener = new MessagePacketListener(context);
    
        //Then in your login method register your stanza listener like this
        public void login() throws SmackInvocationException, XmppStringprepException {
            connect();
    
            try {
                if (!con.isAuthenticated()) {
                    setupOmemo(con);
                    ....
                    con.addSyncStanzaListener(messagePacketListener, new StanzaTypeFilter(Message.class));
                    ....
                    initOmemo();
                }
            } catch(Exception e) {
                ....
            }
        }
    
        //Now you have your MessagePacketListener that process the incoming messages
        public class MessagePacketListener implements StanzaListener{
        private Context context;
    
        MessagePacketListener(Context context) {
            this.context = context;
        }
    
        @Override
        public void processStanza(Stanza packet) {
            Message msg = (Message)packet;
    
            //Message that have body
            if(msg.getBodies().size() > 0){
                //Text message
                //Do something with message msg.getBody()
            }
            else{
                //This must be sth like delivery receipt or Chat state msg
            }
        }
    }
    

    【讨论】:

      【解决方案2】:

      在 Smack 4.2 中,您可以使用以下接口:IncomingChatMessageListener

      示例代码:

      ChatManager chatManager = ChatManager.getInstanceFor(mConnection);
      chatManager.addIncomingListener(new IncomingChatMessageListener() {
                  @Override
                  public void newIncomingMessage(EntityBareJid from, Message message, Chat chat) {
                      // Your code to handle the incoming message
                  }
              });
      

      您还必须将以下语句添加到您的 XMPPTCPConnectionConfiguration:

      .setSendPresence(true)
      

      如果presence为false或者没有设置,那么你收到的所有消息都会保存在你的离线消息存储中,并且不会触发界面。

      希望这会有所帮助。

      【讨论】:

      猜你喜欢
      • 2011-06-27
      • 2020-01-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-31
      • 2019-01-30
      • 2016-06-01
      • 2017-10-28
      相关资源
      最近更新 更多