SPEAKER: How many steps should be
between receiving a notification and acting on it?
When notification actions were introduced in Jelly Bean,
this number dropped to one.
Just tap an action right there.
Direct Reply seeks to make it just as easy
to receive text input from the user right in line
in the notification.
This involves superpowering one or more of the actions
you add to your notification with a RemoteInput.
This is how the system knows to ask for input from the user,
then forward it on to your app.
This is actually the exact same API
used to add voice input to your notifications on Android Wear,
but it's now available on phones and tablets.
So we'll have a notification with an action
with a RemoteInput.
The RemoteInput is what stores information like the hint
text-- the text that appears before the user types anything
in-- and the key, a unique string
used to later retrieve what the user typed in.
The action here is pretty standard.
Of course, we'll want to use addRemoteInput to add
our RemoteInput object.
There's also a setAllowGeneratedReplies method
if you want to enable Smart Reply on Android Wear 2.0
watches.
We are trying to make things easy for users, right?
Totally worth enabling.
There is one other thing we need to be
aware of-- the PendingIntent we're passing in here.
Turns out what kind of PendingIntent
you pass in matters a lot.
For Direct Reply, the system is doing the UI portion for us,
so using a Service or BroadcastReceiver
is exactly what we need.
This also allows it to work even when the device is locked.
Awesome.
But on devices that don't support Direct Reply,
you need to start your own UI-- namely, an Activity.
Avoid trying to start an Activity
from a Service or BroadcastReceiver
PendingIntent.
The notification shade won't automatically dismiss,
and users will need to manually unlock a locked device,
leaving users unsure if anything actually happened.
So we'll want to use an Activity on Marshmallow
and lower devices and the Service or BroadcastReceiver
for our new Direct Reply supported devices.
Now, on to the notification itself.
The best part, Direct Reply doesn't care
about your notification at all.
As long as you add the action, you're good to go.
Of course, we did build a fancy MessagingStyle.
So if you're a messaging app, these two
go perfectly together.
Magical.
So let's say we're ready to receive our reply
and chosen a BroadcastReceiver, appropriate
if we're OK with only using the main thread.
We can then use codes such as this
to extract the text from the intent passed in.
You'll note we use remoteInput getResultsFromIntent
to get a bundle, and then use that key we
passed in to the remoteInput here to retrieve the text.
Of course, if you stop here, you'll
notice your remoteInput just keeps on spinning forever.
So there's one last step-- updating your notification.
Updating the notification is how you
tell the user the text has been saved
or the message has been sent.
For example, if you're using MessagingStyle,
you just append your message to the style
and rebuild your notifications.
For other styles, you can and should
use setRemoteInputHistory, which will append the message
or messages to the bottom of the notification.
Now, keep in mind, I said update, not cancel.
You want to tell the user it succeeded,
rather than leaving them hanging.
So add a remoteInput to your notifications actions
to enable Direct Reply.
Check out all the links in the description
for more information on how it all works as you
continue to build better apps.
[MUSIC PLAYING]