1 00:00:00,340 --> 00:00:03,330 All right we're getting pretty comfortable with notifications. 2 00:00:03,330 --> 00:00:06,750 Let's see what we need to add a new notification for our download service. 3 00:00:08,180 --> 00:00:11,180 The first thing we need to do is re-enable our downloads. 4 00:00:11,180 --> 00:00:15,961 We hijacked the download button a while ago but let's go back into main activity, 5 00:00:15,961 --> 00:00:20,603 and here in the on Create method, let's comment out our test intents method and 6 00:00:20,603 --> 00:00:22,280 uncomment download songs. 7 00:00:23,580 --> 00:00:27,010 Now let's open up our Download Intents Service class. 8 00:00:27,010 --> 00:00:29,000 We're going to need two class level variables. 9 00:00:29,000 --> 00:00:30,710 So let's get those out of the way. 10 00:00:30,710 --> 00:00:34,416 Up here at the top, let's add a private Notification Manager, 11 00:00:34,416 --> 00:00:37,530 which we will use to manage our notifications. 12 00:00:37,530 --> 00:00:41,713 Let's call it M.notificationManager. 13 00:00:42,760 --> 00:00:46,730 The second is a unique ID we need to identify this notification 14 00:00:46,730 --> 00:00:48,910 private static final int. 15 00:00:48,910 --> 00:00:53,350 Let's call it notification underscore ID and 16 00:00:53,350 --> 00:00:56,180 we'll set it equal to something kind of random like 22. 17 00:00:56,180 --> 00:00:59,610 I just double the 11 that we're using from the other service. 18 00:01:00,690 --> 00:01:02,760 Now let's look at this on handle intent method. 19 00:01:03,930 --> 00:01:07,040 We're already getting the song title in the song variable so 20 00:01:07,040 --> 00:01:09,570 we can just use that in the title for a notification. 21 00:01:09,570 --> 00:01:11,310 Let's start building one just like before. 22 00:01:11,310 --> 00:01:13,700 So before we call download song, let's add a few lines and 23 00:01:13,700 --> 00:01:16,860 we'll say notification.Builder, 24 00:01:16,860 --> 00:01:21,800 let's call it builder equals a newNotification.Builder and 25 00:01:21,800 --> 00:01:25,229 for the parameter it needs the context which is this. 26 00:01:26,270 --> 00:01:27,440 Let's chain methods together again. 27 00:01:27,440 --> 00:01:32,750 So on the next line let's call, .set Small Icon, and we'll use the same icon 28 00:01:32,750 --> 00:01:39,325 that we swapped out in the last service R.drawable.ic queue music white. 29 00:01:39,325 --> 00:01:43,725 Next let's set the content title to downloading. 30 00:01:45,745 --> 00:01:49,545 And then we'll set the content text as the song title 31 00:01:49,545 --> 00:01:50,805 which we have in the song variable. 32 00:01:52,155 --> 00:01:55,275 These three pieces of information, an icon, a title, and 33 00:01:55,275 --> 00:01:59,390 some supporting text are the minimum required data for notifications. 34 00:02:01,060 --> 00:02:05,060 Next we need to set our notification manager from the system service used for 35 00:02:05,060 --> 00:02:10,050 notifications am notification manager equals and we're going to need a cast so 36 00:02:10,050 --> 00:02:12,055 it's a cast to notification manager. 37 00:02:12,055 --> 00:02:19,590 We'll call get system service and the name is from contacts.notification service. 38 00:02:19,590 --> 00:02:22,820 I'm going to drop this to a new line so it all shows up here on my screen. 39 00:02:24,070 --> 00:02:27,425 Finally we can start the notification by calling the notify method so 40 00:02:27,425 --> 00:02:29,260 mNotificationManager.notify. 41 00:02:29,260 --> 00:02:31,752 We need to pass in that ID, 42 00:02:31,752 --> 00:02:35,350 notification ID and we need to pass in the notification itself. 43 00:02:35,350 --> 00:02:38,068 We haven't built it yet but we can just do it right here we can say builder. 44 00:02:38,068 --> 00:02:39,424 .build. 45 00:02:41,584 --> 00:02:43,780 All right, let's try this and see if it works. 46 00:02:43,780 --> 00:02:48,160 Actually before we do I'm going to speed up these fake downloads by 47 00:02:48,160 --> 00:02:51,692 changing 10 to 2 here in our endTime calculation. 48 00:02:51,692 --> 00:02:52,839 Okay, now let's run it. 49 00:02:56,716 --> 00:03:00,790 Okay, so now we can click on the download button and we see our notification, cool. 50 00:03:00,790 --> 00:03:01,930 And if we pull this down. 51 00:03:03,300 --> 00:03:03,830 Got a bug. 52 00:03:03,830 --> 00:03:05,460 We have the icon and the title. 53 00:03:05,460 --> 00:03:07,610 But we don't have the song title. 54 00:03:07,610 --> 00:03:09,010 Let's take a look and see what's going on. 55 00:03:10,510 --> 00:03:14,970 So if we come back up here, we're setting the song as the extra. 56 00:03:14,970 --> 00:03:15,990 But it's a string extra. 57 00:03:15,990 --> 00:03:17,150 I think I know what's happening. 58 00:03:17,150 --> 00:03:18,050 So back in main activity, 59 00:03:18,050 --> 00:03:22,850 if you remember, we switched it to pass in a whole song instead of just a string. 60 00:03:24,060 --> 00:03:26,770 So Let's go to download songs, and 61 00:03:26,770 --> 00:03:32,820 here we have a loop running through all of the songs and 62 00:03:32,820 --> 00:03:38,210 it's trying to put it as an extra as a whole song object, that's cool, 63 00:03:38,210 --> 00:03:42,630 so back in our intent service, we need to change this to get a song variable, 64 00:03:42,630 --> 00:03:47,630 and you need to change this method to get Parcelable Extra. 65 00:03:47,630 --> 00:03:51,690 Now we can call song.get title where we need it. 66 00:03:51,690 --> 00:03:54,710 And we'll pass in just the string title here because that's all that's 67 00:03:54,710 --> 00:03:59,370 required for this download song method song.getTitle. 68 00:03:59,370 --> 00:04:00,530 Okay, let's try this again. 69 00:04:03,580 --> 00:04:04,930 Okay, we'll go for a download. 70 00:04:06,240 --> 00:04:07,070 And this time. 71 00:04:07,070 --> 00:04:07,710 Okay, there we go. 72 00:04:07,710 --> 00:04:08,760 We see it cycling through. 73 00:04:08,760 --> 00:04:13,870 It's downloading each song as it works through the loop. 74 00:04:13,870 --> 00:04:18,270 But notice that it doesn't disappear at the end when all the downloads are done. 75 00:04:18,270 --> 00:04:22,200 We need to cancel each notification when each download is complete. 76 00:04:22,200 --> 00:04:24,840 We'll do it by using the notification manager, 77 00:04:24,840 --> 00:04:27,120 here at the end of the download song method. 78 00:04:28,120 --> 00:04:32,090 Let's add a line and say mNotificationManager.cancel we 79 00:04:33,810 --> 00:04:38,310 need to pass in the ID which we already set as notification ID. 80 00:04:38,310 --> 00:04:41,460 That's how we can manage a specific notification by giving it an ID. 81 00:04:42,620 --> 00:04:43,580 And I want to show one more thing. 82 00:04:43,580 --> 00:04:48,710 Up here we're building it, let's change one more method let's call, .setProgress, 83 00:04:48,710 --> 00:04:52,660 and pass in the parameters 0, 0, and true. 84 00:04:52,660 --> 00:04:56,660 This can be customized to show a calculated value but by using 00 and 85 00:04:56,660 --> 00:04:59,870 true, we can create an indeterminate progress bar. 86 00:04:59,870 --> 00:05:01,210 So here let's watch how it works. 87 00:05:05,320 --> 00:05:09,070 This time as we download things get the notification and 88 00:05:09,070 --> 00:05:12,330 we will see a download bar showing that it's doing some activity. 89 00:05:14,810 --> 00:05:18,950 And if we wait for the last one it will disappear when all the downloads are done. 90 00:05:18,950 --> 00:05:20,500 Cool. It's working. 91 00:05:20,500 --> 00:05:23,630 All right, so as extra practice why don't you make it so that the user can 92 00:05:23,630 --> 00:05:27,410 tap on these notifications and view a specific song in the detail activity.