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