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.
Have your receiver registered in the manifest, per the C2DM documentation.
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-priorityBroadcastReceiverfor your own broadcast, and have another manifest-registeredBroadcastReceiverimplement a normal-priorityBroadcastReceiverfor your own broadcast. If the activity gets the broadcast, it displays your popup (ick) and aborts the broadcast. If your “backstop”BroadcastReceivergets the broadcast, it displays aNotification. 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.