Android 是否支持近乎实时的推送通知?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1243066/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 02:49:25  来源:igfitidea点击:

Does Android support near real time push notification?

androidpush-notification

提问by j pimmel

I recently learned about the ability of iPhone apps to receive nearly instantaneous notifications to appsnotifications to apps.

我最近了解到 iPhone 应用程序能够接收几乎即时的应用程序通知到应用程序的通知

This is provided in the form of push notifications, a bespoke protocol which keeps an always on data connection to the iPhone and messages binary packets to the app, which pops up alerts incrediblyquickly, between 0.5 - 5 seconds from server app send to phone app response time. This is sent as data - rather than SMS - in very very small packets charged as part of the data plan not as incoming messages.

这是以推送通知的形式提供的,这是一种定制的协议,它始终保持与 iPhone 的数据连接,并将消息二进制数据包发送到应用程序,从服务器应用程序发送到手机应用程序的时间在 0.5 - 5 秒之间,它以难以置信的速度弹出警报响应时间。这是作为数据发送的 - 而不是 SMS - 在非常非常小的数据包中作为数据计划的一部分而不是作为传入消息进行收费。

I would like to know if, using Android, there is either a similar facility, or whether it's possible to implement something close to this using Android APIs. To clarify, I define similar as:

我想知道在使用 Android 时是否有类似的工具,或者是否可以使用 Android API 实现与此类似的功能。为了澄清,我将类似定义为:

  • Not an SMS message, but some data driven solution
  • As real time as is possible
  • Is scalable, i.e., as the server part of a mobile app, I could notify thousands of app instances in seconds
  • 不是短信,而是一些数据驱动的解决方案
  • 尽可能实时
  • 可扩展,即作为移动应用程序的服务器部分,我可以在几秒钟内通知数千个应用程序实例

I appreciate the app could be pull based, HTTP request/response style, but ideally I don't want to be polling that heavily just to check for notification; besides which it's like drip draining the data plan.

我很欣赏该应用程序可以是基于拉取的 HTTP 请求/响应风格,但理想情况下,我不想仅仅为了检查通知而进行大量轮询;除此之外,它就像滴水耗尽数据计划。

采纳答案by j pimmel

Firebase Cloud Messaging FCM FAQis the new version of GCM. It inherits GCM's core infrastructure to deliver messages reliably on Android, iOS and Chrome. However they'll continue to support GCM because lot of developers are using GCM SDKs today to handle notifications, and client app upgrade takes time.

Firebase 云消息传递FCM 常见问题解答是 GCM 的新版本。它继承了 GCM 的核心基础架构,可在 Android、iOS 和 Chrome 上可靠地传递消息。但是,他们将继续支持 GCM,因为如今许多开发人员都在使用 GCM SDK 来处理通知,并且客户端应用程序升级需要时间。

As of June 26, 2012, Google Cloud Messagingis the preferred way of sending messages to applications running on devices.

截至 2012 年 6 月 26 日,Google Cloud Messaging是向设备上运行的应用程序发送消息的首选方式。

Previously (and now deprecated), the service was called Cloud To Device Messaging.

以前(现在已弃用),该服务称为Cloud To Device Messaging

回答by flesh

XMPPis a good solution. I have used it for a push enabled, realtime, Android application. XMPP is powerful, highly extensible and easy to integrate and use.

XMPP是一个很好的解决方案。我已经将它用于支持推送的实时 Android 应用程序。XMPP 功能强大、高度可扩展且易于集成和使用。

