Reader small image

You're reading from  Android Studio 4.1 Development Essentials – Java Edition

Product typeBook
Published inMay 2021
PublisherPackt
ISBN-139781801815161
Edition1st Edition
Right arrow
Author (1)
Neil Smyth
Neil Smyth
author image
Neil Smyth

Neil Smyth has over 25 years of experience in the IT industry, including roles in software development and enterprise-level UNIX and Linux system administration. In addition to a bachelor’s degree in information technology, he also holds A+, Security+, Network+, Project+, and Microsoft Certified Professional certifications and is a CIW Database Design Specialist. Neil is the co-founder and CEO of Payload Media, Inc. (a technical content publishing company), and the author of the Essentials range of programming and system administration books.
Read more about Neil Smyth

Right arrow

63. An Android Direct Reply Notification Tutorial

Direct reply is a feature introduced in Android 7 that allows the user to enter text into a notification and send it to the app associated with that notification. This allows the user to reply to a message in the notification without the need to launch an activity within the app. This chapter will build on the knowledge gained in the previous chapter to create an example app that makes use of this notification feature.

63.1 Creating the DirectReply Project

Select the Create New Project quick start option from the welcome screen and, within the resulting new project dialog, choose the Empty Activity template before clicking on the Next button.

Enter DirectReply into the Name field and specify com.ebookfrenzy.directreply as the package name. Before clicking on the Finish button, change the Minimum API level setting to API 26: Android 8.0 (Oreo) and the Language menu to Java.

Designing the User Interface

Load the activity_main.xml layout file into the layout tool. With Autoconnect enabled, add a Button object beneath the existing “Hello World!” label as shown in Figure 63-1. With the Button widget selected in the layout, use the Attributes tool window to set the onClick property to call a method named sendNotification. If necessary, use the Infer Constraints button to add any missing constraints to the layout. Before continuing, select the “Hello World!” TextView...

63.2 Creating the Notification Channel

As with the example in the previous chapter, a channel must be created before a notification can be sent. Edit the MainActivity.java file and add code to create a new channel as follows:

.

.

import android.app.NotificationChannel;

import android.app.NotificationManager;

import android.content.Context;

import android.graphics.Color;

.

.

public class MainActivity extends AppCompatActivity {

 

    private NotificationManager notificationManager;

    private final String channelID = "com.ebookfrenzy.directreply.news";

 

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_direct_reply);

 

   &...

63.3 Building the RemoteInput Object

The key element that makes direct reply in-line text possible within a notification is the RemoteInput class. The previous chapters introduced the PendingIntent class and explained the way in which it allows one application to create an intent and then grant other applications or services the ability to launch that intent from outside the original app. In that chapter, entitled “An Android Notifications Tutorial”, a pending intent was created that allowed an activity in the original app to be launched from within a notification. The RemoteInput class allows a request for user input to be included in the PendingIntent object along with the intent. When the intent within the PendingIntent object is triggered, for example launching an activity, that activity is also passed any input provided by the user.

The first step in implementing direct reply within a notification is to create the RemoteInput object. This is achieved using the...

63.4 Creating the PendingIntent

The steps to creating the PendingIntent are the same as those outlined in the “An Android Notifications Tutorial” chapter, with the exception that the intent will be configured to launch MainActivity. Remaining within the MainActivity.java file, add the code to create the PendingIntent as follows:

public void sendNotification(View view) {

 

    String replyLabel = "Enter your reply here";

    RemoteInput remoteInput =

            new RemoteInput.Builder(KEY_TEXT_REPLY)

            .setLabel(replyLabel)

            .build();

 

    Intent resultIntent = new Intent(this, MainActivity.class);

 

    PendingIntent resultPendingIntent...

63.5 Creating the Reply Action

The in-line reply will be accessible within the notification via an action button. This action now needs to be created and configured with an icon, a label to appear on the button, the PendingIntent object and the RemoteInput object. Modify the sendNotification() method to add the code to create this action:

.

.

import android.graphics.drawable.Icon;

import android.app.Notification;

import android.graphics.Color;

import androidx.core.content.ContextCompat;

.

.

public void sendNotification(View view) {

 

    String replyLabel = "Enter your reply here";

    RemoteInput remoteInput =

            new RemoteInput.Builder(KEY_TEXT_REPLY)

            .setLabel(replyLabel)

            ...

63.6 Receiving Direct Reply Input

Now that the notification is successfully seeking input from the user, the app needs to do something with that input. The goal of this particular tutorial is to have the text entered by the user into the notification appear on the TextView widget in the activity user interface.

When the user enters text and taps the send button the MainActivity is launched via the intent contained in the PendingIntent object. Embedded in this intent is the text entered by the user via the notification. Within the onCreate() method of the activity, a call to the getIntent() method will return a copy of the intent that launched the activity. Passing this through to the RemoteInput.getResultsFromIntent() method will, in turn, return a Bundle object containing the reply text which can be extracted and assigned to the TextView widget. This results in a modified onCreate() method within the MainActivity.java file which reads as follows:

.

.

import android.widget...

63.7 Updating the Notification

After sending the reply within the notification you may have noticed that the progress indicator continues to spin within the notification panel as highlighted in Figure 63-4:

Figure 63-4

The notification is showing this indicator because it is waiting for a response from the activity confirming receipt of the sent text. The recommended approach to performing this task is to update the notification with a new message indicating that the reply has been received and handled. Since the original notification was assigned an ID when it was issued, this can be used once again to perform an update. Add the following code to the handleIntent() method to perform this task:

private void handleIntent() {

 

    Intent intent = this.getIntent();

 

    Bundle remoteInput = RemoteInput.getResultsFromIntent(intent);

 

    if (remoteInput != null) {

 

...

63.8 Summary

The direct reply notification feature allows text to be entered by the user within a notification and passed via an intent to an activity of the corresponding application. Direct reply is made possible by the RemoteInput class, an instance of which can be embedded within an action and bundled with the notification. When working with direct reply notifications, it is important to let the NotificationManager service know that the reply has been received and processed. The best way to achieve this is to simply update the notification message using the notification ID provided when the notification was first issued.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Android Studio 4.1 Development Essentials – Java Edition
Published in: May 2021Publisher: PacktISBN-13: 9781801815161
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at £13.99/month. Cancel anytime

Author (1)

author image
Neil Smyth

Neil Smyth has over 25 years of experience in the IT industry, including roles in software development and enterprise-level UNIX and Linux system administration. In addition to a bachelor’s degree in information technology, he also holds A+, Security+, Network+, Project+, and Microsoft Certified Professional certifications and is a CIW Database Design Specialist. Neil is the co-founder and CEO of Payload Media, Inc. (a technical content publishing company), and the author of the Essentials range of programming and system administration books.
Read more about Neil Smyth