caknia
caknia

Reputation: 1251

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:

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.

Upvotes: 6

Views: 3633

Answers (3)

Andrea Thacker
Andrea Thacker

Reputation: 3470

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.

Upvotes: 4

Sariban D&#39;Cl
Sariban D&#39;Cl

Reputation: 2227

First of all you will not get any registration number from GCM server. You will only get Project Id and App Key from Google Developer Console. You have to create registration ID using your client app. You have to write some codes to create registration ID. You can follow this link. Here its explained very well.

Upvotes: 1

Fahim
Fahim

Reputation: 12398

You need to follow this steps to get the gcm Id from the android device. Each device will have a unique gcm Id for each app.

  1. You have to register on Google api console
  2. Follow this link to get the gcm id from the app

Upvotes: 0

Related Questions