简体   繁体   中英

Broadcast Receiver for incoming chat message in Smack using Openfire

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM