0

I'm using C2DM the first time and I'm looking for a general advice how I can achieve the following: Upon receiving a C2DM messages I decide: - if the application is upon the current activity will display an "alert popup". - if the application is not open I'd like to send a message to the notification bar (similar to new emails, sms, twitter etc.)

We have a GlobalBroadcastReceiver extends BroadcastReceiver which implements public void onReceive(Context context, Intent intent). This is the only receiver registered in AndroidManifest.xml.
So basically all our broadcasts are piped through this receiver and the first scenario is no problem.

However I'm, wondering how to tackle the second problem. How can I make sure I receive a C2DM.RECEIVE broadcast even when my application is closed and then: how can I notify the user about the incoming data?

I'm super confident there are already a lot of solutions out there but since I couldn't find them I think I'm just missing something of the bigger picture.

nuala
  • 2,681
  • 4
  • 30
  • 50

1 Answers1

3

How can I make sure I receive a C2DM.RECEIVE broadcast even when my application is closed

Have your receiver registered in the manifest, per the C2DM documentation.

then: how can I notify the user about the incoming data?

Raise a Notification.

Since your receiver will not necessarily know if there is an activity of yours in the foreground, the best solution is to send your own broadcast Intent, but one that is ordered. Have the activity register a high-priority BroadcastReceiver for your own broadcast, and have another manifest-registered BroadcastReceiver implement a normal-priority BroadcastReceiver for your own broadcast. If the activity gets the broadcast, it displays your popup (ick) and aborts the broadcast. If your "backstop" BroadcastReceiver gets the broadcast, it displays a Notification. Here is a blog post with a bit more detail on this pattern, and here is a sample project demonstrating this use of ordered broadcasts.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Could you elaborate this part a little bit more detailed please? "Have the activity register a high-priority BroadcastReceiver for your own broadcast, and have another manifest-registered BroadcastReceiver implement a normal-priority BroadcastReceiver for your own broadcast." Also I'll check this tomorrow. I need my quiet time to test all of this :) – nuala Apr 27 '12 at 15:07
  • @yoshi: I specifically provided hyperlinks to a blog post and a sample project to provide more details. – CommonsWare Apr 27 '12 at 15:11
  • Sorry for the late response. Just after receiving your answer I had a little bit of drama with the whole c2dm system. I like the pattern you described in the article. Really neat to use ordered broadcasts in that way. Anyway after reading I realised actually all the work was already done. I just didn't understood that `BroadcastReceiver`s registered in the manifest will be execute even with no activity running. Sweet :) – nuala May 03 '12 at 19:38