0
votes

I am creating an android chat application using a smack open fire. The problem that I am facing is I know how to send and accept subscription requests but how can I know if I have received any subscription request. I have used Stanza listener but the listener is only listening to incoming chat messages, not subscription requests. Below is my code

StanzaFilter filter=new StanzaTypeFilter(Message.class);
        PacketCollector collector=connection.createPacketCollector(filter);
        connection.addAsyncStanzaListener(new StanzaListener() {
            @Override
            public void processPacket(Stanza packet) throws SmackException.NotConnectedException {
                Log.wtf("MA","Stanz listner");
            }
        },filter);

I am a beginner in smack, please help me how can I listen for incoming subscription request. Thanks in advance.

2
when you send subscription request to this user? this "Stanz listner" does not print in logs?Farhan
StanzaTypeFilter will implement a function 'accept' what have you returned in that?Farhan
I am just using this module for to listen to incoming subscription..Am i wrong? If i am wrong can you tell me what should i write there instead of stanza filter..Thanks in advanceMohit Gaur
this should work. just remove packetCollector line, I am guessing you don't need this from your code.Farhan

2 Answers

0
votes

A subscription request is not a Message. It is a Presence instead.

So, you probably should try:

StanzaFilter filter = new StanzaTypeFilter(Presence.class);

Reference: RFC 3921 - Section 6 - Managing Subscriptions

0
votes

This is how I have done in my android code

StanzaFilter subscribefilter = PresenceTypeFilter.SUBSCRIBE;
PresenceSubscribeListener subscribeListener = new PresenceSubscribeListener(context.getApplicationContext(), connection);
connection.addSyncStanzaListener(subscribeListener, subscribefilter);

And then this connection object has been referenced in a long running service in Android. This has been done such that when a packet is received when you app is in background, you can still process the incoming presence packet.

P.S. I am using smack 4.1.9 in my android code.