推送通知技术如何在 Android 上工作?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11508613/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How does push notification technology work on Android?
提问by Khawar Raza
How has Google implemented their push notification feature? Does it work through polling done by a service running in the background or in a different way?
谷歌是如何实现他们的推送通知功能的?它是通过在后台运行的服务完成的轮询还是以不同的方式工作?
采纳答案by Tal Kanel
From what I've heard during an Android developers conference in Israel:
从我在以色列的 Android 开发者大会上听到的:
There is simply a TCP socket waiting in accept modeon a cloud Google server. The TCP connection had been initiated by the Google Play application. That's why Google Play must be installed on the device for making Google Cloud Messaging (GCM)(formerly Android Cloud to Device Messaging Service - C2DM) work.
有一个简单的 TCP 套接字在云谷歌服务器上以接受模式等待。TCP 连接已由 Google Play 应用程序启动。这就是为什么必须在设备上安装 Google Play 才能使Google Cloud Messaging (GCM)(以前称为 Android Cloud to Device Messaging Service - C2DM)正常工作。
When this TCP client socket receives some message, the message contains information such as the package name of the application it should be addressed to, and of course - the data itself. This data is parsed and packed into an intentthat is broadcast and eventually received by the application.
当这个 TCP 客户端套接字接收到一些消息时,该消息包含诸如它应该被寻址到的应用程序的包名称之类的信息,当然 - 数据本身。这些数据被解析并打包成一个意图,该意图被广播并最终被应用程序接收。
The TCP socket stays open even when the device's radio state turns into "idle" mode. Applications don't have to be running to receive the intents.
即使设备的无线电状态变为“空闲”模式,TCP 套接字也保持打开状态。应用程序不必运行即可接收意图。
More information at http://developer.android.com/google/gcm/gcm.html
回答by Aniket Thakur
Android keeps one active connection to Google's servers, but it doesn't use much power or data, because no traffic is sent along it until something sends a Google Cloud Messaging (GCM) message to an app on your phone. There's only one connection on the phone, used by all apps: installing a new app that uses GCM doesn't add any extra load.
Android 与 Google 的服务器保持一个活动连接,但它不会消耗太多电量或数据,因为在某些东西向您手机上的应用程序发送 Google Cloud Messaging (GCM) 消息之前,不会沿着它发送流量。手机上只有一个连接,供所有应用程序使用:安装使用 GCM 的新应用程序不会增加任何额外负载。
The first step in GCM is that a third-party server (such as an email server) sends a request to Google's GCM server. This server then sends the message to your device, through that open connection. The Android system looks at the message to determine which app it's for, and starts that app. The app must have registered with Android to use GCM, and it must have the relevant permission. When the app starts, it might create a notification straight away with the data from the message. GCM messages are very limited in size, so the app might instead open a normal connection to the third-party server to get more information (for example, downloading the headers of new emails).
GCM 的第一步是第三方服务器(例如电子邮件服务器)向 Google 的 GCM 服务器发送请求。然后,该服务器通过该开放连接将消息发送到您的设备。Android 系统查看该消息以确定它适用于哪个应用程序,然后启动该应用程序。该应用程序必须已在 Android 上注册才能使用 GCM,并且必须具有相关权限。当应用程序启动时,它可能会立即使用消息中的数据创建通知。GCM 消息的大小非常有限,因此应用程序可能会打开与第三方服务器的正常连接以获取更多信息(例如,下载新电子邮件的标题)。
The advantage of using push notifications is that apps don't have to run at regular intervals to check for new data, saving both power and data. The advantage of having a centralized mechanism like GCM is that the device only needs one open network connection and the Android GCM system is the only thing that needs to keep running, rather than each app having to stay running in the background to keep its own network connection to its own server.
使用推送通知的优点是应用程序不必定期运行以检查新数据,从而节省电量和数据。拥有 GCM 等中心化机制的好处是设备只需要一个开放的网络连接,Android GCM 系统是唯一需要保持运行的东西,而不是每个应用程序都必须在后台运行以保持自己的网络连接到自己的服务器。
回答by Shreesh
You can implement the push notification on android yourself with a long polling tcp connection. But that would involve maintaining an extra socket => battery drain. Or you can open a connection at regular intervals using the Alarm Manager.
您可以使用长轮询 tcp 连接在 android 上自己实现推送通知。但这将涉及维护一个额外的插座 => 电池消耗。或者,您可以使用警报管理器定期打开连接。
Google probably opens one socket for all the C2DM push notifications, hence its more battery efficient.
Google 可能会为所有 C2DM 推送通知打开一个套接字,因此其电池效率更高。
回答by user1767754
As of April 10, 2018, Google has deprecated GCM. The GCM server and client APIs are deprecated and will be removed as soon as April 11, 2019. Migrate GCM apps to Firebase Cloud Messaging (FCM), which inherits the reliable and scalable GCM infrastructure, plus many new features.
自 2018 年 4 月 10 日起,Google 已弃用 GCM。GCM 服务器和客户端 API 已弃用,最快将于 2019 年 4 月 11 日移除。将 GCM 应用迁移到 Firebase 云消息传递 (FCM),它继承了可靠且可扩展的 GCM 基础架构以及许多新功能。
回答by Yury
Yes, you're right. Google had a service (GTalk Service) and this service asked Google servers in some periods of time.
你是对的。谷歌有一个服务(GTalk Service),这个服务会在一段时间内询问谷歌服务器。
回答by Robert Manus
On Android devices, when you get push notifications, the sender application's image and a message appear in the status bar. Exactly when the client taps the notification, he/she lands on the application.
在 Android 设备上,当您收到推送通知时,发件人应用程序的图像和一条消息会出现在状态栏中。正是当客户端点击通知时,他/她会登陆应用程序。