What is the best practice for doing work after receiving a C2DM message?

Go To StackoverFlow.com


Can somebody explain the common way to handle C2DM intents work?

I have an app that does intelligent polling - it's essentially a messaging application - so it polls the server to check your inbox for new messages. There is an activity UI, and a background Service that polls conservatively.

Now to supplement the polling I have included C2DM - presumably to push inbox content that sits unread for 30 minutes.

So when I receive a C2DM RECEIVE intent, I am telling my background service to go poll.

How will this work?

I assume C2DM's main use case is an "idle" phone (i.e. in your pocket, screen off). So you get a RECEIVE intent, grab a wake lock, and fire off the background service to go poll; returning wake lock once you send an intent to the background service.

How long will the background service stay alive?

Won't android just kill it off right away?

Should I just try to do 1 server ping and then die?

I'm just confused about how much processing I can do after getting a C2DM intent.

2012-04-04 03:17
by paulpooch
a reminder just in case. C2DM is not available for Amazon App store - Win Myo Htet 2012-04-04 03:32


The approach you're suggesting sounds solid. To use C2DM as a way to supplement polling is a good appraochapproach. C2DM on its own is not 100% reliable (it never will be) and your design protects against that.

The service will run until it finishes its work. I expect it would show a notification to the user, or update a message list.

I'd set the service up to do an exponential-back-off retry, and have it try X times before failing. If it fails, then the next time a poll occurs it'll be brought up to date. I wouldn't want the background service to try for too long, as that's what the polling code is there to do.

Your service will keep going unless the OS needs memory badly. You could force the service to be a foreground service (with notification icon) but I don't expect that's necessary.

I'm assuming your service is polling the service regularly, so it will make up for any failure in the C2DM message to make a server request.

2012-04-05 15:55
by Ollie C
My current implementation is basically onC2dmMessage( just start polling again ). Should the service terminate itself or can I rely on the OS to just kill it off on its own. - paulpooch 2012-04-08 21:31
Not sure what you mean. If it were me I'd have polling running all the time, and then C2DM messages handled using a separate short-life service - Ollie C 2012-04-09 09:16