SlideShare a Scribd company logo
1 of 17
Download to read offline
Adding GCM to Android Project


         Prepared for:
         Rocket Tier LLC
              By,

       Md . Ashiq uz Zoha,
         BUET , Dhaka.
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.
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”.
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 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.
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.
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”.
Step 3 : Code & Code



Now we have to do some code.
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.
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>.
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" />
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.
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.
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.
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.
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" ;
DONE... :-)

More Related Content

What's hot

Adding User Management to Node.js
Adding User Management to Node.jsAdding User Management to Node.js
Adding User Management to Node.jsDev_Events
 
Sentiment Analysis App with DevOps Services
Sentiment Analysis App with DevOps ServicesSentiment Analysis App with DevOps Services
Sentiment Analysis App with DevOps Servicessheetal sharma
 
Google App Engine Overview - BarCamp Phnom Penh 2011
Google App Engine Overview - BarCamp Phnom Penh 2011Google App Engine Overview - BarCamp Phnom Penh 2011
Google App Engine Overview - BarCamp Phnom Penh 2011traactivity
 
Import google contacts with php or javascript using google contacts api and o...
Import google contacts with php or javascript using google contacts api and o...Import google contacts with php or javascript using google contacts api and o...
Import google contacts with php or javascript using google contacts api and o...Design 19
 
IBM cognos work space configuration.pptx
IBM cognos work space configuration.pptxIBM cognos work space configuration.pptx
IBM cognos work space configuration.pptxvishal choudhary
 
windows azure cloud services - Lab Manual
windows azure cloud services - Lab Manualwindows azure cloud services - Lab Manual
windows azure cloud services - Lab ManualArun Kumar
 
An introduction to AngularJS
An introduction to AngularJSAn introduction to AngularJS
An introduction to AngularJSYogesh singh
 
windows azure websites - Lab Manual
windows azure websites - Lab Manualwindows azure websites - Lab Manual
windows azure websites - Lab ManualArun Kumar
 
Top 7 Angular Best Practices to Organize Your Angular App
Top 7 Angular Best Practices to Organize Your Angular AppTop 7 Angular Best Practices to Organize Your Angular App
Top 7 Angular Best Practices to Organize Your Angular AppKaty Slemon
 
10 ways to bind multiple models on a view in mvc code project
10 ways to bind multiple models on a view in mvc   code project10 ways to bind multiple models on a view in mvc   code project
10 ways to bind multiple models on a view in mvc code projectAkshat Kumar
 
How to Implement Basic Angular Routing and Nested Routing With Params in Angu...
How to Implement Basic Angular Routing and Nested Routing With Params in Angu...How to Implement Basic Angular Routing and Nested Routing With Params in Angu...
How to Implement Basic Angular Routing and Nested Routing With Params in Angu...Katy Slemon
 
Google app engine setup
Google app engine setupGoogle app engine setup
Google app engine setupVinay Potluri
 
Introduction to angular js for .net developers
Introduction to angular js  for .net developersIntroduction to angular js  for .net developers
Introduction to angular js for .net developersMohd Manzoor Ahmed
 
MVC3 Development with visual studio 2010
MVC3 Development with visual studio 2010MVC3 Development with visual studio 2010
MVC3 Development with visual studio 2010AbhishekLuv Kumar
 

What's hot (15)

Adding User Management to Node.js
Adding User Management to Node.jsAdding User Management to Node.js
Adding User Management to Node.js
 
Sentiment Analysis App with DevOps Services
Sentiment Analysis App with DevOps ServicesSentiment Analysis App with DevOps Services
Sentiment Analysis App with DevOps Services
 
Google App Engine Overview - BarCamp Phnom Penh 2011
Google App Engine Overview - BarCamp Phnom Penh 2011Google App Engine Overview - BarCamp Phnom Penh 2011
Google App Engine Overview - BarCamp Phnom Penh 2011
 
Import google contacts with php or javascript using google contacts api and o...
Import google contacts with php or javascript using google contacts api and o...Import google contacts with php or javascript using google contacts api and o...
Import google contacts with php or javascript using google contacts api and o...
 
IBM cognos work space configuration.pptx
IBM cognos work space configuration.pptxIBM cognos work space configuration.pptx
IBM cognos work space configuration.pptx
 
windows azure cloud services - Lab Manual
windows azure cloud services - Lab Manualwindows azure cloud services - Lab Manual
windows azure cloud services - Lab Manual
 
An introduction to AngularJS
An introduction to AngularJSAn introduction to AngularJS
An introduction to AngularJS
 
windows azure websites - Lab Manual
windows azure websites - Lab Manualwindows azure websites - Lab Manual
windows azure websites - Lab Manual
 
Top 7 Angular Best Practices to Organize Your Angular App
Top 7 Angular Best Practices to Organize Your Angular AppTop 7 Angular Best Practices to Organize Your Angular App
Top 7 Angular Best Practices to Organize Your Angular App
 
10 ways to bind multiple models on a view in mvc code project
10 ways to bind multiple models on a view in mvc   code project10 ways to bind multiple models on a view in mvc   code project
10 ways to bind multiple models on a view in mvc code project
 
home inspection demo
home inspection demohome inspection demo
home inspection demo
 
How to Implement Basic Angular Routing and Nested Routing With Params in Angu...
How to Implement Basic Angular Routing and Nested Routing With Params in Angu...How to Implement Basic Angular Routing and Nested Routing With Params in Angu...
How to Implement Basic Angular Routing and Nested Routing With Params in Angu...
 
Google app engine setup
Google app engine setupGoogle app engine setup
Google app engine setup
 
