How does GCM work ? (google cloud messaging for Android)

13,782

Solution 1

As per the GCM implementation, it requires that you implement a remote server which will manage all the requests, both incoming and outgoing. You can do this simply with a web server acting as a webservice, which will get (for instance) the requests from the clients with a HTTP POST request, and process act consequently.

Instead of having the user to check the app everytime. How can I use GCM to push notify the user?

This will be managed by the server that I just described. It will know who is subscribed and who should receive the notifications.

How will it tell which user or device to send the push notification to?

Same goes here. The server, upon a subscription, should store the users in some kind of storage (a SQL database, for instance), and this way it will know who to send notifications. This implies you'll have to implement some timeout mechanism. In my case, I make clients send a dummy HTTP POST every 30 seconds. If I don't get this request from a reasonable amount of time, I consider the client timed-out and therefore I remove them from the database.

How does GCM knows when there has been a change to tasks or when a user POST a task?

Same story, it's all handled by the server. You implement the logic of what should happen upon any request.

You might want to read this: How to send location of the device on server when needed

And also this references:

Solution 2

Android keeps one active connection to Google's servers, but it doesn't use much power or data, because no traffic is sent along it until something sends a GCM message to an app on your phone. There's only one connection on the phone, used by all apps: installing a new app that uses GCM doesn't add any extra load.

The first step in GCM is that a third-party server (such as an email server) sends a request to Google's GCM server. This server then sends the message to your device, through that open connection. The Android system looks at the message to determine which app it's for, and starts that app. The app must have registered with Android to use GCM, and it must have the relevant permission. When the app starts, it might create a notification straight away with the data from the message. GCM messages are very limited in size, so the app might instead open a normal connection to the third-party server to get more information (for example, downloading the headers of new emails).

The advantage of using push notifications is that apps don't have to run at regular intervals to check for new data, saving both power and data. The advantage of having a centralized mechanism like GCM is that the device only needs one open network connection and the Android GCM system is the only thing that needs to keep running, rather than each app having to stay running in the background to keep its own network connection to its own server.

Share:
13,782

Related videos on Youtube

user3149525
Author by

user3149525

Updated on June 18, 2022

Comments

  • user3149525
    user3149525 almost 2 years

    I have an android app which i connect to my server using REST API (django rest framework)

    here is a scenario(and maybe my plan):

    • data is sent back and forth as json
    • I have a user model and a task model where users are owners of some task.
    • Users typicaly sends a task to another user (with json similar to this: {"owner": "exampleuser", "from":"otheruser", "content":"example" ...} using POST method) -The tasks has a boolean field "completed" and is deleted once the task is completed (using PUT or PATCH method: completed = true,)
    • once a new task gets created using POST method, the only way users can see any activities concerning their tasks is through an android activity that uses GET method to get a list of all the tasks owned by the user, by looking up all objects owned by the user

    So my questions are:

    1. Instead of having the user to check the app everytime. How can I use GCM to push notify the user?
    2. How will it tell which user or device to send the push notification to?
    3. How does GCM knows when there has been a change to tasks or when a user POST a task?
  • Michael
    Michael over 9 years
    How does GCM turn a message from the server into an Intent, when the application might not be running? What is running on Android that can't be killed (i.e. will always be running whenever a message might come in) that performs this critical step, so my app gets started by an Intent?
  • Vahid
    Vahid over 8 years
    Thanks for the info. According to my question, Do you know what is the URL of an opened socket? (URL of the connection from device to GCM servers that is opened and use for notification data)