After setting up the Google Cloud Messaging and receiving text from the server, how do I process the data? The answer is to implement a broadcast receiver to receive the text and then process the JSON object.

  1. Implement a broadcast receiver.
  2. Start an IntentService.
  3. Create an activity which can handle the JSON object.

How this works.

The Android system receives the message from GCM and then it creates a system wide broadcast announcement that an event has been triggered related to GCM. The broadcast receiver in our Android application will receive that event and will pass it to the IntentService and can invoke the activity to process the JSON object which we have received. This is how we can get data from a notification in GCM.

This is the second part of the Android push notification using new GCM tutorial.

Implementing the broadcast receiver

Now we will implement the broadcast receiver for receiving the push notification from an Android system. When the message arrives, the Android operating system broadcasts an announcement and our task is to catch that announcement and we will pass this announcement to the IntentService so that we can further process the received JSON message.
Snippet 1

Manifest file of GCM 

Make sure to add all the required permissions in the Android manifest file. You need to add the permissions like “receiving the message” and “getting the protection level as signature”. Also make sure to add the metadata tag in your Android manifest file.
Snippet 2


The broadcast receiver will invoke the IntentService and with its help, the notification is displayed on the device. Now our task is to get the data from this notification. So we will implement one more activities to process the received JSON and we’ll pass the JSON to this activity in order to get real string data.
Snippet 3

Manipulating the JSON object

We will use this activity file to process the JSON and process the data. We get the string value of the different JSON elements. Our task is to extract the data out of the JSON file so that we can use that data to display it in our activity. In the onCreate() method, create a JSON object and with the help of the intent, get data and store it in the JSON object and process it.
Snippet 4


If you have any doubts or suggestions, feel free to comment below.