How can i listen incoming subscription request in smack openfire android

一曲冷凌霜 提交于 2019-12-02 19:02:55

问题


I am creating an android chat application using smack openfire.The problem that i am facing is i know how to send and accept subscription request but how can i know if i have received any subscription request. I have used Stanza listener but the listner is only listening to incoming chat messages not subscription request. 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..


回答1:


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




回答2:


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.



来源:https://stackoverflow.com/questions/40206137/how-can-i-listen-incoming-subscription-request-in-smack-openfire-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!