---
title: "GCM(4)GoogleCloudMessaging"
author: "sillycat"
date: "2014-01-04T04:04:04Z"
tags: []
categories: ["移动开发"]
slug: "gcm-4-googlecloudmessaging"
draft: false
lastmod: "2014-01-04T04:04:04Z"
id: 1998911
url: "https://sillycat.iteye.com/blog/1998911"
---

GCM(4)GoogleCloudMessaging

**1. Reading the documents**  
The registration ID identifies the device and application, and also determines which 3rd-party application servers are allowed to send messages to this application instance.

A sender ID is a project number you acquire from the API console. A sender ID is used to identify a 3rd-party application server that is permitted to send messages.

String SENDER\_ID = “”;  
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(context);  
String registrationId = gcm.register(SENDER\_ID);

&lt;uses-permissionandroid:name="com.google.android.c2dm.permission.RECEIVE"/&gt;

&lt;receiverandroid:name=".MyReceiver"android:exported="true"  
android:permission="com.google.android.c2dm.permission.SEND"]]&gt;  
&lt;intent-filter&gt;  
&lt;actionandroid:name="com.google.android.c2dm.intent.RECEIVE"/&gt;  
&lt;categoryandroid:name="YOUR\_PACKAGE\_NAME"/&gt;  
&lt;/intent-filter&gt;  
&lt;/receiver&gt;

When we receive the message from server  
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);  
String messageType = gcm.getMessageType(intent);

if(GoogleCloudMessaging.MESSAGE\_TYPE\_SEND\_ERROR.equals(messageType)){  
     //It’s an error.  
}else if(GoogleCloudMessaging.MESSAGE\_TYPE\_DELETED.equals(messageType)){  
}else if(GoogleCloudMessaging.MESSAGE\_TYPE\_MESSAGE.equals(messageType)){  
     //regular GCM message.  
}

**2. Start from My Old GCM Example**  
Make sure I have this directory with google play services installed.  
/opt/android-sdk/extras/google/google\_play\_services

Add Support lib to eclipse  
Select the project —&gt; Android Tool ——&gt; Add Support Library

Add Support lib to android studio  
Add this lib to the maven pom.xml file  
&lt;dependency&gt;&lt;groupId&gt;com.google.android&lt;/groupId&gt;&lt;artifactId&gt;support-v4&lt;/artifactId&gt;&lt;version&gt;r7&lt;/version&gt;  
&lt;/dependency&gt;

Give up to using maven, I should know about gradle.

**3. Install Gradle and Setup**  
Download and get the latest version  
http://downloads.gradle.org/distributions/gradle-1.10-all.zip

Unzip and copy to working directory  
&gt;pwd  
/Users/carl/tool/gradle-1.10

Soft link it to my /opt directory  
&gt;sudo ln -s /Users/carl/tool/gradle-1.10 /opt/gradle-1.10  
&gt;sudo ln -s /opt/gradle-1.10 /opt/gradle

&gt;sudo vi ~/.profile  
export GRADLE\_HOME=/opt/gradle  
export PATH=${GRADLE\_HOME}/bin:$PATH

&gt;. ~/.profile

Verify the Installation  
&gt;gradle -v  
\------------------------------------------------------------ Gradle 1.10  
\------------------------------------------------------------

Some error there as follow, when I run command gradle build under my android project:  
\* What went wrong: A problem occurred evaluating root project 'EasyRestClientAndroid'.  
&gt; Could not create plugin of type 'AppPlugin'.

I switch my gradle to 1.9

&gt;gradle tasks  
&gt;gradle tasks —all

Error Message  
Execution failed for task ':packageRelease'. &gt; class org.bouncycastle.asn1.ASN1Primitive overrides final method equals

Solution:  
Get rid of bcprov-jdk16-1.46.jar

**4. Go on with the Example**

The latest example is in project EasyAndroidGCM.

**Tips for Gradle**  
**Install the app on the device**  
&gt;gradle installDebug

**Tips for Android Studio**  
**Show Line Number**  
Android Studio ——&gt; Preferences——&gt; Editor ——&gt; Appearance ———&gt; Show line numbers

**Keymap**  
Android Studio ——&gt; Preferences ——&gt; Keymap ——&gt; keymaps ——&gt; Eclipse(Mac OS X)  
Android Studio —&gt;     Preferences ———&gt; Editor —&gt; Auto Import ——&gt; Add unambiguous imports on the fly

**References:**  
http://developer.android.com/reference/com/google/android/gms/gcm/GoogleCloudMessaging.html  
http://developer.android.com/reference/com/google/android/gcm/GCMBaseIntentService.html#getSenderIds(android.content.Context

GCM 1~3  
http://sillycat.iteye.com/blog/1769191  
http://sillycat.iteye.com/blog/1769196  
http://sillycat.iteye.com/blog/1838432

Latest Sample  
https://code.google.com/p/gcm/source/browse/#git%2Fgcm-client%2Fsrc%2Fcom%2Fgoogle%2Fandroid%2Fgcm%2Fdemo%2Fapp%253Fstate%253Dclosed

http://www.appsrox.com/android/tutorials/instachat/3/  
http://tausiq.wordpress.com/2013/09/24/android-push-notification-cloud-to-device-messaging/

gradle  
http://sillycat.iteye.com/blog/1074642  
http://tools.android.com/tech-docs/new-build-system/user-guide  
http://spring.io/guides/gs/gradle-android/  
https://github.com/spring-guides/gs-gradle-android

sample  
https://plus.google.com/+AndroidDevelopers/posts/4Yhpn6p9icf