Introduction to angular js for .net developers
Introduction to angular js  for .net developersIntroduction to angular js  for .net developers
Introduction to angular js for .net developers
 
MVC3 Development with visual studio 2010
MVC3 Development with visual studio 2010MVC3 Development with visual studio 2010
MVC3 Development with visual studio 2010
 

Similar to Google Cloud Messaging

Gcm presentation
Gcm presentationGcm presentation
Gcm presentationNiraj Singh
 
Urban Airship and Android Integration for Push Notification and In-App Notifi...
Urban Airship and Android Integration for Push Notification and In-App Notifi...Urban Airship and Android Integration for Push Notification and In-App Notifi...
Urban Airship and Android Integration for Push Notification and In-App Notifi...Zeeshan Rahman
 
Urban Airship & Android Application Integration Document
Urban Airship & Android Application Integration DocumentUrban Airship & Android Application Integration Document
Urban Airship & Android Application Integration Documentmobi fly
 
GCM Android
GCM AndroidGCM Android
GCM Androidaswapnal
 
Android tutorial
Android tutorialAndroid tutorial
Android tutorialAbid Khan
 
Creation of simple application using - step by step
Creation of simple application using - step by stepCreation of simple application using - step by step
Creation of simple application using - step by steppriya Nithya
 
android level 3
android level 3android level 3
android level 3DevMix
 
Android Kotlin Weather App Project.pdf
Android Kotlin Weather App Project.pdfAndroid Kotlin Weather App Project.pdf
Android Kotlin Weather App Project.pdfSudhanshiBakre1
 
Hello android example.
Hello android example.Hello android example.
Hello android example.Rahul Rana
 
Creating the first app with android studio
Creating the first app with android studioCreating the first app with android studio
Creating the first app with android studioParinita03
 
Android Cloud to Device Messaging Framework at GTUG Stockholm
Android Cloud to Device Messaging Framework at GTUG StockholmAndroid Cloud to Device Messaging Framework at GTUG Stockholm
Android Cloud to Device Messaging Framework at GTUG StockholmJohan Nilsson
 
Android howto hellowidget
Android howto hellowidgetAndroid howto hellowidget
Android howto hellowidgetHiron Das
 
Hacking the Codename One Source Code - Part IV - Transcript.pdf
Hacking the Codename One Source Code - Part IV - Transcript.pdfHacking the Codename One Source Code - Part IV - Transcript.pdf
Hacking the Codename One Source Code - Part IV - Transcript.pdfShaiAlmog1
 
Android N Highligts
Android N HighligtsAndroid N Highligts
Android N HighligtsSercan Yusuf
 
Introduction to Android and Java.pptx
Introduction to Android and Java.pptxIntroduction to Android and Java.pptx
Introduction to Android and Java.pptxGandhiMathy6
 
12. Android Basic Google Map
12. Android Basic Google Map12. Android Basic Google Map
12. Android Basic Google MapOum Saokosal
 

Similar to Google Cloud Messaging (20)

Gcm presentation
Gcm presentationGcm presentation
Gcm presentation
 
Urban Airship and Android Integration for Push Notification and In-App Notifi...
Urban Airship and Android Integration for Push Notification and In-App Notifi...Urban Airship and Android Integration for Push Notification and In-App Notifi...
Urban Airship and Android Integration for Push Notification and In-App Notifi...
 
Urban Airship & Android Application Integration Document
Urban Airship & Android Application Integration DocumentUrban Airship & Android Application Integration Document
Urban Airship & Android Application Integration Document
 
GCM Android
GCM AndroidGCM Android
GCM Android
 
Android tutorial
Android tutorialAndroid tutorial
Android tutorial
 
Creation of simple application using - step by step
Creation of simple application using - step by stepCreation of simple application using - step by step
Creation of simple application using - step by step
 
Android app development guide for freshers by ace web academy
Android app development guide for freshers  by ace web academyAndroid app development guide for freshers  by ace web academy
Android app development guide for freshers by ace web academy
 
android level 3
android level 3android level 3
android level 3
 
Android Kotlin Weather App Project.pdf
Android Kotlin Weather App Project.pdfAndroid Kotlin Weather App Project.pdf
Android Kotlin Weather App Project.pdf
 
Hello android example.
Hello android example.Hello android example.
Hello android example.
 
Creating the first app with android studio
Creating the first app with android studioCreating the first app with android studio
Creating the first app with android studio
 
Android Cloud to Device Messaging Framework at GTUG Stockholm
Android Cloud to Device Messaging Framework at GTUG StockholmAndroid Cloud to Device Messaging Framework at GTUG Stockholm
Android Cloud to Device Messaging Framework at GTUG Stockholm
 
Android how to hellowidget
Android how to hellowidgetAndroid how to hellowidget
Android how to hellowidget
 
Android howto hellowidget
Android howto hellowidgetAndroid howto hellowidget
Android howto hellowidget
 
Google Android
Google AndroidGoogle Android
Google Android
 
Hacking the Codename One Source Code - Part IV - Transcript.pdf
Hacking the Codename One Source Code - Part IV - Transcript.pdfHacking the Codename One Source Code - Part IV - Transcript.pdf
Hacking the Codename One Source Code - Part IV - Transcript.pdf
 
Android N Highligts
Android N HighligtsAndroid N Highligts
Android N Highligts
 
Android development module
Android development moduleAndroid development module
Android development module
 
Introduction to Android and Java.pptx
Introduction to Android and Java.pptxIntroduction to Android and Java.pptx
Introduction to Android and Java.pptx
 
12. Android Basic Google Map
12. Android Basic Google Map12. Android Basic Google Map
12. Android Basic Google Map
 

Google Cloud Messaging

  • 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>.
  • 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. 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" ;