Adding Text to a Notification4:52 with Ben Jakuben
Notifications are another type of communication in Android that is similar in spirit to Broadcast Intents and Receivers. Local notifications show up in the notification drawer and display some information to the user, and optionally trigger some actions, too.
We first covered Notifications here at Treehouse in the Threads and Services course. To familiarize yourself with the initial setup, check out the Foreground Services video, where Ben Deitch sets up a local notification for a foreground service.
[MUSIC] 0:00 [SOUND] Let's change gears a little bit and 0:04 talk about a different kind of communication. 0:06 Notifications, you probably know a little bit about using Notifications from earlier 0:08 work here at Treehouse. 0:13 As usual check the teacher's notes for links but 0:14 will cover it pretty well in this last section. 0:17 Let's take another look at the table we've been using in this course. 0:20 This last row shows an example of when we might want a notification. 0:23 When a service needs to send an update to the user. 0:27 In this example we'll extend an existing notification that shows in the drawer 0:30 when the user is playing a song. 0:35 Note in this list that we don't talk about using notifications 0:38 from an activity to the system. 0:41 We don't need a notification in that scenario because the activity's 0:42 already visible to the user and we can use a stacked bar or 0:45 some other in app mechanism for alerting the user to something. 0:49 Let's start here by taking a look at the documentation about notifications. 0:53 First part of a notification is the icon that appears 0:57 in the notification bar at the top. 1:00 Then when the user pulls the drawer down, the notification has its own 1:02 row in the drawer and we can even add actions to them like this. 1:06 This app is currently using a notification in the player service. 1:10 So let's check it out. 1:14 So here in the onStartCommand method we are using the builder pattern from 1:16 the notification class to build a simple notification. 1:21 All it does is display our app's icon when a song is playing and 1:24 then it goes away when the service stops playing music. 1:27 I think it would be nice to show the title of the song playing. 1:29 So let's see how to do that. 1:32 First the notification builder has a fluent interface. 1:34 So let's delete this semi-colon and chain some of these things together. 1:38 I'm gonna get rid of notification builder, tab over and 1:43 now at the end of this I can append another line .setContentTitle. 1:47 Okay how do we get the song title in here. 1:54 We need to pass in a song when this service is started. 1:57 Let's go back to Main Activity. 2:01 We're starting this service when the play button is tapped. 2:03 So let's look at the On Click listener we're going to scroll all the way up 2:06 to on create, and here we have mplay button and 2:10 in this listener is where we are starting the service. 2:14 All we need to do is add the song as an extra to this intent. 2:17 So we say, intent.putExtra. 2:22 We can use the EXTRA key word SONG, and 2:24 now for the song itself, we can just pass in the first song in our playlist. 2:29 Our playlist itself isn't fully functional. 2:34 Though that would be an interesting exercise if you'd like to extend this and 2:36 make it play everything in a given list. 2:39 Anyhow, let's type Playlist.songs, and 2:41 get the first one at index zero. 2:45 Now a challenge for you. 2:49 Pause me and see if you can set the song and artist back in the service. 2:51 Use autocomplete to help you figure out the additional text field 2:54 you might use for the artist name. 2:57 Okay did you get it? 3:01 I'm going to go back and 3:01 let's start with some place holders here before we build our notification. 3:03 We have a string called title and we'll initialize it to a blank string. 3:08 And then a string called artist also initialized to a blank value. 3:13 I'm setting these to blank strings because I'm going to do a null check and 3:18 I'll only update them if a song is passed in. 3:22 I don't want the app to crash if song data is missing. 3:24 So for the null check, if (intent.getParcelableExtra) and 3:27 then we can use MainActivity.EXTRA_SONG. 3:32 If that's not equal to null, then we can get a song and set title and artist. 3:37 So first, we have Song song = intent.getParcelableExtra 3:43 MainActivity EXTRA_SONG. 3:50 Now we can set title equal to song.getTitle and 3:53 artist equal to song.getArtist. 3:57 Now we can use these in our notification builder. 4:01 So for ContentTitle let's pass in title. 4:04 And then I'm going to chain the artist with .setContentText, and pass in artist. 4:07 And one more thing, 4:16 our regular app icon doesn't look very good as a notification icon. 4:17 So I downloaded another one. 4:20 Let's call R.Drawable.ic_queue_music_white. 4:23 Okay, let's try it out. 4:27 Okay, so we want to play some music. 4:32 There we go. 4:36 And look, there's our notification showing up, and if we pull it down. 4:37 Hey it's showing the icon, the title, and the artist. 4:40 Cool. 4:44 It would be really nice to open our app directly from this notification. 4:45 Let's try to do that in the next video. 4:49
You need to sign up for Treehouse in order to download course files.Sign up