1 00:00:00,000 --> 00:00:04,227 [MUSIC] 2 00:00:04,227 --> 00:00:06,384 [SOUND] Let's change gears a little bit and 3 00:00:06,384 --> 00:00:08,859 talk about a different kind of communication. 4 00:00:08,859 --> 00:00:13,299 Notifications, you probably know a little bit about using Notifications from earlier 5 00:00:13,299 --> 00:00:14,970 work here at Treehouse. 6 00:00:14,970 --> 00:00:17,320 As usual check the teacher's notes for links but 7 00:00:17,320 --> 00:00:19,360 will cover it pretty well in this last section. 8 00:00:20,710 --> 00:00:23,870 Let's take another look at the table we've been using in this course. 9 00:00:23,870 --> 00:00:27,500 This last row shows an example of when we might want a notification. 10 00:00:27,500 --> 00:00:30,780 When a service needs to send an update to the user. 11 00:00:30,780 --> 00:00:35,010 In this example we'll extend an existing notification that shows in the drawer 12 00:00:35,010 --> 00:00:36,350 when the user is playing a song. 13 00:00:38,050 --> 00:00:41,040 Note in this list that we don't talk about using notifications 14 00:00:41,040 --> 00:00:42,750 from an activity to the system. 15 00:00:42,750 --> 00:00:45,670 We don't need a notification in that scenario because the activity's 16 00:00:45,670 --> 00:00:49,240 already visible to the user and we can use a stacked bar or 17 00:00:49,240 --> 00:00:52,260 some other in app mechanism for alerting the user to something. 18 00:00:53,750 --> 00:00:57,430 Let's start here by taking a look at the documentation about notifications. 19 00:00:57,430 --> 00:01:00,420 First part of a notification is the icon that appears 20 00:01:00,420 --> 00:01:02,660 in the notification bar at the top. 21 00:01:02,660 --> 00:01:06,150 Then when the user pulls the drawer down, the notification has its own 22 00:01:06,150 --> 00:01:09,250 row in the drawer and we can even add actions to them like this. 23 00:01:10,850 --> 00:01:14,680 This app is currently using a notification in the player service. 24 00:01:14,680 --> 00:01:15,430 So let's check it out. 25 00:01:16,750 --> 00:01:21,110 So here in the onStartCommand method we are using the builder pattern from 26 00:01:21,110 --> 00:01:24,490 the notification class to build a simple notification. 27 00:01:24,490 --> 00:01:27,410 All it does is display our app's icon when a song is playing and 28 00:01:27,410 --> 00:01:29,478 then it goes away when the service stops playing music. 29 00:01:29,478 --> 00:01:32,940 I think it would be nice to show the title of the song playing. 30 00:01:32,940 --> 00:01:34,950 So let's see how to do that. 31 00:01:34,950 --> 00:01:38,300 First the notification builder has a fluent interface. 32 00:01:38,300 --> 00:01:43,375 So let's delete this semi-colon and chain some of these things together. 33 00:01:43,375 --> 00:01:47,623 I'm gonna get rid of notification builder, tab over and 34 00:01:47,623 --> 00:01:52,680 now at the end of this I can append another line .setContentTitle. 35 00:01:54,910 --> 00:01:57,470 Okay how do we get the song title in here. 36 00:01:57,470 --> 00:01:59,820 We need to pass in a song when this service is started. 37 00:02:01,000 --> 00:02:02,430 Let's go back to Main Activity. 38 00:02:03,910 --> 00:02:06,450 We're starting this service when the play button is tapped. 39 00:02:06,450 --> 00:02:10,060 So let's look at the On Click listener we're going to scroll all the way up 40 00:02:10,060 --> 00:02:14,060 to on create, and here we have mplay button and 41 00:02:14,060 --> 00:02:16,170 in this listener is where we are starting the service. 42 00:02:17,440 --> 00:02:20,660 All we need to do is add the song as an extra to this intent. 43 00:02:22,010 --> 00:02:24,690 So we say, intent.putExtra. 44 00:02:24,690 --> 00:02:29,110 We can use the EXTRA key word SONG, and 45 00:02:29,110 --> 00:02:33,250 now for the song itself, we can just pass in the first song in our playlist. 46 00:02:34,740 --> 00:02:36,450 Our playlist itself isn't fully functional. 47 00:02:36,450 --> 00:02:39,560 Though that would be an interesting exercise if you'd like to extend this and 48 00:02:39,560 --> 00:02:41,800 make it play everything in a given list. 49 00:02:41,800 --> 00:02:45,440 Anyhow, let's type Playlist.songs, and 50 00:02:45,440 --> 00:02:49,740 get the first one at index zero. 51 00:02:49,740 --> 00:02:51,700 Now a challenge for you. 52 00:02:51,700 --> 00:02:54,970 Pause me and see if you can set the song and artist back in the service. 53 00:02:54,970 --> 00:02:57,780 Use autocomplete to help you figure out the additional text field 54 00:02:57,780 --> 00:02:59,180 you might use for the artist name. 55 00:03:01,130 --> 00:03:01,740 Okay did you get it? 56 00:03:01,740 --> 00:03:03,740 I'm going to go back and 57 00:03:03,740 --> 00:03:08,720 let's start with some place holders here before we build our notification. 58 00:03:08,720 --> 00:03:13,410 We have a string called title and we'll initialize it to a blank string. 59 00:03:13,410 --> 00:03:17,110 And then a string called artist also initialized to a blank value. 60 00:03:18,520 --> 00:03:22,200 I'm setting these to blank strings because I'm going to do a null check and 61 00:03:22,200 --> 00:03:24,680 I'll only update them if a song is passed in. 62 00:03:24,680 --> 00:03:27,560 I don't want the app to crash if song data is missing. 63 00:03:27,560 --> 00:03:32,360 So for the null check, if (intent.getParcelableExtra) and 64 00:03:32,360 --> 00:03:37,060 then we can use MainActivity.EXTRA_SONG. 65 00:03:37,060 --> 00:03:43,340 If that's not equal to null, then we can get a song and set title and artist. 66 00:03:43,340 --> 00:03:50,402 So first, we have Song song = intent.getParcelableExtra 67 00:03:50,402 --> 00:03:53,910 MainActivity EXTRA_SONG. 68 00:03:53,910 --> 00:03:57,360 Now we can set title equal to song.getTitle and 69 00:03:57,360 --> 00:04:01,860 artist equal to song.getArtist. 70 00:04:01,860 --> 00:04:04,210 Now we can use these in our notification builder. 71 00:04:04,210 --> 00:04:06,190 So for ContentTitle let's pass in title. 72 00:04:07,410 --> 00:04:14,490 And then I'm going to chain the artist with .setContentText, and pass in artist. 73 00:04:16,190 --> 00:04:17,090 And one more thing, 74 00:04:17,090 --> 00:04:20,610 our regular app icon doesn't look very good as a notification icon. 75 00:04:20,610 --> 00:04:23,070 So I downloaded another one. 76 00:04:23,070 --> 00:04:26,574 Let's call R.Drawable.ic_queue_music_white. 77 00:04:27,730 --> 00:04:29,051 Okay, let's try it out. 78 00:04:32,967 --> 00:04:34,790 Okay, so we want to play some music. 79 00:04:36,630 --> 00:04:37,250 There we go. 80 00:04:37,250 --> 00:04:40,685 And look, there's our notification showing up, and if we pull it down. 81 00:04:40,685 --> 00:04:44,665 Hey it's showing the icon, the title, and the artist. 82 00:04:44,665 --> 00:04:45,645 Cool. 83 00:04:45,645 --> 00:04:49,835 It would be really nice to open our app directly from this notification. 84 00:04:49,835 --> 00:04:51,155 Let's try to do that in the next video.