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

GCM(4)GoogleCloudMessaging

 
阅读更多

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

分享到:
评论

相关推荐

    Go-gcm-GoogleCloudMessaging的Go库

    4. **延迟与重试机制**:网络不稳定可能导致消息无法立即送达。Go-gcm库内置了重试机制,对于未成功发送的消息,会根据预设策略进行延迟重试,提高消息传递的成功率。 5. **错误处理**:GCM服务可能会返回各种错误...

    GCM测试实例

    4. **实现GCM服务**:创建一个`GcmListenerService`子类,重写`onMessageReceived()`方法来处理接收到的推送消息。在这里,你可以根据消息内容执行相应的操作,如显示通知或更新UI。 5. **服务器端集成**:在服务器...

    Android消息推送之GCM客户端完整实现

    4. **初始化GCM**:在应用启动时,通过GCMRegistrar类(旧版)或GoogleCloudMessaging类(新版)进行初始化,并注册设备。注册过程中,系统会生成一个设备ID,这个ID需要保存在本地,以便服务器识别并推送消息。 5....

    GCM-Test:GCM-测试

    - GCM消息大小有限制,最大为4KB。 - GCM不保证消息的可靠交付,可能存在延迟或丢失。 - 需要处理用户权限和设备网络状态的变化。 8. **替代技术:FCM(Firebase Cloud Messaging)**: 由于GCM已不再维护,...

    java-post-gcm.7z

    4. **处理错误**:GCM可能会遇到各种错误,如网络问题、无效的registration ID等。因此,需要适当地处理`onError()`回调,提供重试机制或者提示用户重新启动应用。 5. **权限设置**:在AndroidManifest.xml文件中,...

    CalcGcm:GCM计算器的应用程序设置示例

    在Android开发中,Google Cloud Messaging (GCM) 是一种...4. **权限**:确保在AndroidManifest.xml文件中添加了必要的权限,如`&lt;uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" /&gt;`和`...

    AndroidC2DMDemo--客户端代码开发

    GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this); String messageType = gcm.getMessageType(intent); if (!extras.isEmpty()) { // has effect of unparcelling Bundle if ...

    GCM-demo:我用于学习 GCM 的 GCM Quickstart 应用程序版本

    GCM演示 来自 google-services ...这是谷歌为我们测试 GCM 所做的应用程序,我希望添加评论并进行小的修改,从而学习如何使用 GoogleCloudMessaging API 和 Android Studio with Gradle.. 您可以从获取设置详细信息

    GCM_Android:Android中的Google云消息传递功能的工作演示代码

    - 初始化GCM服务,通常在Application或MainActivity的onCreate方法中进行,通过`GoogleCloudMessaging.register()`方法注册设备。 4. **服务端集成**: - 服务端需要使用API密钥和设备注册ID来构造请求,通过...

    android-gcm-example:通过 CloudMine 示例的 Google Cloud Messaging

    String token = instanceID.getToken("SENDER_ID", GoogleCloudMessaging.INSTANCE_ID_SCOPE, null); sessionManager.setSessionVariable("gcm_token", token); // ... } } ``` 请注意替换`YOUR_APP_KEY`、`...

    Android编程实现google消息通知功能示例

    GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this); String messageType = gcm.getMessageType(intent); // 根据messageType处理不同类型的事件 ... } } ``` `onHandleIntent()`方法是处理...

Global site tag (gtag.js) - Google Analytics