为什么首选使用 GCM 进行推送通知? [英] Why it is preferred to use GCM for push notifications?

查看:21
本文介绍了为什么首选使用 GCM 进行推送通知?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I know, it's so. But I don't understand, why?

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

Why not simply send queries to server periodically? Sure, it may discharge battery and increase internet traffic. I understand it. But how usage of Google Cloud Messaging can eliminate this problems?

我找到了答案.但对我来说不是很清楚.

I have found an answer. But it isn't pretty clear for me.

谁能给我一个清楚的解释?

Can anyone give me a clear explanation?

推荐答案

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

Let's say you have 50 applications on your phone that do not use GCM. Each app developer decides it is appropriate to poll their respective backend once a minute.

由于这些都是独立的应用程序,因此每次调用可能不会与另一个 api 调用同时发生.对电池的最大伤害是当 Android 设备中的无线电在关闭后必须重新打开以进行 API 调用时,因此在时间块之间发生的多次调用会更快地耗尽电池(阅读无线电状态机上的这篇文章以更好地理解为什么这是 https://developer.android.com/培训/高效下载/高效网络访问.html)

Since these are all separate applications, each call will likely not happen at the same time as another api call. The biggest kill to battery is when the radio within an android device has to turn back on after being off to make an API call, so multiple calls happening with blocks of time in between drains battery faster (read this article on the radio state machine to better understand why this is https://developer.android.com/training/efficient-downloads/efficient-network-access.html)

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

In addition, each application will be hitting a separate endpoint. Each time you make an API call, you have to go through the connect process for a given server. With batched api requests or HTTP 2.0, multiple calls going to the same server can be optimized by not having to re-do a handshake or the connect process.

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

Now imagine, all 50 applications used GCM. GCM will poll an endpoint at some regular time interval on behalf of all 50 apps. Let's say GCM polls once a minute to a server that all the respective apps' backends send their notifications to to send to a device. You have reduced 50 different oddly timed API calls that are likely turning on and off the battery to one api call. You will use less data for polling. You do not incur the cost of the connect step of an HTTP call to 50 different servers. In addition, google is using the same polling already in place checking for OS updates, so there is no additional network overhead from using GCM (this info is based on old docs What technology does GCM (Google Cloud Messaging) use?)

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

Also, see this explanation straight from the Android website in an article entitled "Minimizing the Effects of Regular Updates" (http://developer.android.com/training/efficient-downloads/regular_updates.html):

每次您的应用轮询您的服务器以检查是否需要更新时,您都会在典型的 3G 连接上激活无线电,不必要地消耗电力长达 20 秒.

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.

Android 版 Google Cloud Messaging (GCM) 是一种轻量级机制,用于将数据从服务器传输到特定应用实例.使用 GCM,您的服务器可以通知在特定设备上运行的应用有可用的新数据.

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.

与轮询相比,您的应用必须定期 ping 服务器以查询新数据,这种事件驱动模型允许您的应用仅在知道有数据要下载时才创建新连接.

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 是使用持久的 TCP/IP 连接实现的.虽然可以实现您自己的推送服务,但最好使用 GCM.这最大限度地减少了持久连接的数量,并允许平台优化带宽并最大限度地减少对电池寿命的相关影响.

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.

这篇关于为什么首选使用 GCM 进行推送通知?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