This course will be retired on June 1, 2025.
Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
Now that we have a fully formed notification, the next thing we want to tackle is how to open our app from the notification itself.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
Let's take another look
at the notification docs.
0:00
So here in the nav, we see a section for
a notification actions.
0:03
Click on it, and now this talks about
adding specific actions to a notification,
0:07
like opening an app or snoozing an alarm.
0:12
Check out this paragraph here.
0:15
Inside a Notification, the action
itself is defined by a PendingIntent
0:17
containing an Intent that starts
an Activity in your application.
0:22
Let's go try that out.
0:26
We'll start here in onStartCommand
by creating a pending intent.
0:28
PendingIntent and
we'll call it pendingIntent.
0:32
We're very creative with our names.
0:35
And we'll set it with PendingIntent dot,
and the method we choose here
0:37
needs to correspond to the type
of action we want to perform.
0:42
In this case,
we want to start an activity in our app.
0:46
So let's pick getActivity.
0:48
This takes four parameters,
the context, this,
0:52
a request code,
I'm going to drop down to a new line, and
0:55
let's call this REQUEST_OPEN,
we'll define that in a moment.
0:58
And then we need an explicit intent and
1:02
some optional intent flags,
let's come back to those.
1:04
For REQUEST_OPEN, I'm going to hit
Alt+Enter and create the constant field.
1:08
We do want it to be an Int, so let's
define it as some distinct Int value.
1:13
I'm going to use 99.
1:17
So now back here where we're defining
the intent, we need an explicit intent.
1:21
So above our pending intent, let's add
Intent mainIntent equals a new intent
1:26
with this as the context and
MainActivity as the explicit class.
1:32
Now we can use it for the third
parameter here, mainIntent, and for
1:38
the flags, let's just pass in 0.
1:42
We could pass in a flag constant from
the Intent class, but 0 works fine here.
1:44
Now we need to add this pending
intent to our notification.
1:50
We can chain another
method call down here and
1:53
add .setContentIntent
with our pending intent.
1:55
All right, let's run it and
see what happens.
2:02
Okay, so now if we play our song,
all right, first thing we see,
2:06
the notification up here.
2:08
Let's go to the home screen.
2:09
And now if we bring the notification
drawer down and tap on it, hooray,
2:12
we're taken back to our app.
2:16
MainActivity is displayed, cool.
2:18
There's one more cool thing with this too.
2:20
Now we can open our app
from the lock screen.
2:22
Now, I'm not sure exactly why, but this
emulator is not going to the lock screen.
2:25
Usually you can hit the power button,
put it to sleep, and
2:29
then when you hit the power button again,
you're taken to the lock screen.
2:32
So your emulator may work that way,
if not,
2:36
you can see how it looks
on an actual device.
2:38
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up