There are loads of free XMPP servers (though out of courtesy you shouldn't abuse them) and there are open source servers you can run on one of your own boxes. OpenFireis an excellent choice.

有大量免费的 XMPP 服务器(尽管出于礼貌您不应该滥用它们),并且您可以在自己的一台机器上运行开源服务器。OpenFire是一个很好的选择。

The library you want isn't Smackas noted above, it's aSmack. But note, this is a build environment - you will have to build the library.

您想要的库不是上面提到的Smack,而是aSmack。但请注意,这是一个构建环境 - 您必须构建库。

This is a calculation I did on battery life impact of an XMPP solution:

这是我对 XMPP 解决方案的电池寿命影响所做的计算:

The Android client must maintain a persistent TCP connection by waking up periodically to send a heartbeat to the XMPP server.
This clearly imposes a cost in terms of power usage. An estimate of this cost is provided below:

  • Using a 1400mAh battery (as supplied in the Nexus One and HTC Desire)
  • An idle device, connected to an 3G network, uses approximately 5mA
  • The wake-up, heartbeat, sleep cycle occurs every 5 minutes, takes three seconds to complete and uses 300mA
  • The cost in battery usage per hour is therefore:
    • 36 seconds 300mA = 3mAh sending heartbeat
    • 3600 seconds 5mA = 5mAh at idle
    • 4:95 + 3 = 7:95mAh combined
  • A 1400mAh battery lasts approximately 11.6 days at idle and 7.3 days when running the application, which represents an approximate 37% reduction in battery life.
  • However, a reduction in battery life of 37% represents the absolute worst case in practice given that devices are rarely completely idle.

Android 客户端必须通过定期唤醒以向 XMPP 服务器发送心跳来维持持久的 TCP 连接。
这显然在电力使用方面产生了成本。下面提供了此成本的估算:

  • 使用 1400mAh 电池(Nexus One 和 HTC Desire 中提供)
  • 连接到 3G 网络的空闲设备使用大约 5mA
  • 唤醒、心跳、睡眠周期每 5 分钟发生一次,需要 3 秒完成并使用 300mA
  • 因此,每小时电池使用成本为:
    • 36 秒 300mA = 3mAh 发送心跳
    • 3600 秒 5mA = 空闲时 5mAh
    • 4:95 + 3 = 7:95mAh 组合
  • 一块 1400mAh 的电池在空闲状态下可持续使用约 11.6 天,在运行应用程序时可持续使用 7.3 天,这意味着电池寿命减少了约 37%。
  • 然而,考虑到设备很少完全闲置,电池寿命减少 37% 代表了实践中绝对最糟糕的情况。

回答by dalelane

I recently started playing with MQTT http://mqtt.orgfor Android as a way of doing what you're asking for (i.e. not SMS but data driven, almost immediate message delivery, scalable, not polling, etc.)

我最近开始在Android 上使用 MQTT http://mqtt.org作为一种满足您要求的方式(即不是 SMS,而是数据驱动、几乎即时的消息传递、可扩展、非轮询等)

I have a blog post with background information on this in case it's helpful http://dalelane.co.uk/blog/?p=938

我有一篇博客文章,里面有这方面的背景信息,以防万一它有帮助http://dalelane.co.uk/blog/?p=938

(Note: MQTT is an IBM technology, and I should point out that I work for IBM.)

(注意:MQTT 是 IBM 的一项技术,我应该指出我为 IBM 工作。)

回答by peter

Have a look at the Xtifyplatform. Looks like this is what they are doing,

看看Xtify平台。看起来这就是他们正在做的事情,

回答by William L.

Google is depreciating C2DM, but in its place their introducing GCM (Google Cloud Messaging) I dont think theirs any quota and its free! It does require Android 2.2+ though! http://developer.android.com/guide/google/gcm/index.html

谷歌正在贬低 C2DM,但他们引入了 GCM(谷歌云消息传递),我认为他们没有任何配额而且它是免费的!不过它确实需要Android 2.2+!http://developer.android.com/guide/google/gcm/index.html

回答by jamesh

If you can depend on the Google libraries being there for you target market, then you may want to piggy back on GTalkfunctionality (registering a resource on the existing username - the intercepting it the messages as they come in with a BroadcastReceiver).

如果您可以依靠 Google 库来满足您的目标市场,那么您可能希望利用 GTalk功能(在现有用户名上注册资源 - 当消息通过 BroadcastReceiver 传入时对其进行拦截)。

If not, and I expect you can't, then you're into bundling your own versions of XMPP. This is a pain, but may be made easier if XMPP is bundled separately as a standalone library.

如果没有,并且我希望您不能,那么您正在捆绑您自己的 XMPP 版本。这很痛苦,但如果 XMPP 作为独立库单独捆绑在一起,可能会更容易。

You may also consider PubSubHubub, but I have no idea the network usage of it. I believe it is built atop of XMPP.

您也可以考虑PubSubHubub,但我不知道它的网络使用情况。我相信它是建立在 XMPP 之上的。

回答by mtbkrdave

There is a new open-source effort to develop a Java library for push notifications on Android, using the Meteor comet server as a backend. You can check it out at the Deacon Project Blog. We need developers, so please spread the word!

有一项新的开源工作用于在 Android 上开发用于推送通知的 Java 库,使用 Meteor Comet 服务器作为后端。您可以在Deacon 项目博客上查看。我们需要开发人员,所以请宣传!

回答by Morgan Christiansson

I have been looking into this and PubSubHubBub recommended by jamesh is not an option. PubSubHubBub is intended for server to server communications

我一直在研究这个,jamesh 推荐的 PubSubHubBub 不是一个选择。PubSubHubBub 用于服务器到服务器的通信

"I'm behind a NAT. Can I subscribe to a Hub? The hub can't connect to me."

/Anonymous

No, PSHB is a server-to-server protocol. If you're behind NAT, you're not really a server. While we've kicked around ideas for optional PSHB extensions to do hanging gets ("long polling") and/or messagebox polling for such clients, it's not in the core spec. The core spec is server-to-server only.

/Brad Fitzpatrick, San Francisco, CA

Source: http://moderator.appspot.com/#15/e=43e1a&t=426ac&f=b0c2d(direct link not possible)

“我在 NAT 后面。我可以订阅集线器吗?集线器无法连接到我。”

/匿名的

不,PSHB 是服务器到服务器协议。如果您在 NAT 后面,那么您就不是真正的服务器。虽然我们已经开始考虑可选的 PSHB 扩展来为此类客户端执行挂起获取(“长轮询”)和/或消息框轮询,但它不在核心规范中。核心规范仅适用于服务器到服务器。

/布拉德·菲茨帕特里克,旧金山,加利福尼亚

来源:http: //moderator.appspot.com/#15/e=43e1a&t=426ac&f=b0c2d(无法直接链接)

I've come to the conclusion that the simplest method is to use Comet HTTP push. This is both a simple and well understood solution but it can also be re-used for web applications.

我得出的结论是,最简单的方法是使用 Comet HTTP 推送。这是一个简单且易于理解的解决方案,但它也可以重新用于 Web 应用程序。

回答by Freek Nortier

Google recently(18May2016) announcedthat Firebaseis now it's unified platform for mobile developers including near real time push notifications.It is also multi-platform :

谷歌最近(18May2016)宣布,火力地堡是现在统一为移动开发者,包括近实时推送notifications.It平台也多平台:

The company now offers all Firebase users free and unlimited notifications with support for iOS, Android and the Web.

该公司现在为所有 Firebase 用户提供免费且无限制的通知,并支持 iOS、Android 和 Web。

source

来源

回答by Stefan

I have recently developed http://pushdroid.orgits a single application that should be installed on the phone just like google has implemented it in 2.2 this works from 1.5 and is broadcasting via intent.

我最近开发了http://pushdroid.org,它是一个应该安装在手机上的应用程序,就像谷歌在 2.2 中实现的那样,它从 1.5 开始工作,并通过意图进行广播。