android - 为什么首选使用 GCM 进行推送通知?

标签 android push-notification google-cloud-messaging

我知道,就是这样。但是我不明白,为什么?

为什么不简单地定期向服务器发送查询呢?当然,它可能会耗尽电池并增加互联网流量。我明白了。但是如何使用Google Cloud Messaging可以消除这个问题吗?

我找到了 an answer .但对我来说还不是很清楚。

谁能给我一个明确的解释?

最佳答案

假设您的手机上有 50 个不使用 GCM 的应用程序。每个应用开发者都决定每分钟轮询一次他们各自的后端是合适的。

由于这些都是独立的应用程序,因此每个调用可能不会与另一个 api 调用同时发生。最大的电池消耗是当 android 设备中的 radio 在关闭后必须重新打开以进行 API 调用时,因此多次调用之间的时间 block 会更快地耗尽电池电量(阅读 radio 状态机上的这篇文章以了解更好地理解为什么这是 https://developer.android.com/training/efficient-downloads/efficient-network-access.html )

此外,每个应用程序都将访问一个单独的端点。每次进行 API 调用时,都必须完成给定服务器的连接过程。通过批处理 API 请求或 HTTP 2.0,可以优化对同一服务器的多个调用,而无需重新进行握手或连接过程。

现在想象一下,所有 50 个应用程序都使用了 GCM。 GCM 将代表所有 50 个应用程序以某个固定时间间隔轮询端点。假设 GCM 每分钟轮询一次服务器,所有相应应用程序的后端将通知发送到该服务器以发送到设备。您已将可能打开和关闭电池的 50 个不同的奇怪定时 API 调用减少到一个 API 调用。您将使用更少的数据进行轮询。您不会产生对 50 个不同服务器的 HTTP 调用的连接步骤的成本。此外,谷歌正在使用相同的轮询检查操作系统更新,因此使用 GCM 不会产生额外的网络开销(此信息基于旧文档 What technology does GCM (Google Cloud Messaging) use?)

此外,请直接从 Android 网站上一篇题为“最小化定期更新的影响”(http://developer.android.com/training/efficient-downloads/regular_updates.html) 的文章中查看此解释:

Every time your app polls your server to check if an update is required, you activate the wireless radio, drawing power unnecessarily, for up to 20 seconds on a typical 3G connection.

Google Cloud Messaging for Android (GCM) is a lightweight mechanism used to transmit data from a server to a particular app instance. Using GCM, your server can notify your app running on a particular device that there is new data available for it.

Compared to polling, where your app must regularly ping the server to query for new data, this event-driven model allows your app to create a new connection only when it knows there is data to download.

The result is a reduction in unnecessary connections, and a reduced latency for updated data within your application.

GCM is implemented using a persistent TCP/IP connection. While it's possible to implement your own push service, it's best practice to use GCM. This minimizes the number of persistent connections and allows the platform to optimize bandwidth and minimize the associated impact on battery life.

关于android - 为什么首选使用 GCM 进行推送通知?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31645010/

相关文章:

android - 我可以在 Embarcadero C++Builder 中为 Android (*.so) 构建共享库吗?

android - Dagger 2 Android 应用程序组件缺少注入(inject)方法

android - 弄清楚警报管理器

一次推送显示两次 iOS 推送通知横幅

android - 以编程方式使用 Amazon pinpoint 发送推送通知

google-cloud-messaging - 需要新的 GCM google-services.json 和插件吗?

android - 如何为 Android 设置 Eclipse

ios - APNS:openssl s_client gethostbyname失败

android - Google C2D_MESSAGE 应用程序未定义权限?

android - 谷歌Android导入类文件的问题