Question

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

Was it helpful?

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.

OTHER TIPS

Is your chat screen embedded in an android activity? If so, you can override your Activity's onResume() and onPause() methods to set some sort of a flag, so that your service would now if the activity is being active(when the user is using it) or if it is not visible. One way to do this is to use shared preferences. Here's a simplified outline:

@Override
public void onResume() {
  setUserPresence(true);
  super.onResume();
}

@Override
public void onPause() {
  setUserPresence(false);
  super.onResume()
}

public void setUserPresence(boolean state) {
  SharedPreferences prefs = getSharedPreferences("settings_file_name",MODE_PRIVATE);
  SharedPreferences.Editor editor = prefs.edit();
  editor.putBoolean("UserPresent", state);
  editor.commit(); //Save your settings to the shared preferences file
}

And then all you need to do is check this flag in your service, using the same SharedPreferences approach.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top