This tutorial will walk you through how to successfully send push notifications to an application using Google Cloud Messaging (GCM).
Part 1—Client App covers the setup needed for a client application to register with GCM, send that registration information to the app server, and handle notifications it receives.
The parts in this process are:
- Create a Google Developer Project
- Enable API
- Add Credentials
- Create Android Project
- Get Google-Services Configuration (JSON) File
- Set Android Build Dependencies
- Android Manifest Modification
- Create Service Classes
- Fill in MainActivity.java Class
- Add in Strings
- Add in Colors
- Add in Layout
- Next Steps
Step 1: Create a Google Developer Project
Click Create Project. Give the project a name. This name can NEVER be changed. However, it’s just an identifier for the back-end code. The Project ID you see is random, and it is also only used for back-end code identification. You don’t have to worry about trying to change it to a specific value—the one it randomly generates is fine.
Step 2: Enable API
You will then be taken to the main Google Developer’s Console page. You should see the name of your project in the header toward the right, so you know you’re in the correct place.
Under Mobile APIs, click on Google Cloud Messaging. (Sometimes there’s no Mobile APIs category and everything is listed together, in which case it’s called Google Cloud Messaging for Android.)
You will then get a message saying that you can’t use the API until you’ve created credentials. That’s the next step.
Step 3: Add Credentials
Click the button Go to Credentials, which will take you to a page that looks like this:
(You can get to the same page by going to the API Manager sidebar on the left and clicking on Credentials).
Under Where will you be calling the API from?, choose your platform. E.g. To make an Android app, choose Android.
Click What Credentials Do I Need? to move on to part 2:
Name your API key. At this point, you can also make your API more secure by clicking Add Package Name and Fingerprint”. Don’t do this now though unless you know what you’re doing. Just click Create API Key.
It will now show you your API key. Copy and save it to a safe location.
Step 4: Create Android Project
Create a new project in Android Studio (or Eclipse if that’s your preference; all instructions will reference Android Studio though). Open its AndroidManifext.xml and copy its package name (e.g.
com.example.myapp) — you will need this for the next step.
Step 5: Get Google-Services Configuration (JSON) File
Make sure you’re signed into the same google account you used when you obtained your API key. Then go to the this link (part of the Google Developer’s Console) to obtain your Google Services JSON file.
You will see two fields. Click the drop-down for App Name and choose the corresponding project. Then in the space for Android Package Name, paste the package name of your Android project. Finally, click Continue to Choose and Configure Services.
If it’s not already selected, click Cloud Messaging.
Then click Enable Google Cloud Messaging to enable it. It will then show your your Server API Key (different than your project’s API key) and Server ID. Save both to the same safe location you stored your other number.
Scroll all the way to the bottom and click Continue to Generate Configuration Files.
Click Download google-services.json to get the JSON file.
google-services.json to the app/ or mobile/ directory of your Android project (e.g.
Step 6: Set Android Build Dependencies
Open your project in Android Studio (if it’s not already open) and go to the Gradle Scripts directory and open build.gradle (Project: YOUR_PROJECT_NAME).
Inside the dependencies block, include
(Only add if it’s not already there. Keep whichever version is most recent.)
From the same directory, open build.gradle (Module:app). At the top of the file, put:
Inside the dependencies block, put
Make sure the version number of play-services is the latest version. You find all versions (including the latest) in
When you’re done, a pop-up should appear to let you sync your project with the gradle files.
Step 7: Android Manifest Modifications
Open your project’s AndroidManifest.xml file (located in Manifests).
Put the following permissions code between the <use-sdk> and <application> blocks:
Put the following code in the <application> block, after the <activity> sub-block. This is the code for your broadcast receiver, which handles the messages sent from GCM. The SEND permission is held by Google Play services. This prevents other apps from invoking the broadcast receiver.
Directly below the receiver code, define the service(s) you will call. Pay attention to the “.” at the front of the name. This means you’re going to put the Java class for that service in the same folder as your MainActivity class (which is located in the folder called YOUR_PACKAGE in the
Expected Error: You will likely notice an error if you hover over the service name that says Error: Cannot resolve symbol “YOUR_SERVICE_NAME”. This is because we haven’t created the corresponding Java classes yet. We will do that in the next step.
Step 8: Create Service Classes
Create the following classes in the java directory (the same directory as MainActivity.java). The code for these classes comes from the Google-Services Sample Code.
Note: You need to include your package name at the top of every class (this is not shown in the following code).
Step 9: Fill in MainActivity.java Class
As before you will need to include your package name at the top of this class (this is not shown in the following code).
Step 10: Add in Strings
Copy the following code into the strings.xml file in the res directory in your program.
Step 11: Add in Colors
Create a colors.xml file in the res directory in your program and paste the following code.
Step 12: Add in Layout
Here is the xml code for the layout of the app. It goes in activity_main.xml in the layout directory (located in the res directory of your program). Naturally, you can modify this layout as you desire—just be sure to update MainActivity.java to reflect any changes you make.
Step 13: Next Steps
In the next section of this tutorial, I will walk you through how to implement an app that will let you send push notifications to the client app you just created.
UPDATE 4-18-2016: Part 2 IS coming (I promise), I have just been super busy and haven’t had a chance to update this site with my tutorials. I hope to get to it by this upcoming weekend or the end of the month at the latest. Thanks for your patience!