9

I mean do they use ping-pong messages to get user connection information? In short, how they know where to send for coming request?

3 Answers3

11

For iOS, these apps use APNS (Apple Push Notification Service).

This is a service provided by Apple that helps applications to alert users when something happens.

In short (and simplified), it works like this

  • When the app is installed, the user is asked if the app should be allowed to send push messages to the phone.

  • If the user accepts, the app registers to the APNS server (hosted by Apple) and registers a "device token". This token is a serial number that helps the Apple Server to keep track of the phone.

  • The app connects to the application server (for example Viber's server) and sends the device token also to this server. The server will connect it to a specific user.

  • When the user receives a call, the viber server contacts the APNS server, which in turn alerts the user.

  • The APNS server keeps track of all apps the user has on his/her phone. It will handle notifications for all of them. The connection between the iPhone and the APNS server is built-in to the iOS platform and it happens automatically on a regular basis.

  • When the APNS server is told by the viber server that something is happening for specific device token, the APNS server will contact that specific phone and send a message to the phone. In this case the user will be told that he/she has an incoming call from viber.

Documentation for APNS can be found here: Local and push notification programming guide

When it comes to Android, there is a equivalent service, C2DM My guess is that these apps are working in a similar way on Android using this service.

If you need a little help getting started with Push, there is a great service called Urban Airship that makes things a lot easier, it support both iOS, Android and other platforms.

EDIT: In the case where the application is already open the connection can either be kept open using a socket connection, or content can be refreshed by polling. It depends on how time critical the application is.

jake_hetfield
  • 3,388
  • 1
  • 23
  • 30
  • Very well explained answer! Thanks :) – Omer Faruk Celebi May 30 '12 at 08:05
  • How does these apps work for sending multimedia content like images, video as APNS does not support sending video, images using push notification? – Animesh Dec 17 '13 at 12:08
  • The media is not sent in the actual push, the push just tells the user that there is a new message (and shows some of the text in the push). When the user taps the push, the app will open, and the app can now connect to the server and download all messages and media contents. You can see this in whatsapp, when you receive a new message via push, and open whatsapp, the new message isn't in the list. It will appear after a few seconds, when whatsapp actually downloaded it from the server. – jake_hetfield Dec 19 '13 at 09:13
  • socket connection or time-interval polling drains battery a lot. Long-polling (or comet) is better solution. Check this [answer](http://stackoverflow.com/questions/1086380/how-does-facebook-gmail-send-the-real-time-notification) for how facebook does in their web application – Sang Dec 17 '15 at 10:53
1

In such applications, users are track based on Android Device Unique ID.

import android.provider.Settings.Secure;

private String android_id = Secure.getString(getContext().getContentResolver(),
                                                    Secure.ANDROID_ID);
laurent
  • 88,262
  • 77
  • 290
  • 428
HTWoks
  • 11
  • 1
  • Actually, I asked this question for connection layer. How they find or track receiver connection information? For example lets think they use socket for sending text. But in this case they should know where to open a connection. That is what I am looking for. Thanks for your answer :) – Omer Faruk Celebi May 30 '12 at 07:27
0

These apps all connect to the provider's server and keep the connection open. So the server is able to tell whether the client is connected. From then on, it's just normal network communication:

  1. Client #1 sends message to server together with destination information
  2. Server sends notification to destination client that new message is available
  3. Client #2 retrieves message from server upon notification
Thorsten Dittmar
  • 55,956
  • 8
  • 91
  • 139
  • But they have nearly millions of users? Are you sure that they keep all connection open? – Omer Faruk Celebi May 30 '12 at 07:31
  • This is roughly the way that push email works, too (the server and client keep an open connection), so keeping that many connections open doesn't seem to be a problem. – Thorsten Dittmar May 30 '12 at 07:39
  • Actually the connection will be shutdown by the cell-network provider but there are ways that allow you to achieve the same by using something called [long polling](http://en.wikipedia.org/wiki/Push_technology#Long_polling). – Till May 30 '12 at 07:48
  • Although this is a part of the truth, lots of information is missing here. The question is specifically about 3rd part applications made for iOS and Android. In these cases connection is not held open by the app itself. That would consume large amount of battery, and the OS can not let applications run in background constantly. In iOS it is not even allowed by the platform. The phone only holds only one connection open for all apps and that is to the push service provided by Apple/Google and that in turn keeps track of the apps that the user has registered to receive push notifications from. – jake_hetfield May 30 '12 at 08:02
  • I guess we should describe the scenario a little better to answer correctly. @jake_hetfield implies that only Push-Notifications would be the clue - that certainly is only partly true as those are not needed once the app is open but only once it is in background. – Till May 30 '12 at 08:11
  • When the app is open, the connection is easily or already established, at this point it is a very trivial task to keep track of users. It would be similar to the problem "when a user connects to a web site and asks for a page, how does the server know to whom to send the page". Of course the server needs to keep track of user accounts etc, but being an app developer I assumed it is the non-trivial problem the question was about, which is to connect to the user when the connection is not open. – jake_hetfield May 30 '12 at 08:28
  • @Till the problem here I think is for the receiver side. From the app perspective even the receiver doesn't start the application, for example server knows where to send the incoming sms text. Thus server should keep track of all user connection information. I hope I'm more clear now. – Omer Faruk Celebi May 30 '12 at 08:44
  • @jake_hetfield I must have missed the part of the question that states "connect to the user when the connection is not open". – Till May 30 '12 at 09:43
  • @Till you are right there is no such part, as I said I only made that assumption since it seemed more likely to me that it was the issue. I added a note about the case you mentioned to my answer. – jake_hetfield May 30 '12 at 10:20
  • Great to see that even though my answer got downvoted, it has the longest discussion thread :-) – Thorsten Dittmar May 30 '12 at 10:22