Search code examples
androidsmack

Issue in getting friend request using smack android in offline state


I am using smack api in developing a chat application in android. I am facing an issue in getting the buddy request in offline state. I am getting the buddy request received by using the following code.

LoginScreen.connection.addPacketListener(new PacketListener() {
    public void processPacket(Packet packet) {
        final Presence newPresence = (Presence) packet;
    final Presence.Type presenceType = newPresence.getType();
    final String fromId = newPresence.getFrom();
    final String toId = newPresence.getTo();
    final RosterEntry newEntry = roster.getEntry(fromId);
    final String name = fromId.substring(0, fromId.indexOf("@"));
    if (presenceType == Presence.Type.subscribed) {
        Log.i("test-chat", "#####SUBSCRIBED#########");
    }
    if (presenceType == Presence.Type.subscribe) {
            //adding buddy request to local DB
        }
    }
 }, new PacketFilter() {
     public boolean accept(Packet packet) {
     if (packet instanceof Presence) {
         Presence presence = (Presence) packet;
     if (presence.getType().equals(Presence.Type.subscribed)
        || presence.getType().equals(Presence.Type.subscribe)
        || presence.getType().equals(Presence.Type.unsubscribed)
        || presence.getType().equals(Presence.Type.unsubscribe)
        || presence.getType().equals(Presence.Type.available)
        || presence.getType().equals(Presence.Type.unavailable)) {
         return true;
     }
     }
     return false;
  }
});

This is working fine when user is online. But suppose when the user is offline another user sents a buddy request to that user. When the user comes online I am unable to get the buddy request because the listener is not getting called. The listener needs to be called, to fetch all the buddy request that was got during offline state.

Please help.


Solution

  • Your code is fine. This happened to me as well. Well the actual problem is you are registering the listener after the request is already received.

    Try registering the listener before you login the user and you will see that the request is received even when you are offline.

    This will actually solve the problem. Try this and if you still face any problem... please elaborate your situation.

    Hope this helps.