{"id":2214261,"date":"2022-08-12T08:28:01","date_gmt":"2022-08-12T06:28:01","guid":{"rendered":"http:\/\/nhub.news\/?p=2214261"},"modified":"2022-08-12T15:03:16","modified_gmt":"2022-08-12T13:03:16","slug":"google-cloud-messaging-with-android","status":"publish","type":"post","link":"http:\/\/nhub.news\/fr\/2022\/08\/google-cloud-messaging-with-android\/","title":{"rendered":"Google Cloud Messaging with Android"},"content":{"rendered":"<p style=\"text-align: justify;\"><b>Array<\/b><br \/>\nJoin the DZone community and get the full member experience.<br \/>You have probably heard a lot of talk about the wonderful things the cloud can do for you, and you are probably curious about how those services may come into play in your daily life. If this sounds like you, then you need to know that cloud services are playing an increasingly important role in our lives, and we need to look at how they can change how we message one another.\u00a0<br \/>Many people are looking at Android cloud messaging as the next leap forward into a future where it is possible to reach out to the people we care about and save those messages directly in the cloud. Never miss the opportunity to communicate with someone who truly matters to you, and start using cloud storage to back up your messages. It is as simple as that!\u00a0<br \/>You might have heard of c2dm (cloud-to-device messaging), which basically allowed third-party applications to send (push) lightweight messages to their android applications. Well, c2dm as such is now deprecated and replaced with its successor up the evolutionary ladder: GCM, or google cloud messaging.<br \/>GCM is a (free) service that allows developers to push two types of messages from their application servers to any number of android devices registered with the service:<br \/>collapsible,\u00a0\u00ab\u00a0send-to-sync\u00a0\u00bb messages<br \/>non-collapsible messages with a\u00a0payload\u00a0up to 4k in size<br \/>\u00ab\u00a0collapsible\u00a0\u00bb means that the most recent message overwrites the previous one. A \u00ab\u00a0send-to-sync\u00a0\u00bb message is used to notify a mobile application to sync its data with the server. In case the device comes online after being offline for a while, the client will only get the most recent server message.<br \/>If you want to add push notifications to your android applications, the getting started guide\u00a0will walk you through the setup process step by step, even supplying you with a two-part demo application\u00a0(client + server) that you can just install and play around with. The setup process will provide you with the two most essential pieces of information needed to run GCM:<br \/>An API Key is needed by your server to send GCM push notifications<br \/>Everything is summarized in the following screen you get after using the google API console:<br \/>The quickest way to write both server and client code is to install the sample demo application and tweak it to your needs. In particular, you might want to at least do any of the following:<br \/>Change the demo&rsquo;s in-memory datastore into a real persistent one.<br \/>Change the type and\/or the content of push messages.<br \/>We&rsquo;ll do the last option as an example. Picking up where the demo ends, here&rsquo;s a quick way to set up push preferences\u00a0and integrate them into your existing android application clients.<br \/>in your android project-resources ( res\/xml) directory, create a preference.xml file such as this one:<br \/>and the corresponding activity:<br \/>the above will provide the following ui:<br \/>The \u00ab\u00a0enable server push\u00a0\u00bb checkbox is where your android application user decides to register for your push messages. Then, it&rsquo;s only a matter of using that preferences class in your main activity and doing the required input processing. the following skeleton class only shows your own code add-ons to the pre-existing sample application:<br \/>The above is pretty much self-explanatory. Now GCM push notifications have been integrated into your existing application. If you are registered, you get a system notification message at each server push, even when your application is not running. Opening up the message will automatically open your application:<br \/>GCM is pretty easy to set up since most of the plumbing work is done for you. a side note: if you like to isolate the push functionality in its own sub-package, be aware that the GCM service gcmintentservice, provided by the sample application and responsible for handling GCM messages, needs to be in your main package (as indicated in the-set up documentation)\u2014otherwise GCM won&rsquo;t work.<br \/>When communicating with the sample server via an HTTP post, the sample client does a number of automatic retries using\u00a0exponential back-off, meaning that the waiting period before a retry in case of failure is each time twice the amount of the preceding wait period, up to the maximum number of retries (5 at the time of this writing).\u00a0<br \/>You might want to change that if it doesn&rsquo;t suit you. It may not matter that much, though, since those retries are done in a separate thread (using\u00a0asynctask) from the main UI thread, which therefore minimizes the effects on your mobile application&rsquo;s pre-existing flow of operations.<br \/>Opinions expressed by DZone contributors are their own.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Array Join the DZone community and get the full member experience.You have probably heard a lot of talk about the wonderful things the cloud can do for you, and you are probably curious about how those services may come into play in your daily life. If this sounds like you, then you need to know [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":2214260,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[93],"tags":[],"_links":{"self":[{"href":"http:\/\/nhub.news\/fr\/wp-json\/wp\/v2\/posts\/2214261"}],"collection":[{"href":"http:\/\/nhub.news\/fr\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/nhub.news\/fr\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/nhub.news\/fr\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/nhub.news\/fr\/wp-json\/wp\/v2\/comments?post=2214261"}],"version-history":[{"count":0,"href":"http:\/\/nhub.news\/fr\/wp-json\/wp\/v2\/posts\/2214261\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/nhub.news\/fr\/wp-json\/wp\/v2\/media\/2214260"}],"wp:attachment":[{"href":"http:\/\/nhub.news\/fr\/wp-json\/wp\/v2\/media?parent=2214261"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/nhub.news\/fr\/wp-json\/wp\/v2\/categories?post=2214261"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/nhub.news\/fr\/wp-json\/wp\/v2\/tags?post=2214261"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}