Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

GCM Android

3,242 views

Published on

  • Be the first to comment

GCM Android

  1. 1. Google Cloud Messaging - AndroidSwapnal Agrawal
  2. 2. • How to keep the data on your device app- Updated & FreshCan use Notifications ?????How to send notificationmsgdata
  3. 3. How to send notification• Polling App checks the server periodically Easy to implement Good for frequent content changeBut……It reduces bandwidth and battery resourcesIf there isn’t any new data, we’re still checking
  4. 4. How to send notification• Pushing App waits for information to get sent to it. Constant Connection Less Battery ConsumptionBut…A bit complex to implement
  5. 5. What is GCM? Google Cloud Messaging for Android (GCM) is aservice that allows you to send data from your server toyour users Android-powered device. The GCM service handles all aspects of queueing ofmessages and delivery to the target Android applicationrunning on the target device GCM is completely free no matter how big yourmessaging needs are, and there are no quotas.
  6. 6. How efficient is it? Easy to use It allows 3rd-party application servers to send messagesto their Android applications. An Android application on an Android device doesntneed to be running to receive messages. Limitless Quotas Battery Efficiency Rich set of new API’s
  7. 7. Limitations of GCMThe device must be running 2.2 or greater.The device must have the Marketplaceinstalled.Your app needs additional permissions inthe manifest.
  8. 8. GCM Building Blocks• Components :- Mobile Device 3rd party App Server GCM Server• Credentials :- SenderId ApplicationId RegistrationId Google User Account Sender Auth Token
  9. 9. GCM RegisterationAndroid Device5. Intent BroadcastServiceAppViewApplicationServerGCMServiceAppView1. Register forNotification2. SendRegistration ID3. SendNotification4. SendMessage
  10. 10. GCM LifeCycle• Enabling GCM.An Android application running on a mobile deviceregisters to receive messages.• Sending a message.A 3rd-party application server sends messages tothe device.• Receiving a message.An Android application receives a message from aGCM server.
  11. 11. Enabling GCM You should have one google account login If you already have existing projects, the first page you see will bethe Dashboard page. From there you can create a new project by openingthe project drop-down menu (upper left corner) and choosingOtherprojects > Create. Click Create project. Your browser URL will change to something like:https://code.google.com/apis/console/#project:219845360754Enable the GCM service: In the main Google APIs Console page, select Services. Turn the Google Cloud Messaging toggle to ON. In the Terms of Service page, accept the terms.
  12. 12. Enabling GCMObtain API key as per the steps given athttp://developer.android.com/google/gcm/gs.html
  13. 13. Import com.google.android.gcm.GCMRegistrar;@Overrideprotected void onCreate(BundlesavedInstanceState){GCMRegistrar.register(this, “219845360754”);}GCM RegistrationGCMGCMFrameworkProject Id
  14. 14. GCM – Android Manifest<manifest package="com.example.gcm" ...><uses-sdk android:minSdkVersion="8" android:targetSdkVersion="16"/><uses-permission android:name="android.permission.INTERNET" /><uses-permission android:name="android.permission.GET_ACCOUNTS" /><uses-permission android:name="android.permission.WAKE_LOCK" /><uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" /><permission android:name="com.example.gcm.permission.C2D_MESSAGE"android:protectionLevel="signature" /><uses-permission android:name="com.example.gcm.permission.C2D_MESSAGE" /><application ...><receiverandroid:name=".MyBroadcastReceiver"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="com.example.gcm" /></intent-filter></receiver><service android:name=".GCMIntentService" /></application></manifest>
  15. 15. GCM – Sending MessageServer Side Application :-import com.google.android.gcm.server.Message;import com.google.android.gcm.server.Result;import com.google.android.gcm.server.Sender;[..]public Result sendMessage(final String device, final String message) throwsIOException{Sender sender = new Sender(getApiKey());Message gcmMessage = new Message.Builder().addData("payload",message).collapseKey("0").build(); return sender.send(gcmMessage, device, 5);}
  16. 16. GCM – Receive MessageGCMGCMFrameworkRegId {Intent}• System receives the incoming messageand extracts the raw key/value pairs• System passes key/value pairs to targetedAndroid Application in acom.google.android.c2dm.intent.RECEIVEIntent as a set of extras• Android application extracts the raw datafrom the Intent by key and processes thedata
  17. 17. GCM – Receive Messageimport com.google.android.gcm.server.Message;import com.google.android.gcm.server.Result;import com.google.android.gcm.server.Sender;[..]public Result sendMessage(final String device, final String message) throwsIOException{Sender sender = new Sender(getApiKey());Message gcmMessage = new Message.Builder().addData("payload",message).collapseKey("0").build(); return sender.send(gcmMessage, device, 5);}
  18. 18. References• https://docs.google.com/presentation/d/1qQPT653uUrKFXYUxPE7gy-5Kii0UupxS6K8TvAwfnXI/edit#slide=id.p74• http://developer.android.com/google/gcm/gs.html• http://developer.android.com/google/gcm/gcm.html• https://bremen-gtug.googlecode.com/svn/presentations/2012_12_03/Google%20Cloud%20Messaging/template.html#3• http://www.fppt.com (PPT templates)

×