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);
<uses-permissionandroid:name="com.google.android.c2dm.permission.RECEIVE"/>
<receiverandroid:name=".MyReceiver"android:exported="true"
android:permission="com.google.android.c2dm.permission.SEND"]]>
<intent-filter>
<actionandroid:name="com.google.android.c2dm.intent.RECEIVE"/>
<categoryandroid:name="YOUR_PACKAGE_NAME"/>
</intent-filter>
</receiver>
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 —> Android Tool ——> Add Support Library
Add Support lib to android studio
Add this lib to the maven pom.xml file
<dependency><groupId>com.google.android</groupId><artifactId>support-v4</artifactId><version>r7</version>
</dependency>
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
>pwd
/Users/carl/tool/gradle-1.10
Soft link it to my /opt directory
>sudo ln -s /Users/carl/tool/gradle-1.10 /opt/gradle-1.10
>sudo ln -s /opt/gradle-1.10 /opt/gradle
>sudo vi ~/.profile
export GRADLE_HOME=/opt/gradle
export PATH=${GRADLE_HOME}/bin:$PATH
>. ~/.profile
Verify the Installation
>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'.
> Could not create plugin of type 'AppPlugin'.
I switch my gradle to 1.9
>gradle tasks
>gradle tasks —all
Error Message
Execution failed for task ':packageRelease'. > 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
>gradle installDebug
Tips for Android Studio
Show Line Number
Android Studio ——> Preferences——> Editor ——> Appearance ———> Show line numbers
Keymap
Android Studio ——> Preferences ——> Keymap ——> keymaps ——> Eclipse(Mac OS X)
Android Studio —> Preferences ———> Editor —> Auto Import ——> 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
GCM(4)GoogleCloudMessaging
最新推荐文章于 2022-08-08 14:49:32 发布