August 9, 2018 comments 84 Reads

In part 6 of the notifications tutorial, we will learn how to add a ProgressBar to a notification and update it periodically. This is useful if you for example want to download something in a Foreground Service.
To add the ProgressBar, we call setProgress on our NotificationCompat.Builder, where we have to pas a max value, the progress value and a boolean which indicates if we want to have an indeterminate ProgressBar that has an ongoing animation, or a determinate ProgressBar that shows the actual progress.
With setOngoing we can make our notification persistent, so it can’t be swiped away, and with setOnlyAlertOnce we make sure that it doesn’t make sound or pop up everytime we update it with notificationManager.notify.
Watch the whole playlist: