XMPP的Facebook无法发送消息(xmpp facebook not send message

2019-07-30 07:48发布

有一个可用的Android开源项目:里奇 - 毕姆项目和用于连接Facebook的聊天功能,可以使用下面的指南。

在Android的实实现聊天API步骤:

首先,我们必须实现在现有项目MemorizingTrustManager库项目。

=>对于您需要复制以下在现有项目中三个文件

MemorizingTrustManager / SRC / DE / duenndns / SSL / MTMDecision.java MemorizingTrustManager / SRC / DE / duenndns / SSL / MemorizingActivity.java MemorizingTrustManager / SRC / DE / duenndns / SSL / MemorizingTrustManager.java =>并添加以下中的值/串值。 XML

<resources>
    <string name="mtm_accept_cert">Accept Unknown Certificate?</string>
    <string name="mtm_decision_always">Always</string>
    <string name="mtm_decision_once">Once</string>
    <string name="mtm_decision_abort">Abort</string>
    <string name="mtm_notification">Certificate Verification</string>
</resources>

第二步,而不是使用SASLAuthentication如X-Facebook的平台,您可以使用下面的代码与Facebook和登录使用您的Facebook的Jabber ID连接(username@chat.facebook.com)

    public void connectToFb() throws XMPPException {

ConnectionConfiguration config = new ConnectionConfiguration("chat.facebook.com", 5222);
config.setSASLAuthenticationEnabled(true);
config.setSecurityMode(SecurityMode.required);
config.setRosterLoadedAtLogin(true);
config.setTruststorePath("/system/etc/security/cacerts.bks");
config.setTruststorePassword("changeit");
config.setTruststoreType("bks");
config.setSendPresence(false);
try {
    SSLContext sc = SSLContext.getInstance("TLS");
    sc.init(null, MemorizingTrustManager.getInstanceList(this), new java.security.SecureRandom());
    config.setCustomSSLContext(sc);
} catch (GeneralSecurityException e) {
    Log.w("TAG", "Unable to use MemorizingTrustManager", e);
}
XMPPConnection xmpp = new XMPPConnection(config);
try {
    xmpp.connect();
    xmpp.login("facebookusername", "****"); // Here you have to used only facebookusername from facebookusername@chat.facebook.com
    Roster roster = xmpp.getRoster();
    Collection<RosterEntry> entries = roster.getEntries();
    System.out.println("Connected!");
    System.out.println("\n\n" + entries.size() + " buddy(ies):");
    // shows first time onliners---->
    String temp[] = new String[50];
    int i = 0;
    for (RosterEntry entry : entries) {
        String user = entry.getUser();
        Log.i("TAG", user);
    }
} catch (XMPPException e) {
    xmpp.disconnect();
    e.printStackTrace();
}
}

例如上面运行,但不能发送message.How我可以发送邮件?

Answer 1:

希望这会为你使用。

        String msg = edtMsg.getText().toString().trim(); 
    edtMsg.setText("");

    if(!msg.equals("")){
        Message msgObj = new Message("xxx@chat.facebook.com", Message.Type.chat);
        msgObj.setBody(msg);
        connection.sendPacket(msgObj);
    }


Answer 2:

你必须设置成功登录后连接。

/ ** *通过设置对话框时调用的连接与XMPP服务器* * * @参数连接* establised /

public void setConnection(XMPPConnection connection) {

        this.connection = connection;
        if (connection != null) {
            // Add a packet listener to get messages sent to us
            PacketFilter filter = new MessageTypeFilter(Message.Type.chat);
            connection.addPacketListener(new PacketListener() {
                public void processPacket(Packet packet) {
                    Message message = (Message) packet;
                    if (message.getBody() != null) {
                        String fromName = StringUtils.parseBareAddress(message
                                .getFrom());
                        Log.i("XMPPClient", "Got text [" + message.getBody()
                                + "] from [" + fromName + "]");
                        messages.add(fromName + ":");
                        messages.add(message.getBody());
                        // Add the incoming message to the list view
                        mHandler.post(new Runnable() {
                            public void run() {
                                setListAdapter();
                            }
                        });
                    }
                }
            }, filter);
        }
    }



private void setListAdapter() {
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                R.layout.multi_line_list_item, messages);
        mList.setAdapter(adapter);
    }


文章来源: xmpp facebook not send message