5

I am trying to create an application similar to what's up or messenger for learning purpose.

Basically I created an activity that binds to a service which connects to a java server using TCP/IP.

While the service is alive user receives and sends messages. The problem is when the user closes the application (eg using task manager - holding down the Home button). If the application is closed the service is killed becase it has 0 binds.

My question is: What is the pattern of such applications so that the user always receive messages, how does what's up always manages to send messages and keep their service alive.

Thank You.

Catalin
  • 752
  • 1
  • 16
  • 32

3 Answers3

1

Chat applications uses both socket connection and GCM/APN. If the app is in foreground, establish a socket connection. When the app is in background, upon receiving a GCM message, again establish a socket connection. This way you will also save the battery. Keeping the socket connection open for a long time will drain the battery of the device. For a sample code, you can refer our android chat sdk:

https://github.com/AppLozic/Applozic-Android-SDK

https://github.com/AppLozic/Applozic-iOS-SDK

Devashish Mamgain
  • 2,077
  • 1
  • 18
  • 39
0

Applications like Google Talk and other instant messaging programs often use Google Cloud Messaging. There was a Google I/O talk about this that you might find interesting.

Alex Lockwood
  • 83,063
  • 39
  • 206
  • 250
  • I know that c2dm is a solution, but I read that another option is xmpp. I think xmpp works using tpc/ip sockets like in my example, so using xmpp how you resolve the service problem? Correct me if I'm wrong about xmpp. Thanks – Catalin Aug 23 '12 at 08:39
  • Hey, I have a question about keeping my service alive: http://stackoverflow.com/questions/35168769/start-in-foreground – Ruchir Baronia Feb 03 '16 at 04:56
0

If you have backend then you can take a look at OneSignal its totally free and good documented : OneSignal

I think you already know about Push Notifications :basically you don't listen server everytime. When push come the Cloud Messaging Platform will trigger phone for you.

If you want your Service always open even app in closed state use STICKY_SERVICE : start-sticky-and-start-not-sticky

Also if you want to go for Pricing Plans please read FCM : Firebase Cloud Messaging

In this page : android-migrate-fcm you should see this note :

Firebase Cloud Messaging (FCM) is the new version of GCM. It inherits the reliable and scalable GCM infrastructure, plus new features! See the FAQ to learn more. If you are integrating messaging in a new app, start with FCM. GCM users are strongly recommended to upgrade to FCM, in order to benefit from new FCM features today and in the future.

Community
  • 1
  • 1
Yasin Kaçmaz
  • 6,573
  • 5
  • 40
  • 58