gcm-android
gcm-android copied to clipboard
This is a simple example how to use GCM in android Application to receive and send push (creating your own server request).
Android GCM - Google Cloud Message
This is a simple example of how to use GCM in Android applications. My motivation is help users to put this feature in your applications, because in some topics, I saw problems in how to implement it, but in general is very easy.
To create this repository, I read official GCM Page and Android API.
I will suppose that you have your bundle id application (in AndroidManifest.xml), in my example I will use:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.brunogabriel.nativegcm">
Register your account
First it is necessary to create and enable GCM by making a project, steps:
-
With your package android in hands:
- Enter in [Google Developers Console] (http://console.developers.google.com) and create your own application, in my case named Native GCM.
-
Do not forgot to save your
project number
, we will use it in Android Application directly. -
Create your credentials:
- When I made it, I did not saved an IP Address, only empty this field to test.
- So now, write your credentials key.
Configuration: Android application
In Android project:
-
First, please, read as soon as possible all comments (small quantity) in
.java
files, because some contains tips, e.g. in functionexecuteBusinessRule
, its possible for users save unique device id in databases (like chat application, to send notification or start a chat). -
In gradle file, has an important dependency:
com.google.android.gms:play-services:8.4.0
, to enable receive data. -
In
string.xml
, don't forgot to changegcm_default_sender_id
value to yourproject number
(step 2 in register account). -
Please, see implementation in
AndroidManifest.xml
, because there are features necessary to application work correctly. -
In
MainActivity
, we'll register your unique device id, first this application will validate if play services is enabled by callingisEnablePlayService
, if there is no error, start 'RegistrationIntentService'. -
I advise all people to debug this app and collect first unique device id, to do, put a break point in
RegistrationIntentService
methodonHandleIntent
orexecuteBusinessRule
to get this value, don't forget to write this in your notes to test server after. Do not forget, every time you install the application this id changes.
Some explanations
In GCMListenerService
we've important features:
-
onMessageReceived
- Contains a bundle received, in our case, JSON Data (Bundle). -
sendNotification
- Method that calls a device local notification, push alert.
Servers
Now, you need to encapsulate your message in a json request, in sequence, send to Google Server. Google has a responsibility to send data (push).
If you have no idea how to implement a server, see this section.