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 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.