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
- 浏览: 2551013 次
- 性别:
- 来自: 成都
文章分类
最新评论
-
nation:
你好,在部署Mesos+Spark的运行环境时,出现一个现象, ...
Spark(4)Deal with Mesos -
sillycat:
AMAZON Relatedhttps://www.godad ...
AMAZON API Gateway(2)Client Side SSL with NGINX -
sillycat:
sudo usermod -aG docker ec2-use ...
Docker and VirtualBox(1)Set up Shared Disk for Virtual Box -
sillycat:
Every Half an Hour30 * * * * /u ...
Build Home NAS(3)Data Redundancy -
sillycat:
3 List the Cron Job I Have>c ...
Build Home NAS(3)Data Redundancy
发表评论
-
ionic UI(4)ionic2 framework - basic and components and native
2016-03-24 02:33 1261ionic UI(4)ionic2 framework - b ... -
ionic UI(3)TypeScript - handbook
2016-03-22 23:21 634ionic UI(3)TypeScript - handboo ... -
ionic UI(2)ionic2 framework - TypeScript - tutorial
2016-03-22 06:52 1656ionic UI(2)ionic2 framework - T ... -
Parse and Heroku Service(3)Parse Server and Parse Dashboard
2016-03-22 06:30 967Parse and Heroku Service(3)Pars ... -
Parse and Heroku Service(2)Mail Templates and Push Notification
2016-03-22 02:45 583Parse and Heroku Service(2)Mail ... -
ionic UI(1)Introduction
2016-03-19 03:18 722ionic UI(1)Introduction 1 Inst ... -
Parse and Heroku Service(1)Heroku Installation and Play
2016-03-19 00:13 824Parse and Heroic Service(1)Hero ... -
Hybrid(5)Customize Meteor Directly Google Login
2015-09-01 02:33 910Hybrid(5)Customize Meteor Direc ... -
Hybrid(4)Favorite Places - Google Login
2015-09-01 02:02 1341Hybrid(4)Favorite Places - Goog ... -
Hybrid(3)More Meteor Example - Social
2015-08-11 05:04 756Hybrid(3)More Meteor Example - ... -
Hybrid(2)meteor Running Android and iOS
2015-07-28 23:59 1049Hybrid(2)meteor Running Android ... -
Create the Google Play Account
2015-07-18 06:42 1101Create the Google Play Account ... -
Secure REST API and Mobile(1)Document Read and Understand OAUTH2
2015-07-14 00:36 763Secure REST API and Mobile(1)Do ... -
Screen Size and Web Design
2015-07-11 01:11 725Screen Size and Web Design iPh ... -
Hybrid(1)ionic Cordova meteor
2015-06-25 05:49 471Hybrid(1)ionic Cordova meteor ... -
Android Fire Project(1)Recall Env and Knowledge
2015-02-11 12:28 686Android Fire Project(1)Recall ... -
Android Content Framework(1)Concept
2014-06-14 13:54 1079Android Content Framework(1)Con ... -
Feel Android Studio(1)Install and Update Android Studio
2014-04-11 03:12 2028Feel Android Studio(1)Install a ... -
IOS7 App Development Essentials(2)iBeacon
2014-03-05 05:55 890IOS7 App Development Essentials ... -
IOS7 App Development Essentials(1) Persistent Store
2014-03-05 05:54 1326IOS7 App Development Essentials ...
相关推荐
4. **延迟与重试机制**:网络不稳定可能导致消息无法立即送达。Go-gcm库内置了重试机制,对于未成功发送的消息,会根据预设策略进行延迟重试,提高消息传递的成功率。 5. **错误处理**:GCM服务可能会返回各种错误...
4. **实现GCM服务**:创建一个`GcmListenerService`子类,重写`onMessageReceived()`方法来处理接收到的推送消息。在这里,你可以根据消息内容执行相应的操作,如显示通知或更新UI。 5. **服务器端集成**:在服务器...
4. **初始化GCM**:在应用启动时,通过GCMRegistrar类(旧版)或GoogleCloudMessaging类(新版)进行初始化,并注册设备。注册过程中,系统会生成一个设备ID,这个ID需要保存在本地,以便服务器识别并推送消息。 5....
- GCM消息大小有限制,最大为4KB。 - GCM不保证消息的可靠交付,可能存在延迟或丢失。 - 需要处理用户权限和设备网络状态的变化。 8. **替代技术:FCM(Firebase Cloud Messaging)**: 由于GCM已不再维护,...
4. **处理错误**:GCM可能会遇到各种错误,如网络问题、无效的registration ID等。因此,需要适当地处理`onError()`回调,提供重试机制或者提示用户重新启动应用。 5. **权限设置**:在AndroidManifest.xml文件中,...
在Android开发中,Google Cloud Messaging (GCM) 是一种...4. **权限**:确保在AndroidManifest.xml文件中添加了必要的权限,如`<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />`和`...
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this); String messageType = gcm.getMessageType(intent); if (!extras.isEmpty()) { // has effect of unparcelling Bundle if ...
GCM演示 来自 google-services ...这是谷歌为我们测试 GCM 所做的应用程序,我希望添加评论并进行小的修改,从而学习如何使用 GoogleCloudMessaging API 和 Android Studio with Gradle.. 您可以从获取设置详细信息
- 初始化GCM服务,通常在Application或MainActivity的onCreate方法中进行,通过`GoogleCloudMessaging.register()`方法注册设备。 4. **服务端集成**: - 服务端需要使用API密钥和设备注册ID来构造请求,通过...
String token = instanceID.getToken("SENDER_ID", GoogleCloudMessaging.INSTANCE_ID_SCOPE, null); sessionManager.setSessionVariable("gcm_token", token); // ... } } ``` 请注意替换`YOUR_APP_KEY`、`...
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this); String messageType = gcm.getMessageType(intent); // 根据messageType处理不同类型的事件 ... } } ``` `onHandleIntent()`方法是处理...