Search code examples
androidxmppuser-presence

Know Presence of the user itself


In my chat application i want that when the person is not on the chat screen then he should receive the notifications and when the person is not on the chat screen then he should not receive any notifications ( chat should be simply displayed on the chat screen itself).

When the person is on the chat screen, I forcefully change the status the of the user and broadcast it. Now here the problem is that when the user will receive any msg/chat now how will the service running on the user's device will decide the presence of the mobile user itself ( i.e is he is on the chat screen or not ). I think it is not possible to get the presence of the user itself ( with help of roster as the user itself is not listed in his roster)

this function is written in my service class

PacketFilter filter = new MessageTypeFilter(Message.Type.chat);
        final PacketCollector collector = connection.createPacketCollector(filter);
        connection.addPacketListener(new PacketListener() {

            @Override
            public void processPacket(Packet packet) {
                // TODO Auto-generated method stub
                //notification(packet.getFrom());
                packet = collector.nextResult();
                Message message = (Message)packet;
                senderName = packet.getFrom();

                int alphaPOS = senderName.indexOf("@");
                String subSenderName = senderName.substring(0,alphaPOS); 



                notificationforChat(subSenderName+": "+message.getBody(),packet.getFrom().toString());

            }
        }, filter);

Now how to stop the notifications when the user is on the chat screen itself???

Please help me


Solution

  • In your activity write this method.

            public static boolean isVisible = false;
            @Override
            public void onWindowFocusChanged(boolean hasFocus) {
                // TODO Auto-generated method stub
                super.onWindowFocusChanged(hasFocus);
                if(hasFocus)
                {
                       isVisible = true;
                }
                else
                {
                       isVisible = false;
                }
            }
    

    and wherever you wish use this boolean to check that is screen visible or not.