Adding a Download Notification5:28 with Ben Jakuben
In this video we'll see how to add a new notification for our background download service.
All right we're getting pretty comfortable with notifications. 0:00 Let's see what we need to add a new notification for our download service. 0:03 The first thing we need to do is re-enable our downloads. 0:08 We hijacked the download button a while ago but let's go back into main activity, 0:11 and here in the on Create method, let's comment out our test intents method and 0:15 uncomment download songs. 0:20 Now let's open up our Download Intents Service class. 0:23 We're going to need two class level variables. 0:27 So let's get those out of the way. 0:29 Up here at the top, let's add a private Notification Manager, 0:30 which we will use to manage our notifications. 0:34 Let's call it M.notificationManager. 0:37 The second is a unique ID we need to identify this notification 0:42 private static final int. 0:46 Let's call it notification underscore ID and 0:48 we'll set it equal to something kind of random like 22. 0:53 I just double the 11 that we're using from the other service. 0:56 Now let's look at this on handle intent method. 1:00 We're already getting the song title in the song variable so 1:03 we can just use that in the title for a notification. 1:07 Let's start building one just like before. 1:09 So before we call download song, let's add a few lines and 1:11 we'll say notification.Builder, 1:13 let's call it builder equals a newNotification.Builder and 1:16 for the parameter it needs the context which is this. 1:21 Let's chain methods together again. 1:26 So on the next line let's call, .set Small Icon, and we'll use the same icon 1:27 that we swapped out in the last service R.drawable.ic queue music white. 1:32 Next let's set the content title to downloading. 1:39 And then we'll set the content text as the song title 1:45 which we have in the song variable. 1:49 These three pieces of information, an icon, a title, and 1:52 some supporting text are the minimum required data for notifications. 1:55 Next we need to set our notification manager from the system service used for 2:01 notifications am notification manager equals and we're going to need a cast so 2:05 it's a cast to notification manager. 2:10 We'll call get system service and the name is from contacts.notification service. 2:12 I'm going to drop this to a new line so it all shows up here on my screen. 2:19 Finally we can start the notification by calling the notify method so 2:24 mNotificationManager.notify. 2:27 We need to pass in that ID, 2:29 notification ID and we need to pass in the notification itself. 2:31 We haven't built it yet but we can just do it right here we can say builder. 2:35 .build. 2:38 All right, let's try this and see if it works. 2:41 Actually before we do I'm going to speed up these fake downloads by 2:43 changing 10 to 2 here in our endTime calculation. 2:48 Okay, now let's run it. 2:51 Okay, so now we can click on the download button and we see our notification, cool. 2:56 And if we pull this down. 3:00 Got a bug. 3:03 We have the icon and the title. 3:03 But we don't have the song title. 3:05 Let's take a look and see what's going on. 3:07 So if we come back up here, we're setting the song as the extra. 3:10 But it's a string extra. 3:14 I think I know what's happening. 3:15 So back in main activity, 3:17 if you remember, we switched it to pass in a whole song instead of just a string. 3:18 So Let's go to download songs, and 3:24 here we have a loop running through all of the songs and 3:26 it's trying to put it as an extra as a whole song object, that's cool, 3:32 so back in our intent service, we need to change this to get a song variable, 3:38 and you need to change this method to get Parcelable Extra. 3:42 Now we can call song.get title where we need it. 3:47 And we'll pass in just the string title here because that's all that's 3:51 required for this download song method song.getTitle. 3:54 Okay, let's try this again. 3:59 Okay, we'll go for a download. 4:03 And this time. 4:06 Okay, there we go. 4:07 We see it cycling through. 4:07 It's downloading each song as it works through the loop. 4:08 But notice that it doesn't disappear at the end when all the downloads are done. 4:13 We need to cancel each notification when each download is complete. 4:18 We'll do it by using the notification manager, 4:22 here at the end of the download song method. 4:24 Let's add a line and say mNotificationManager.cancel we 4:28 need to pass in the ID which we already set as notification ID. 4:33 That's how we can manage a specific notification by giving it an ID. 4:38 And I want to show one more thing. 4:42 Up here we're building it, let's change one more method let's call, .setProgress, 4:43 and pass in the parameters 0, 0, and true. 4:48 This can be customized to show a calculated value but by using 00 and 4:52 true, we can create an indeterminate progress bar. 4:56 So here let's watch how it works. 4:59 This time as we download things get the notification and 5:05 we will see a download bar showing that it's doing some activity. 5:09 And if we wait for the last one it will disappear when all the downloads are done. 5:14 Cool. It's working. 5:18 All right, so as extra practice why don't you make it so that the user can 5:20 tap on these notifications and view a specific song in the detail activity. 5:23
You need to sign up for Treehouse in order to download course files.Sign up