`
sillycat
  • 浏览: 2578794 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

GCM(1)Get Start

 
阅读更多
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


分享到:
评论

相关推荐

    著名的TomCrypt加密解密函数库

    Modes come with a start, encrypt/decrypt and set/get IV interfaces. Mode supported. ECB CBC OFB CFB CTR 单向 Hash 函数 支持的hash方式. MD2 MD4 MD5 (较为常用) SHA-1 SHA-224/256/384/512...

    Asynchronous Android Programming(PACKT,2ed,2016)

    To start with, we will discuss the details of the Android process model and the Java low-level concurrent framework, delivered by the Android SDK. Next, we will discuss the creation of IntentServices...

    Asynchronous Android Programming

    To start with, we will discuss the details of the Android process model and the Java low-level concurrent framework, delivered by the Android SDK. Next, we will discuss the creation of IntentServices...

    android后台服务自启动

    2. GCM/FCM的唤醒机制:Google提供的云消息推送(GCM/FCM)可以在设备休眠状态下唤醒应用,用于执行后台任务。 3. JobScheduler:Android API 14以上可以使用JobScheduler来安排后台任务,系统会在满足条件时自动...

    libtomcrypt-1.17

    Modes come with a start, encrypt/decrypt and set/get IV interfaces. Mode supported. ECB CBC OFB CFB CTR IEEE LRW mode F8 Chaining Mode One-Way Hash Functions Hashes come with init, process, ...

    nginx配置ssl证书实现https访问的示例

    如果需要重启Nginx服务,可以使用`service nginx stop`、`service nginx start`或`service nginx restart`命令。 最后,确保服务器的安全组设置允许80和443端口的入站流量。在阿里云ECS管理后台的相应安全组规则中...

Global site tag (gtag.js) - Google Analytics