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

在Python中,SM4是一种对称密钥加密算法,而GCM(Galois/Counter Mode)是一种常用的块密码模式,用于数据加密和认证。如果你想要在Python中实现SM4-GCM模式,通常会依赖于像`cryptography`这样的库,它提供了高级的加密支持。 首先,你需要安装`cryptography`库,如果尚未安装,可以使用pip命令: ```bash pip install cryptography ``` 然后你可以使用该库的`ciphers`模块中的`AES-GCM`类结合SM4算法,虽然直接SM4-GCM不是标准提供的,但是可以通过包装实现。这里是一个简化的示例: ```python from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes from cryptography.hazmat.backends import default_backend def sm4_gcm_encrypt(plaintext, key, associated_data=None): backend = default_backend() sm4_cipher = Cipher(algorithms.SM4(key), modes.GCM(associated_data or b''), backend) encryptor = sm4_cipher.encryptor() ciphertext = encryptor.update(plaintext) + encryptor.finalize() tag = encryptor.tag return ciphertext, tag def sm4_gcm_decrypt(ciphertext, key, tag, associated_data=None): backend = default_backend() sm4_cipher = Cipher(algorithms.SM4(key), modes.GCM(associated_data or b'', tag), backend) decryptor = sm4_cipher.decryptor() plaintext = decryptor.update(ciphertext) + decryptor.finalize() return plaintext # 使用示例 key = b'secret_sm4_key' plaintext = b'this is a secret message' ciphertext, tag = sm4_gcm_encrypt(plaintext, key) recovered_plaintext = sm4_gcm_decrypt(ciphertext, key, tag) assert plaintext == recovered_plaintext, "Decryption failed"
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值