Adding GCM to Android Project         Prepared for:         Rocket Tier LLC              By,       Md . Ashiq uz Zoha,    ...
Step 1 : Create A Google Project●   Go to this address with your browser    https://code.google.com/apis/console/●   Here ...
Step 1 : Create A Google Project●   When a name is given, automatically all the    Google services appears and “Services” ...
Step 1 : Create A Google Project●   The page will like something like this
Step 1 : Create A Google Project●   Now , take a look at the two red rectangles in    the previous picture.●   In the url ...
Step 1 : Create A Google Project●   Now , Go back to “Overview” in the left pane.    Here , There is a hyperlink “Register...
Step 2 : Configure Android Project●   First , open Eclipse IDE and start Android SDK     Manager. Install “Google Cloud Me...
Step 3 : Code & CodeNow we have to do some code.
Step 3 : Code & Code●   Open “AndroidManifest.xml” of your project.●   Add the following permissions in the manifest ,    ...
Step 3 : Code & Code●   From project properties , select minimum SDK    version to Android 2.2 and target SDK version to t...
Step 3 : Code & Code●   Here is the codes...    <receiver            android:name="com.google.android.gcm.GCMBroadcastRece...
Step 3 : Code & Code●   Editing the manifest is done.●   Now, Copy        –   GCMIntentService.java ,        –   Constant....
Step 3 : Code & Code●   Copy activity_result.xml and paste it to    /res/layout directory.●   Go to GCMIntentService , rep...
Step 3 : Code & Code●   Now, we are at the last stage.●   Go to the “Launcher” Activity of your    application.●   Copy AL...
Step 4 : Finishing Task●   At last , after these copy-paste works done ,    just add the following code in the onCreate() ...
Step 5 : Finish()●   Open Constant.java file and edit following with    your relevant information :●   public static final...
DONE... :-)
Upcoming SlideShare
Loading in...5
×

Google Cloud Messaging

2,084

Published on

0 Comments
7 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
2,084
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
60
Comments
0
Likes
7
Embeds 0
No embeds

No notes for slide

Google Cloud Messaging

  1. 1. Adding GCM to Android Project Prepared for: Rocket Tier LLC By, Md . Ashiq uz Zoha, BUET , Dhaka.
  2. 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. 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. 4. Step 1 : Create A Google Project● The page will like something like this
  5. 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 , well 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 , its needed in server side code to send Push messages.
  6. 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 apps package name was “your.package.name” .● After writing select “Choose this ID”.● Done....● *** Dont be confused with “Sender ID” and “Project ID” , they are different thing.
  7. 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. 8. Step 3 : Code & CodeNow we have to do some code.
  9. 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. 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. Its not necessary to keep it on top but I think its 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>.
  11. 11. Step 3 : Code & Code● Here is the codes... <receiver android:name="com.google.android.gcm.GCMBroadcastReceiver" android:permission="com.google.android.c2dm.permission.SEND" > <intent-filter> <action android:name="com.google.android.c2dm.intent.RECEIVE" /> <action android:name="com.google.android.c2dm.intent.REGISTRATION" /> <category android:name=""<your.package.name>" /> </intent-filter> </receiver> <service android:name=".GCMIntentService" /> <activity android:name=".RegistrationResultActivity" android:screenOrientation="portrait" android:theme="@android:style/Theme.NoTitleBar" />
  12. 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. 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 apps 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. 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. 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. 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" ;
  17. 17. DONE... :-)
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×