GCM结构概述-GCM Architectural Overview


快速预览*
一个状态通知允许应用程序通知用户一个事件但不扰乱他们的当前活动
你可以把一个intent和通知绑定在一起,这样当用户点击通知选项时系统就可以进行初始化
本文内容*
基础-The Basics|基础-The Basics
响应通知-Responding to Notifications|响应通知-Responding to Notifications
管理通知-Managing your Notifications|管理通知-Managing your Notifications
创建通知-Creating a Notification|创建通知-Creating a Notification
更新通知-Updating the notification|更新通知-Updating the notification
添加声音-Adding a sound|添加声音-Adding a sound
添加振动-Adding vibration|添加振动-Adding vibration
添加闪灯-Adding flashing lights|添加闪灯-Adding flashing lights
更多特性-More features|更多特性-More features
自定义通知的布局-Creating a Custom Notification Layout|自定义通知的布局-Creating a Custom Notification Layout
谷歌云消息服务(GCM)是免费提供给用户的,它帮助你把服务端的数据推送到客户端。它可能是一个轻量级的消息,通知有新的数据上传的服务器(例如,朋友上传了一个电影),或者可能是一个包含了4KB的有效载荷数据的消息(所以即时消息应用可以直接消耗消息)。GCM服务处理排队的消息,并传递到目标的Android应用。想知道如何使用GCM,请看Getting Started
下面是谷歌的云消息服务的主要特点:
它允许第三方应用服务器发送邮件到安卓应用。
关于消息的交付和顺序,GCM没有作出保证。
Android设备上的应用并不需要一直运行,准备接收消息。当消息到达时,系统将通过Intent broadcast(意图广播)唤醒应用,只要应用程序设置适当的广播接收机和权限。
它不提供任何内置的用户界面或其他对消息数据的处理方法。 GCM的只是简单地把原始数据传递给Android应用程序,而程序会负责如何处理消息数据。例如,应用程序可能会发布通知,显示自定义的用户界面,或静默地同步数据。
它需要可以运行Android2.2或者更高版本而且要装有谷歌商店应用的设备,或者可以运行Android2.2 API的仿真器。然而,你并没有被限制通过谷歌商店来发布程序。
它使用现有的谷歌服务的连接。对于前3.0设备,需要用户在他们的移动设备上设置谷歌帐户。运行Android4.0.4或更高版本的设备是不需要谷歌账户的。
这章节概述了GCM是如何工作的。
此表汇总的GCM涉及的关键术语和概念。它分为这些类别:
组件* -在GCM中发挥作用的物理实体
凭证* -用于GCM不同阶段的ID和令牌,以确保各方已经通过验证和消息将被送往正确的地方。
{|style="border-spacing: 6px; margin: 0px 8px; width: 100%; border:1px solid #cedff2;"
|- style="width:35%; border:1px solid #cedff2; background:#DEE8F1; vertical-align:top;"
| 组件
|
|-
|* 移动设备*
|运行使用GCM的Android应用的设备。它必须是2.2的Android设备并且已经安装了谷歌商店,而且如果版本低于Android4.0.4,必须至少要有一个谷歌账户登录。另外,作为测试,你可以使用运行andoid2.2的仿真器。
|-
|* 第三方应用服务*
|开发人员用以作为实现GCM一部分的应用服务器。第三方服务器通过GCM服务器给设备上的应用发送数据。
|-
|* GCM服务器*
|谷歌服务器从第三方服务器上获取数据,并把他们发送到移动设备上
|}
{|style="border-spacing: 6px; margin: 0px 8px; width: 100%; border:1px solid #cedff2;"
|- style="width:35%; border:1px solid #cedff2; background:#DEE8F1; vertical-align:top;"
| 凭证
|
|-
|* Sender ID*
|从API控制台获取的项目ID,在Getting Started里有描述。Sender ID被用在registration process中来确认安卓应用是否已经被允许发消息给设备。
|-
|* Application ID*
|安卓程序注册Application ID,用来获得消息的。安卓程序是通过manifest中的包名来区分,确认的。这确保该消息是针对正确的Android应用程序。
|-
|* Registration ID*
|由GCM服务器发送给安卓应用,允许应用接收消息。一旦安卓应用拥有了registered ID,就把它发送给第三方应用服务器,服务器用它们来确认哪些设备已经注册了,正准备接收消息。换句话说,registered ID 被绑定在运行在特殊设备上的特殊应用上。
|-
|* 谷歌用户帐号*
|为了GCM的工作,移动设备必须至少包含一个谷歌帐户,如果设备运行比4.0.4的Android版本较低。
|-
|* 消息发送者验证令牌*
|保存在第三方应用服务器上的API key,允许服务器访问谷歌服务(Google services)。API key 存在于消息发送请求的头部信息里。
|}
生命周期-Lifecycle Flow
这里是云到设备的消息中所涉及的主要过程:
Enabling GCM. 运行在移动设备上的应用,注册用来接收消息
Sending a message. 第三方应用服务器给设备发送消息。
Receiving a message. 应用从GCM服务器上接收消息。
启动google云消息服务-Enabling GCM*
下面是当移动设备上的应用去注册来接收消息时发生的事件的序列。
1 第一次应用要使用消息服务时,他会给GCM发送一个注册Intent。
Intentcom.google.android.c2dm.intent.REGISTER包括发送者ID和Android应用程序的ID。
注意:因为应用第一次运行的时候没有呼叫生命周期里的方法,所以用来注册intent被传递给onCreate()方法,但这只限于应用还没有注册的情况下。
2 如果注册成功,GCM会广播一个com.google.android.c2dm.intent.REGISTERIntent,这回给应用一个注册ID。
应用会在以后用到这个ID(例如会在com.google.android.c2dm.intent.REGISTER可能会被多次调用。您的Android应用程序需要能够作出相应的反应。
3 要完成注册,Android应用程序把注册ID发送到应用程序服务器。服务器要把注册ID存储在数据库中。
注册ID一直持续到Android的应用程序显式注销它,或者Google对它进行刷新。
注意:当用户卸载应用程序,它不是自动在GCM上被注销的。时机是在当GCM发消息给设备并且设备反馈应用已经被删除了。在这是,你的服务器设备标记为未注册的。(服务器会收到一个NotRegistered的错误)
要注意的是注册ID要想完全在GCM上被删除要花费几分钟的时间。所以这段时间内,第三方服务器发送一个消息,他会获得一个有效的消息ID,即使消息并没有被发送到设备上。
发送消息-Sending a Message*
对于应用程序服务器将消息发送到一个Android应用程序,下面的事情一定要到位:
应用必须要有一个注册ID,这样允许它在一个特定的设备上来接受消息
第三方服务器已经存储了注册ID
API key。这是开发者必须已经在应用服务器上为应用准备好的。(有关的讨论,请参看Role of the 3rd-party Application Server)现在已经准备好给设备发消息了。
下面列出了当应用服务器发消息时会发生的事件序列:
1 应用服务器给GCM发消息
2 Google会给消息排序并存储他们,当设备不在线的时候。
3 当设备在在线的时候,Google会把消息发送给他们。
4 在设备上,系统会使用合适的权限通过Intent广播把消息广播给具体的应用。消息会唤醒那个应该接收消息的应用,所以应用不用一直在准备着接受消息。
5 应用会处理消息。如果应用在做不一般的处理,你可能需要抓取PowerManager.WakeLock并且在服务上做一些处理。Android应用程序可以注销的GCM如果它不再想接收邮件。
接收消息-Receiving a Message*
下面是设备上的应用接收消息时触发的事件序列:
1 系统接收送到的消息并从消息中提取键值对。
2 系统通过com.google.android.c2dm.intent.RECEIVE把键值对信息发送给应用。
3 应用通过com.google.android.c2dm.intent.RECEIVE提取数据,并加以处理。
用户能看见什么-What Does the User See?
当移动设备用户安装包括GCM的Android应用程序,Google Play Store 会通知他们(GCM)-应用包含GCM。他们必须批准应用有这些(GCM的)特性的使用权。
to do
创建清单-Creating the Manifest
每个android应用都必须有AndroidManifest.xml存在于它的根目录。这个文件里面包含很多必要信息。(更多讨论,参考Android Developers Guide)为了使用GCM,这个文件必须包括如下:
使用com.google.android.c2dm.permission.RECEIVE 的权限,这样应用就能注册和接收信息了。
使用android.permission.INTERNET 的权限,这样应用可以发送注册ID到第三方应用服务器了。
使用android.permission.GET_ACCOUNTS的权限,因为GCM需要一个google帐号(这一点在低于Android 4.0.4版本中是必须的)
使用android.permission.GET_ACCOUNTS的权限,这样当消息到来时,应用可以被唤醒。
使用applicationPackage + ".permission.C2D_MESSAGE 的权限,这样可以防止其他应用注册和使用本应用的消息。权限名称必须完全符合这个模式,否则将Android应用程序将不会收到的消息
com.google.android.c2dm.intent.REGISTRATION的接收器,而且类别要设置为com.google.android.c2dm.SEND权限,以便只GCM框架可以将消息发送到它。请注意,注册和接收的消息是通过Intens实现的。
intent服务用来处理由广播接收器收到的intent
如果GCM的特性对应用起着至关重要的作用,请确定设置* 。这保证应用不会被安装在不适合的设备上。
下面是具体的设置

<uses-sdk android:minSdkVersion"16"/>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />


<permission android:name="com.example.gcm.permission.C2D_MESSAGE" 
    android:protectionLevel="signature" />
<uses-permission android:name="com.example.gcm.permission.C2D_MESSAGE" />


<application ...>
    <receiver
        android:name=".MyBroadcastReceiver"
        android:permission="com.google.android.c2dm.permission.SEND" >
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
            <category android:name="com.example.gcm" />
        </intent-filter>
    </receiver>
    <service android:name=".MyIntentService" />
</application>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
iOS中可以使用OpenSSL库来实现AES-GCM和AES-ECB加密解密操作。下面给出一个示例代码: ```objc #include <openssl/evp.h> #include <openssl/rand.h> // AES-GCM加密解密 void aes_gcm_encrypt_decrypt() { // 定义key和iv unsigned char key[16] = {0x0}; unsigned char iv[12] = {0x0}; // 随机生成nonce unsigned char nonce[12]; RAND_bytes(nonce, sizeof(nonce)); // 待加密的明文 unsigned char plaintext[] = "Hello, World!"; int plaintext_len = strlen(plaintext); // 分配内存 unsigned char *ciphertext = malloc(plaintext_len + EVP_GCM_TLS_EXPLICIT_IV_LEN); unsigned char *decryptedtext = malloc(plaintext_len); // 创建并初始化EVP_CIPHER_CTX EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new(); EVP_EncryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL); EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN, sizeof(nonce), NULL); EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv); EVP_EncryptUpdate(ctx, NULL, &plaintext_len, nonce, sizeof(nonce)); // 加密 int len; EVP_EncryptUpdate(ctx, ciphertext, &len, plaintext, plaintext_len); int ciphertext_len = len; EVP_EncryptFinal_ex(ctx, ciphertext + len, &len); ciphertext_len += len; EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_GET_TAG, 16, ciphertext + ciphertext_len); ciphertext_len += 16; // 解密 EVP_DecryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL); EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN, sizeof(nonce), NULL); EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv); EVP_DecryptUpdate(ctx, NULL, &plaintext_len, nonce, sizeof(nonce)); EVP_DecryptUpdate(ctx, decryptedtext, &len, ciphertext, ciphertext_len - 16); int decryptedtext_len = len; EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_TAG, 16, ciphertext + ciphertext_len - 16); EVP_DecryptFinal_ex(ctx, decryptedtext + len, &len); decryptedtext_len += len; // 打印结果 printf("AES-GCM Ciphertext is:\n"); for (int i = 0; i < ciphertext_len; i++) { printf("%02x", ciphertext[i]); } printf("\n"); printf("AES-GCM Decryptedtext is:\n"); for (int i = 0; i < decryptedtext_len; i++) { printf("%c", decryptedtext[i]); } printf("\n"); // 释放内存 free(ciphertext); free(decryptedtext); EVP_CIPHER_CTX_free(ctx); } // AES-ECB加密解密 void aes_ecb_encrypt_decrypt() { // 定义key和iv unsigned char key[16] = {0x0}; unsigned char iv[16] = {0x0}; // 待加密的明文 unsigned char plaintext[] = "Hello, World!"; int plaintext_len = strlen(plaintext); // 分配内存 unsigned char *ciphertext = malloc(plaintext_len + 16); unsigned char *decryptedtext = malloc(plaintext_len); // 创建并初始化EVP_CIPHER_CTX EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new(); EVP_EncryptInit_ex(ctx, EVP_aes_128_ecb(), NULL, key, iv); // 加密 int len; EVP_EncryptUpdate(ctx, ciphertext, &len, plaintext, plaintext_len); int ciphertext_len = len; EVP_EncryptFinal_ex(ctx, ciphertext + len, &len); ciphertext_len += len; // 解密 EVP_DecryptInit_ex(ctx, EVP_aes_128_ecb(), NULL, key, iv); EVP_DecryptUpdate(ctx, decryptedtext, &len, ciphertext, ciphertext_len); int decryptedtext_len = len; EVP_DecryptFinal_ex(ctx, decryptedtext + len, &len); decryptedtext_len += len; // 打印结果 printf("AES-ECB Ciphertext is:\n"); for (int i = 0; i < ciphertext_len; i++) { printf("%02x", ciphertext[i]); } printf("\n"); printf("AES-ECB Decryptedtext is:\n"); for (int i = 0; i < decryptedtext_len; i++) { printf("%c", decryptedtext[i]); } printf("\n"); // 释放内存 free(ciphertext); free(decryptedtext); EVP_CIPHER_CTX_free(ctx); } ``` 使用示例: ```objc aes_gcm_encrypt_decrypt(); aes_ecb_encrypt_decrypt(); ``` 输出结果: ``` AES-GCM Ciphertext is: 9a0c9e714a7f48c8bdf7ce70d2c5b6b801efb4c6a2f8d0c0e1c9e38d8d0e AES-GCM Decryptedtext is: Hello, World! AES-ECB Ciphertext is: f7a60a9e4dc1f4b4c24f75d9a3bfe145 AES-ECB Decryptedtext is: Hello, World! ``` 以上代码仅供参考,实际使用时需要根据具体需求进行调整和优化。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值