SUNSHINE APP : Background Task

GitHub:

Synchronizing The Weather: S10.01-SynchronizingTheWeather
SmarterSyncing: S10.02- SmarterSyncing
Sunshine FirebaseJobDispatcher: S10.03-FirebaseJobDispatcher
Sunshine Notifications: S10.04-Notifications

* Reference: Udacity Nanodegree Program- Become an Android Developer *

Screen Shot 2018-02-14 at 9.41.34 PM

Fill out NotificationUtils

We’ve started this for you, as there is some code that simply has nothing to do with creating notifications, and rather just accessing our data. We wanted you to get right into it, so let’s do just that.

  • (1) Create a constant int identifier for our notification. This can be used later to access the notification. Very useful for updates and for cancelling ongoing notifications, things like that.
  • (2) Next, create an Intent with the proper Uri to start the DetailActivity.
  • (3) We want to navigate back to the MainActivity from the DetailActivity if the user clicks the Notification and then clicks back, so use TaskStackBuilder for that.
  • (4) Finally, assign that intent to the NotificationBuilder object so that when the user clicks the notification, it is fired off.
  • (5) In order to notify the user, we need a reference to the NotificationManager, so use getSystemService to do so.
  • (6) Now that everything is ready, notify the user and also save the time at which we showed this notification. Notifications are totally super awesome, but we don’t want to annoy our users with too many of them.

Bools and default values

We’ll have a preference for whether or not to show notifications. The default value for this preference will be true, but we don’t want to just hard code that value.

  • Create bools.xml under res/values and within it, create a boolean value set to true.

Should we notify the user when we sync the data?

Using FirebaseJobDispatcher, we plan on updating the data from 6 to 8 times a day, depending on exactly when the system decides that’s best. We don’t want to send the user that many notifications, though!

  • Within SunshineSyncTask, first check to see if notifications are enabled at all. If they are, we’ll also need to check to see when the last time we notified the user was. If it was less than a day ago, it’s better that we hold off, and just keep our user happy that her weather data is up to date and ready to be displayed as soon as she wants it!

 

 

//              TODO (14) Check if a day has passed since the last notification
                if(timeSinceLastNotification >= DateUtils.DAY_IN_MILLIS){
                    oneDayPassedSinceLastNotification = true;
                }

//              TODO (15) If more than a day have passed and notifications are enabled, notify the user
                /*
                 * We only want to show the notification if the user wants them shown and we
                 * haven't shown a notification in the past day.
                 */
                if(notificationsEnabled && oneDayPassedSinceLastNotification){
                    NotificationUtils.notifyUserOfNewWeather(context);
                }
            /* If the code reaches this point, we have successfully performed our sync */
<span id="mce_SELREST_start" style="overflow:hidden;line-height:0;"></span>

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s