GCM(1)Get Start
1. Getting Started
Follow the document to create Google APIs Console project
The URL are as follow:
https://code.google.com/apis/console/
I click create project to create an API project and then rename it to easymessagepush.
Take a note of my project number ***
Enabling the GCM Service
Click on the Service tab menu and turn on the 'Google Cloud Messaging for Android'
Obtaining an API Key
Select the tab menu, 'API Access'
I just select the 3 one by one
Create new Server key
Create new Browser key
Create new Android key
I did not type in something, I want make them allowed for any app, any IP and any referer right now, maybe I will change that in the future.
Install the Helper Libraries
From the SDK Manager on eclipse, find the menu
Extras ---> Google Cloud Messaging for Android Library
Install this feature, it will create a GCM directory under my SDK ROOT/extras/google/
For me, that is /opt/android-sdk/extras/google
Everything will be in this directory /opt/android-sdk/extras/google/gcm
Writing the Android Application
Step1: Copy the gcm.jar file into our application classpath
find the gcm.jar from here gcm-client/dist
My project is managed by MAVEN, so I decide to add this in pom.xml
<dependency>
<groupId>com.google.android</groupId>
<artifactId>gcm</artifactId>
<version>1.0.2</version>
</dependency>
Step2: Make the following changes in the application's Android manifest
1. GCM requires Android 2.2 or later, make sure we have this kind of line in AndroidManifest.xml
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15"/>
2. Declare and use a custom permission so only this application can receive GCM message:
<permission android:name="com.sillycat.easyrestclientandroid.permission.C2D_MESSAGE"
android:protectionLevel="signature"/>
<uses-permission
android:name="android.permission.INTERNET"/>
<uses-permission
android:name="com.sillycat.easyrestclientandroid.permission.C2D_MESSAGE"/>
The package name com.sillycat.easyrestclientandroid should be the same as
<manifestxmlns:android="http://schemas.android.com/apk/res/android"
package="com.sillycat.easyrestclientandroid" …snip…
3. Add the following permissions
<!-- App receives GCM messages. -->
<uses-permissionandroid:name="com.google.android.c2dm.permission.RECEIVE"/>
<!-- GCM connects to Google Services. -->
<uses-permissionandroid:name="android.permission.INTERNET"/>
<!-- GCM requires a Google account. -->
<uses-permissionandroid:name="android.permission.GET_ACCOUNTS"/>
<!-- Keeps the processor from sleeping when a message is received. -->
<uses-permissionandroid:name="android.permission.WAKE_LOCK"/>
4. Add the following broadcast receiver
<application>
…snip...
<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="com.sillycat.easyrestclientandroid" />
</intent-filter>
</receiver>
…snip…
</application>
This broadcast receiver is responsible for handling the 2 intents that can be sent by GCM.
com.google.android.c2dm.intent.RECEIVE
com.google.android.c2dm.intent.REGISTRATION
These should be defined in the manifest(rather than programmatically) so that these intents can be received even if the application is not running.
5. Add the following intent service
<application>
…snip..
<service android:name=".GCMIntentService" />
…snip...
</application>
This intent service will be called by the GCMBroadcastReceiver.
Step 3: Write the GCMIntentService class
The detail of the project is in the sample EasyRestClientAndroid project.
Writing the Server-side Application
The detail is in project 4mymessage project.
References:
https://developer.android.com/google/gcm/gcm.html
https://developer.android.com/google/gcm/gs.html
GCM(1)Get Start
猜你喜欢
转载自sillycat.iteye.com/blog/1769191
今日推荐
周排行