0
votes

I am creating a chat app using smack using openfire sever by XMPP protocol. I can chat with other user and working well. I created connection in Service class. So i am able to get notification message when i am not in my application but i get my service is restarting again and again when network problem, network change, network slow etc. So my connection is lost. and user is not always able to chat.

So i solved connection problem. like as

if( service is not runing)
  start my service 
else
 if(connection is NULL or not Connected)
  reconnection . so my connection is created

My service class:-

if(connection is NULL or not connected)
  reconnect. so my connection is created
 else
  not connected.

But now if my service is runing and connection is lost. and i open my application , a new connection is created. This connection is created by application not services. So now when i am not in my app , i did not get any notification for incoming message because incoming message was coming by service connection.

Now i have involved. please expert help me. How can i solve?

Is there way for push notification in openfire?

1
I can only recommend to have a look at the various open source XMPP clients for Android that use aSmack and how they deal with service restarts, connectivity changes, etc. Basically you want the XMPP connection instance to be managed by an service and listen for connectivity changes to reconnect the connection.Flow

1 Answers

1
votes

You could try binding to your service;

  • Create a method in your service called connect()

  • When the application is started instead of creating a connection object in activity class, bind to the service, call myServcie.connect();

  • This way the connection is still owned by the service,

Other way to communicate with the service is via intents.... send an intent to the service when activity is created and have the BroadcastReceiver in the Service call connect();

One thing to watch out for is continuously reconnecting in a mobile environment is not recommended.... battery life being what it is. You can check to see if the application package is the one that is showing and then reconnect if it is etc.

James