149
CHAPTER 8: APIs
Here are a few notes about this code:
KEY_TEXT_REPLY is used to identify the reply text in the intent receiver.
CONVERSATION_ID is used to identify the conversation chain; here the
notification and the intent that receives the reply must know they refer to
each other.
As usual, make sure that in production code you use string resources
and appropriate text.
When the notification shows up, it will contain a Reply button, and when the user clicks
it, the system will prompt for some reply text, which is then going to be sent to the intent
receiver (messageReplyIntent in the example).
The intent receiver for the reply text might then have a receive callback that looks like this:
override fun onReceive(context: Context,
intent: Intent) {
Log.e("LOG", intent.toString())
val KEY_TEXT_REPLY = "key_text_reply"
val remoteInput = RemoteInput.
getResultsFromIntent(intent)
val txt = remoteInput?.
getCharSequence(KEY_TEXT_REPLY)?:"undefined"
val conversationId =
intent.getIntExtra("conversationId",0)
Log.e("LOG","reply text = " + txt)
// Do s.th. with the reply...
// Build a new notification, which informs the user
// that the system handled their interaction with
// the previous notification.
val NOTIFICATION_CHANNEL_ID = "1"
val repliedNotification =
NotificationCompat.Builder(context,
NOTIFICATION_CHANNEL_ID)
.setSmallIcon(android.R.drawable.ic_media_play)
.setContentText("Replied")
.build()
buildChannel(NOTIFICATION_CHANNEL_ID)
// Issue the new notification.
val notificationManager =
NotificationManagerCompat.from(context)
notificationManager.notify(conversationId,
repliedNotification)
}
150
Do'stlaringiz bilan baham: |