Filtering Foreground Notifications

With that sharp corner filed down a bit, we turn to another imperfection in PhotoGallery. Your notifications work great, but they are sent even when the user already has the application open.

You can fix this problem with broadcast intents, too. But they will work in a completely different way.

Sending broadcast intents

This is the easiest part of the solution: sending your own broadcast intent. To send a broadcast intent, just create an intent and pass it in to sendBroadcast(Intent). In this case, you will want it to broadcast an action you define, so define an action constant as well. Add the following bits of code to PollService.

Listing 30.5  Sending a broadcast intent (PollService.java)

p​u​b​l​i​c​ ​c​l​a​s​s​ ...

Get Android Programming: The Big Nerd Ranch Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.