Filtering Foreground Notifications

Your notifications work great, but they are sent even when the user already has the application open. You can use broadcast intents to change the behavior of PollWorker based on whether your app is in the foreground.

First, you will send a broadcast intent from your PollWorker whenever new photos are fetched. Next, you will register two broadcast receivers. The first receiver will be registered in your Android manifest. Whenever it receives a broadcast from PollWorker, it will post the notification to the user as you had done before. The second receiver will be registered dynamically so that it is only active when your application is visible to the user. Its job will be to intercept broadcasts from ...

Get Android Programming: The Big Nerd Ranch Guide, 4th Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.