1. Adding GCM to Android Project
Prepared for:
Rocket Tier LLC
By,
Md . Ashiq uz Zoha,
BUET , Dhaka.
2. Step 1 : Create A Google Project
● Go to this address with your browser
https://code.google.com/apis/console/
● Here there is a dropdown list at the left side
where “Project” is selected by default.Click
here and select “Create” from the list.
● A popup window appears , enter project name
here. Name the project whatever name your
Android Application has. You can select any
name but I prefer it to name as same with app.
3. Step 1 : Create A Google Project
● When a name is given, automatically all the
Google services appears and “Services” tab
at the left pane is selected for the newly
created project.
● Here , find “Google Cloud Messaging for
Android ” in the list and toggle it ON.
● Then , from the left pane , select “API Access”.
4. Step 1 : Create A Google Project
● The page will like something like this
5. Step 1 : Create A Google Project
● Now , take a look at the two red rectangles in
the previous picture.
● In the url of the page on the top , we'll see
something like
“project:XXXXXXXXXXXX:access” , the 12
digit number in the middle is the “Sender ID”.
Previously in c2dm, this was an Gmail
address.
● 2nd red rectangle contains API key , it's
needed in server side code to send Push
messages.
6. Step 1 : Create A Google Project
● Now , Go back to “Overview” in the left pane.
Here , There is a hyperlink “Register” to the
right of “Project ID”. Name it like “your-
package-name” where your app's package
name was “your.package.name” .
● After writing select “Choose this ID”.
● Done....
● *** Don't be confused with “Sender ID” and
“Project ID” , they are different thing.
7. Step 2 : Configure Android Project
● First , open Eclipse IDE and start Android SDK
Manager. Install “Google Cloud Messaging
for Android Library”.
● Create a folder called “libs” in the project
directory (Our project that will use GCM).
● Pick the file “gcm.jar” from “<Android SDK
installation path>/extras/google/gcm/gcm-
client/dist”.
● Paste “gcm.jar” into 'libs' folder.Then right click
on it and select “Add to build path”.
8. Step 3 : Code & Code
Now we have to do some code.
9. Step 3 : Code & Code
● Open “AndroidManifest.xml” of your project.
● Add the following permissions in the manifest ,
before the <application> tag...
● <uses-permission android:name="android.permission.INTERNET" />
● <uses-permission android:name="android.permission.GET_ACCOUNTS" />
● <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
● <permission android:name="<your.package.name>.permission.C2D_MESSAGE"
● android:protectionLevel="signature" />
● <uses-permission android:name="<your.package.name>.permission.C2D_MESSAGE" />
● <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
● <uses-permission android:name="android.permission.VIBRATE" />
● <uses-permission android:name="android.permission.WAKE_LOCK" />
● Replace all the “<your.package.name>” with the package name of your
Application.
10. Step 3 : Code & Code
● From project properties , select minimum SDK
version to Android 2.2 and target SDK version to the
latest version of Android
● Now go into <application> tag in the manifest. Add
following codes above all the activities , i,e just after
<application> tag. It's not necessary to keep it on top
but I think it's better to keep the module for GCM
separate everywhere , so that we can do our job with
minimal change in code. It will work , if we just write
in anywhere within
<application>XXXXX</application>.
12. Step 3 : Code & Code
● Editing the manifest is done.
● Now, Copy
– GCMIntentService.java ,
– Constant.java and
– RegistrationResultActivity.java
from attached files and paste them into
/src/<your.package.name> directory of the
project.
13. Step 3 : Code & Code
● Copy activity_result.xml and paste it to
/res/layout directory.
● Go to GCMIntentService , replace
<your.package.name> with your app's
package name. If it shows any error for copy
paste , adjust the imports for your application
package. Ctrl+Shift+O does this import work
better.
14. Step 3 : Code & Code
● Now, we are at the last stage.
● Go to the “Launcher” Activity of your
application.
● Copy ALL methods from Methods.txt which is
attached and paste into your Launcher Activity
class . Press CTRL+SHIFT+O to make
necessary imports. Replace <YourActivity>
with your current “Launcher” Activity name.
15. Step 4 : Finishing Task
● At last , after these copy-paste works done ,
just add the following code in the onCreate()
method of the Launcher Activity you are
currently working with.
if (isOnline() == true) {
InitiateGCM();
} else {
showaleart();
}
● ** isOnline() method needs this permission in manifest to work properly...
● <uses-permission android:name="android.permission.READ_PHONE_STATE" /> , so add it
to your manifest.
16. Step 5 : Finish()
● Open Constant.java file and edit following with
your relevant information :
● public static final String SENDER_ID = "SENDER ID from
Google Project ";
● public static final String SERVER_URL = "Url of your server
API for receiving registration IDs and sending message to
devices";
● public static final String App_package ="your.package.name" ;