how to get offline pubsub message from stanza using smack
Asked Answered
S

0

7

I want to get the offline message, I'm using pubsub and I have added addItemEventListener on a node but when the user gets online then the user gets a message in stanza, not in the event listener's handlePublishedItems method. I want to know that if I'm doing anything wrong? that's why it is receiving messages in the stanza? because I have show method of library class here that shows

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"));
}

that is also setting stanzalistener but I'm not getting it in handlePublishedItems() method. my code and flow are below, so can anyone help me? thanks in advance

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();
            }

and message i got in stanza like,

<message to='user1.....' from='pubsub.dev1.internal.llp'>
    <event
        xmlns='http://jabber.org/protocol/pubsub#event'>
        <items node='test1'>
            <item publisher='[email protected]/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'/>

how to get a message from this stanza?

Stpierre answered 3/10, 2017 at 7:30 Comment(1)
did you find any solution I need itBrazilin

© 2022 - 2024 — McMap. All rights reserved.