GCM(1)Get Start

本文详细介绍了如何使用Google Cloud Messaging (GCM) 进行消息推送。从创建Google API项目开始,逐步指导开发者完成API密钥获取、安装辅助库、编写Android应用和服务端应用等步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值