如何使用smack从stanza离线pubsub消息

时间:2017-10-03 07:30:01

标签: android xmpp publish-subscribe smack

我想获得离线消息,我使用pubsub并且我在节点上添加了addItemEventListener,但是当用户上线时,用户在中获取消息而不是在eventlistener&#39中; s handlePublishedItems方法。 我想知道如果我做错了什么?这就是为什么它在节中接收消息的原因?因为我展示了显示

的libraby类here的方法
public void addItemEventListener(@SuppressWarnings("rawtypes") ItemEventListener listener)
    {
        StanzaListener conListener = new ItemEventTranslator(listener); 
        itemEventToListenerMap.put(listener, conListener);
        pubSubManager.getConnection().addSyncStanzaListener(conListener, new EventContentFilter(EventElementType.items.toString(), "item"));
}

这也是设置stanzalistener但我没有在handlePublishedItems()方法中获取它。我的代码和流程在下面,任何人都可以帮助我吗?提前谢谢

XMPPTCPConnectionConfiguration config = null;
            try {
                config = XMPPTCPConnectionConfiguration.builder()
                        .setUsernameAndPassword("user1", "Test_123")
                        .setHost(getString(R.string.domain))
                        .setXmppDomain(getString(R.string.domain))
                        .setSecurityMode(ConnectionConfiguration.SecurityMode.disabled)
                        .setPort(5222)
                        .setDebuggerEnabled(true) // to view what's happening in detail
                        .build();
            } catch (XmppStringprepException e) {
                e.printStackTrace();
            }

            conn1 = new XMPPTCPConnection(config);
            conn1.setUseStreamManagement(true);
            XMPPTCPConnection.setUseStreamManagementDefault(true);
            conn1.setUseStreamManagementResumption(true);

            ReconnectionManager.getInstanceFor(conn1).enableAutomaticReconnection();
//            conn1.setPacketReplyTimeout(10000);
            try {
                conn1.connect();
                if (conn1.isConnected()) {
                    Log.w("app", "conn done");
                }
                conn1.login();

                if (conn1.isAuthenticated()) {
                    Log.w("app", "Auth done");
                }
            } catch (Exception e) {
                Log.w("app", e.toString());
            }



            StanzaFilter filter=new StanzaFilter(){
                @Override
                public boolean accept(Stanza stanza) {
                    if (stanza instanceof Presence) {
                        Presence pres=(Presence)stanza;
                        Log.d("stanzafilter", String.valueOf(pres));
                        if (pres.getType() == Presence.Type.subscribe)           return true;
                    }
                    return false;
                }
            };


            conn1.addAsyncStanzaListener(new StanzaListener() {
                @Override
                public void processStanza(Stanza packet) throws SmackException.NotConnectedException, InterruptedException {
                    Log.d("processStanza", String.valueOf(packet));
                }
            },filter);

            PubSubManager mgr = PubSubManager.getInstance(conn1);

// Get the node
            node = null;

            try {
                node = mgr.getNode(node1);
                Log.d("getNode", String.valueOf(node));
            } catch (SmackException.NoResponseException | XMPPException.XMPPErrorException |
                    InterruptedException | SmackException.NotConnectedException e) {
                e.printStackTrace();
            }



            assert node != null;

            node.addItemEventListener(new ItemEventListener() {
                @Override
                public void handlePublishedItems(ItemPublishEvent items) {
                    System.out.println("======================handlePublishedItems==============================");
                    System.out.println(items.getItems());
 }
            });


            try {
                node.subscribe(String.valueOf(conn1.getUser()));

                Log.d("subscribe", String.valueOf(node));
                Log.d("getUser", String.valueOf(conn1.getUser()));
            } catch (SmackException.NoResponseException | XMPPException.XMPPErrorException |
                    InterruptedException | SmackException.NotConnectedException e) {
                e.printStackTrace();
            }

和我收到的消息一样,

<message to='user1.....' from='pubsub.dev1.internal.llp'>
    <event
        xmlns='http://jabber.org/protocol/pubsub#event'>
        <items node='test1'>
            <item publisher='user2@dev1.internal.llp/test1' id='5E338FA5B6C6'>
                <message
                    xmlns='pubsub:test:message'>
                    <body>android pubsub test22222</body>
                </message>
            </item>
        </items>
    </event>
    <delay
        xmlns='urn:xmpp:delay' from='dev1.internal.llp' stamp='2017-10-03T07:09:46.403720Z'>Offline Storage
    </delay>
</message>
<r
    xmlns='urn:xmpp:sm:3'/>

如何从本节获取消息?

0 个答案:

没有答案