speaker: how manysteps should be between receiving anotification and acting on it? when notification actionswere introduced in jelly bean, this number dropped to one. just tap an action right there.
Android notification, direct reply seeks tomake it just as easy to receive text input fromthe user right in line in the notification. this involves superpoweringone or more of the actions
you add to your notificationwith a remoteinput. this is how the system knowsto ask for input from the user, then forward it on to your app. this is actuallythe exact same api used to add voice input to yournotifications on android wear, but it's now availableon phones and tablets. so we'll have anotification with an action with a remoteinput. the remoteinput is what storesinformation like the hint
text-- the text that appearsbefore the user types anything in-- and the key,a unique string used to later retrievewhat the user typed in. the action here ispretty standard. of course, we'll want touse addremoteinput to add our remoteinput object. there's also asetallowgeneratedreplies method if you want to enable smartreply on android wear 2.0 watches.
we are trying to make thingseasy for users, right? totally worth enabling. there is one otherthing we need to be aware of-- the pendingintentwe're passing in here. turns out what kindof pendingintent you pass in matters a lot. for direct reply, the systemis doing the ui portion for us, so using a serviceor broadcastreceiver is exactly what we need.
this also allows it to workeven when the device is locked. awesome. but on devices that don'tsupport direct reply, you need to start your ownui-- namely, an activity. avoid trying tostart an activity from a service orbroadcastreceiver pendingintent. the notification shadewon't automatically dismiss, and users will need to manuallyunlock a locked device,
leaving users unsure ifanything actually happened. so we'll want to use anactivity on marshmallow and lower devices and theservice or broadcastreceiver for our new directreply supported devices. now, on to thenotification itself. the best part, directreply doesn't care about your notification at all. as long as you add theaction, you're good to go. of course, we did builda fancy messagingstyle.
so if you're amessaging app, these two go perfectly together. magical. so let's say we're readyto receive our reply and chosen abroadcastreceiver, appropriate if we're ok with onlyusing the main thread. we can then usecodes such as this to extract the text fromthe intent passed in. you'll note we use remoteinputgetresultsfromintent
to get a bundle, andthen use that key we passed in to the remoteinputhere to retrieve the text. of course, if youstop here, you'll notice your remoteinput justkeeps on spinning forever. so there's one last step--updating your notification. updating thenotification is how you tell the user thetext has been saved or the message has been sent. for example, if you'reusing messagingstyle,
you just append yourmessage 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 bottomof the notification. now, keep in mind, isaid update, not cancel. you want to tell theuser it succeeded, rather than leavingthem hanging. so add a remoteinput toyour notifications actions
to enable direct reply.
check out all the linksin the description for more information onhow it all works as you continue to build better apps. [music playing]