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

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

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

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

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

解决方法

假设您的手机上有50个不使用GCM的应用程序.每个应用开发者都决定每分钟调查一次后端.

由于这些都是单独的应用程序,因此每次调用可能不会与另一个api调用同时发生.对电池的最大杀戮是当一个Android设备中的无线电在关闭之后必须重新打开以进行API调用时,所以多个呼叫在时间段之间发生电池更快地耗尽电池(在无线电状态机上阅读这篇文章到更好地理解为什么这是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 如何解决dialog弹出时无法捕捉Activity的back事件 在...
Android实现自定义带文字和图片的Button 在Android开发中经常...
Android 关于长按back键退出应用程序的实现最近在做一个Andr...
android自带的时间选择器只能精确到分,但是对于某些应用要求...