Thursday 26 February 2015

Google Cloud Messaging (GCM) Client Code


To create a working client/Android end code, you have to create an Android application with GCM implementation. For GCM Android app, you need the following classes:

1. GCMIntentService class – which is responsible for creating the push notifications.

2. GcmBroadcastReceiver – which invokes the GCMIntentService class whenever a push notifications is received.

3. MainActivity – which is responsible for registering the android device with the Google GCM Server and returns the Registration ID.

Now, I have attached complete GCM Server and GCM Client source code. You can simply download and work with the source code. If something need to fix, please don't hesitate to correct me. If this code works for you, please don't forget to give your feedback.

Thank you very much for all users who provided their valuable feedback, thus I attached complete source code for server and client.  

11 comments:

  1. Google Cloud Messaging (GCM) Java web application ? pls

    ReplyDelete
  2. Dear Viewers,

    I received many push notifications on my phone daily. It seems that you simply download the code and run it. In the given server end source code, I mentioned (hard coded) my "Registration ID". That is why, I am receiving push notifications. While testing please replace this Registration ID with your own.
    In case of any concern please don't hesitate to contact me.
    Thank you

    ReplyDelete
  3. Replies
    1. Hi raja,
      you can find register id in "GCMServer" class of my server side code. It looks like:
      "APA91bEbKqwTbvvRuc24vAYljcrhslOw-jXBqozgH8C2OB3H8R7U00NbIf1xp151ptweX9VkZXyHMik022cNrEETm7eM0Z2JnFksWEw1niJ2sQfU3BjQGiGMq8KsaQ7E0jpz8YKJNbzkTYotLfmertE3K7RsJ1_hAA"

      In case of any concern please do let me know.
      Thank you

      Delete
    2. I think raja's query is how he can check his register id ?

      Delete
  4. sir i am getting a connection refused error.

    ReplyDelete
  5. Hey can I use the client side code and just send notification from postman?? Will it work without implementing the server side?

    ReplyDelete
    Replies
    1. Sorry for little late reply, I added this client side code implementing gcm. Therefore, need to look docs of Postman whether it supports the same client end implementation? If you already implemented this with postman, please give your feedback.

      Thank you

      Delete
  6. Hi,

    After running the client code in mobile, how can i see the registration id ?

    ReplyDelete
    Replies
    1. Hi,

      Yes, you can see the registration id. Please follow these steps:
      Open MainActivity:
      getRegistrationId Method returns the registration id as below:
      String regid = getRegistrationId();

      In case it returns null or empty, then following method register device.
      registerInBackground();

      It then save this registration id in shared preference for future use.
      In case you still need any assistance, please do let me know.

      Thank you

      Delete