Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Manage GCM Registration ID in Server

I learn about GCM server today. Reading contents in developer.android.com section GCM, I still not sure how to manage registration ID in third party server. What I understand is:

  • android app can register and unregister its ID from/to GCM server.
  • Server side can send message into devices using registration ID and
    server key.

So, how can I get device Registration ID from GCM server? Is there I need to get directly from android devices?

Thank you for your advice.

like image 812
andikurnia Avatar asked Mar 11 '15 02:03

andikurnia


People also ask

What is a GCM Sender ID?

Google Cloud Messaging (GCM) Sender ID: A unique numerical value that is created when you configure your project in the Google Developer Console/ Google Cloud Console. The Project Number is the GCM Sender ID, and this is used in the registration process to identify an app server that is permitted to send messages to the client app.

How do I get the GCM Service?

The GCM service handles all aspects of queueing of messages and delivery to client applications running on target devices, and it is completely free. To get this service from Google, however, you will need to do certain things: Enable GCM for that project. Get the SENDER_ID of the project or create a configuration file.

How to update GCM API key in connecto?

Click on “Cloud Messaging ” under “Settings” section, which will take you to following page Server Key is the GCM API Key required to be updated in the Connecto Dashboard.

How to integrate GCM with Android push notifications?

Implement an Android Client app to register with GCM, send the registration id to your push notification server and manage the notifications sent from your server via GCM. Implement a server side API to get and store registration ids from the client app and optionally provide an Admin panel to send push notification from.


1 Answers

The best place to learn about the basic information is here at developer.android.com

You will see this sample code in their tutorial:

 /**
 * Registers the application with GCM servers asynchronously.
 * <p>
 * Stores the registration ID and app versionCode in the application's
 * shared preferences.
 */
private void registerInBackground() {
    new AsyncTask() {
        @Override
        protected String doInBackground(Void... params) {
            String msg = "";
            try {
                if (gcm == null) {
                    gcm = GoogleCloudMessaging.getInstance(context);
                }
                regid = gcm.register(SENDER_ID);
                msg = "Device registered, registration ID=" + regid;

                // You should send the registration ID to your server over HTTP,
                // so it can use GCM/HTTP or CCS to send messages to your app.
                // The request to your server should be authenticated if your app
                // is using accounts.
                sendRegistrationIdToBackend();

                // For this demo: we don't need to send it because the device
                // will send upstream messages to a server that echo back the
                // message using the 'from' address in the message.

                // Persist the registration ID - no need to register again.
                storeRegistrationId(context, regid);
            } catch (IOException ex) {
                msg = "Error :" + ex.getMessage();
                // If there is an error, don't just keep trying to register.
                // Require the user to click a button again, or perform
                // exponential back-off.
            }
            return msg;
        }

        @Override
        protected void onPostExecute(String msg) {
            mDisplay.append(msg + "\n");
        }
    }.execute(null, null, null);
    ...
}

If I understand correctly you are confused about how to get the GCM RegistrationId to your server implementation. You can do that in the sendRegistrationIdToBackend() method which you will define.

You need to create an endpoint to receive a POST request that each device will post its RegistrationID to once it has received one from GCM. Then your server implementation will be aware of all the devices that have your app installed and have registered with GCM.

NOTE: One thing you may want to watch out for is not creating duplicate RegistrationIDs if a device ends up posting their RegistrationID twice, or a user uninstalls and then reinstalls the app but GCM gives them the same RegistrationID you can end up with duplicates stored in your database on the server. If this happens and you want to send a push to all your users then some of your users might end up getting multiple push notifications depending on how many duplicates are in the database.

like image 70
Andrea Thacker Avatar answered Oct 12 '22 05:10

Andrea Thacker