ChatGPT解决这个技术问题 Extra ChatGPT

Does Android support near real time push notification?

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

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 incredibly quickly, 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.

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:

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.

The notification for gmail and gchat is usually within that threshold. There should be a way to replicate this in your own app.
Yes, There's an android library that makes push messaging very simple... just include Jigy's Droid Notifications library in your app and from there it's just about 10 lines of code... hope this helps :)
Check out Pushy (pushy.me) for a realtime push notification gateway that uses MQTT under the hood for improved reliability. Full disclosure - I founded Pushy.

J
Jay Savsani

Firebase Cloud Messaging FCM FAQ is 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.

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

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


Google provides detailed documentation for implementing this into your Android app in Java at code.google.com/android/c2dm but their sample code for communicating with the server side aspect of C2DM is lacking. I've written up a tutorial for that aspect here: blog.boxedice.com/2010/10/07/…
Is there a way to offer push notifications to users with Android 2.2, but make the same app runnable on older distributions, just without push notifications?
@OneWorld: yes, just like any other Android backward compatibility technique, use reflection to pick alternate strategy when C2DM is unavailable, for more see: developer.android.com/resources/articles/…
developer.android.com/videos/index.html#v=PLM4LajwDVc Google I/O 2010 Presentation about Push Notifications
C2DM has been officially deprecated as of June 26, 2012, use [Google Cloud Messaging for Android] (developer.android.com/guide/google/gcm/index.html instead
f
flesh

XMPP is 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.

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. OpenFire is an excellent choice.

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

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

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.


However in pratice the phone will last 3 hours when apps do this since hundreds of intents run when the phone wakes up or display switches on or network state changes.
d
dalelane

I recently started playing with MQTT http://mqtt.org for 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.)

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

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


How is mqtt doing this? There must be some polling involved somewhere? Even apple has to poll but I think they are only doing one connection at a time therefore not draining the battery that much...
It does it by opening a TCP/IP connection and leaving it open. It doesn't poll, although it does have to occasionally send a small ping message down the connection to keep it alive.
MQTT is definitely the way to go if sending your push message through Google makes you uncomfortable. Also, we have a nondisclosure agreements with our clients which prohibit the use of cloud services for data exchanges.
J
Janusz

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


W
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


j
jamesh

If you can depend on the Google libraries being there for you target market, then you may want to piggy back on GTalk functionality (registering a resource on the existing username - the intercepting it the messages as they come in with a 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.

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


I think the article stats that the piggy back functionality will be always be available as long as the gtalk libraries are on the device. Do you have some resources with examples how to actually do that?
GTalk is removed from the SDK since 1.0 (iirc).
M
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

"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)

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.


m
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!


F
Freek Nortier

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

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

source


W
Will

I cannot find where I read it at, but I believe gmail utilizes an open TCP connection to do the e-mail push.


There is a blog post at joelapenna.com/blog/2009/07/android-foursquare-and-push by a developer of an Android app that seems to support this.
These seem to me, they are polling too. The mobile device makes a connection to the server and notifies the server it's waiting for updates. Actual push is initiated by the server, which is very difficult for mobile devices, and most of the time involves network provider support (sms/wap push)...
"C2DM allows third-party developers to use the same service the Google apps do." from Android Cloud To Device Messaging
M
MrSnowflake

As GTalk is gone from the SDK, it might be a good idea to make a 'standard' push messaging system. That way, only one service has to run, only one extra tcp connection needs to be open. Applications should talk to this service using Intents and should first request permission to send and receive notification from the service. The service should then notify the user a new application wants to send and receive messages. The user will then grant or deny permission, so he stays in control. The application will then register an action + category to the service, so the service knows how to deliver the pushed message.

Would the a good idea or not?


R
Rahul Patel

Why dont you go with the XMPP implementation. right now there are so many public servers available including gtalk, jabber, citadel etc. For Android there is one SDK is also available named as SMACK. This we cant say a push notification but using the XMPP you can keep a connection open between client and server which will allow a two way communication. Means Android client and server both can communicate to each other. At present this will fulfill the need of Push in android. I have implemented a sample code and it really works great


Care to provide a link to your sample code? Done any testing regarding battery life?
I've done testing on battery life - the heartbeat you need to send every six minutes reduces battery life by 30% in the worst case (ie. the device only wakes up to send a heartbeat). In the best case (the user is using the device for browsing, calls etc), the impact on battery life is negligible.
S
Stefan

I have recently developed http://pushdroid.org its 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.


f
fernandohur

The problem with GCM is that there is a lot of configuration involved in the process:

You have to add a lot of boilerplate to you Android app

You need to configure an external server to comunicate with the GCM server

You will have to write tests

If you like simple things (like me) you should try UrbanAirship. It is (IMHO) the easiest way to use GCM in your app without doing a lot of configuration. It also gives you a pretty GUI to test that your GCM messages are being delivered correctly.

You can find the docs and getting started guide here

You can find a sample application here

Note: I am not afiliated with UrbanAirship in any way


2
2 revs

They have their listeners which has to be used by you by using their library classes in your code. You need not to bother about pushing. You have to send the message to server server will push the message to the device. They use OAuth. Regarding Protocols, there are two methods using CCS and XMPP. CCS just uses XMPP as an authenticated transport layer, so you can use most XMPP libraries to manage the connection. To send notifications to device you can write code in android app to send as well as your server code. the message sending will be done only by your code. Rest will be taken care by Google Server in GCM case. You can check detail at this link

http://developer.android.com/google/gcm/server.html

Also, for security issues

google cloud messaging security https://groups.google.com/forum/#!topic/android-gcm/M-EevBitbhQ

In case your app is not running then also devices can recieve notification because you have to write code for broadcast listeners. In background it will be listening to server and whenever any message packet will be there it will recieve the message as notification. Android has service you need to not to bother about it. You have only to use those resources using the library class that makes your work easier and let them write if your app is not running then also it recieve notification. Obviously, there would be some listener whick make the app to recieve.Check "Recieve the message" section in this link

http://developer.android.com/google/gcm/client.html

It will acccept request from users also. For GCM it will do. Please check "Send a message"

http://developer.android.com/google/gcm/client.html