Android Direct Response Notification

Keywords: Android Google Session SDK

Android Direct Response Notification

Direct response to notification

Android N/7.0

Preface

Notification can be a feature of Android system. From the first version of Android to Android N, Notification has changed a lot. Almost every version of Google has updated and upgraded the notification module, which shows that notification plays an important role in Android system. In Android N, the notification module is updated as follows:

  • Template Update: We are updating the notification template, with a new emphasis on heroic images and avatars. Developers will be able to take full advantage of the new template with only a small amount of code tweaking.
  • Message delivery style customization: You can customize more user interface tags related to your notifications using the MessagingStyle class. You can configure messages, session titles, and content views.
  • Bundled notifications: The system can group messages together (for example, by message topic) and display groups. Users can appropriately reject or file operations. If you have implemented Android Wear notifications, you are already familiar with this pattern.
  • Direct reply: For real-time communication applications, Android supports inline reply so that users can quickly reply to short messages directly in the notification interface.
  • Custom Views: Two new API s allow you to make full use of system decorative elements, such as notification headings and operations, when using custom views in notifications.

Apart from the fourth point of direct reply, all updates are related to UI aesthetics. This article will elaborate on how to use Android N notification module's new function notification direct reply. Before explaining this function, we should first understand the major changes in UI. The whole notification style can be said to have undergone tremendous changes. The left picture is the old version of the notification style, and the right one is the N version of the notification style.

The same notification shows such a big difference between Android M and Android N. Android N has an additional application name. This is a great update, which allows users to quickly identify which APP notification is, and greatly improves the user experience.

When you press the notification, the left picture is the old version of Android M, and the right picture is the Android N style:

As can be seen from the figure above, Android N provides more direct and fast notification settings when long-press notifications are available, and now notifications for Block system-level applications are also available. These are great updates to the user experience.

Okay, let's get to the point, if you send a direct reply notice.

Create notification

Because this article is based on the function development of Android N, so some classes and methods used in this paper are based on API level 24 of SDK. If in the actual development, if the reader develops the APP in a low version of Android system, the reader needs to consider compatibility and can not directly quote the code of the examples in this article. For example, Notification.Builder (Android 3.0) is used in this article. In order to be compatible with low versions, Notification Compat. Builder in Version 4 support library should be used.

First, let's review how to create a common notification. To create a notification, some content is optional and some content is not optional. Therefore, the necessary notification content is:

  • Small icon, set by setSmallIcon()
  • Title, set by setContentTitle()
  • Detailed text, set by setContentText()

Let's start now.

Create notification objects

Using notification builder pattern, notification objects can be easily created as follows:

Notification notification = new Notification.Builder(this)
        .setSmallIcon(Icon.createWithResource(this.getPackageName(),R.drawable.my_default_icon_label))
        .setContentText("You can reply on notification.")
        .setContentTitle("Test Notification")
        .build();

As in the above code, this is a Notification object that contains only the necessary content. Because these codes are very common, this article will not go into too much detail.

Add an Action

In version 4.4 of Android, the update and upgrade of notification adds the Action function, so-called Action function, that is, notification has one or more buttons that can be clicked. The following picture:

The implementation code calls the addAction() method as follows:

Notification notification = new Notification.Builder(this)
    .setSmallIcon(Icon.createWithResource(this.getPackageName(), R.drawable.my_default_icon_label))
    .setContentText("You can reply on notification.")
    .setContentTitle("Test Notification")
    .setAutoCancel(true)
    .addAction(
            new Notification.Action.Builder(
                    null,
                    "My Action",
                    null)
                    .build())
    .build();

Add a Broadcast Receiving Action Event

When you click on the Action button of the notification, send the click event to a broadcasting receiver. The code is as follows:

//Create an Intent to start broadcasting
Intent quickIntent = new Intent();
quickIntent.setAction("quick.reply.input");
//Create Notification Objects
Notification notification = new Notification.Builder(this)
    .setSmallIcon(Icon.createWithResource(this.getPackageName(), R.drawable.my_default_icon_label))
    .setContentText("You can reply on notification.")
    .setContentTitle("Test Notification")
    .setAutoCancel(true)//Automatically disappears when you click on the notification
    .addAction(
            new Notification.Action.Builder(
                    null,
                    "MyAction",
                    //Converting Intent to Pending Intent
                    PendingIntent.getBroadcast(this, 1, quickIntent,
                            PendingIntent.FLAG_ONE_SHOT))
                    .build())
    .build();
//Create a broadcast receiver
BroadcastReceiver br = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {

        unregisterReceiver(this);
    }
};
//Registered Broadcasting Receiver
IntentFilter filter = new IntentFilter();
filter.addCategory(this.getPackageName());
filter.addAction("quick.reply.input");
registerReceiver(br, filter);

Add Direct Response Input Box

Add a direct reply input box, call the addRemoteInput() method of Notification.Action.Builder, and create a RemoteInput object as a parameter. When RemoteInput.Builder creates the object, it passes in the parameter key, which is the key of the content of the input box and gets the content through the key where it receives the content. If there is no key to get content, you need to first traverse all the data, first get the key, then get the content. As follows:

Notification notification = new Notification.Builder(this)
        .setSmallIcon(Icon.createWithResource(this.getPackageName(),R.drawable.my_default_icon_label))
        .setContentText("You can reply on notification.")
        .setContentTitle("Test Notification")
        .setAutoCancel(true)
        .addAction(
                new Notification.Action.Builder(
                        null,
                        "MyAction",
                        PendingIntent.getBroadcast(this, 1, quickIntent,
                                PendingIntent.FLAG_ONE_SHOT))
                        //Reply directly to the input box, quick_notification_reply is key
                        .addRemoteInput(new RemoteInput.Builder("quick_notification_reply")
                            .setLabel("Please input here!").build())
                        .build())
        .build();

The effect is as follows:

Send the contents of the direct reply to the broadcasting receiver

Receive the content of notification input box in broadcasting and display it on the TextView of APP. The implementation is as follows:

BroadcastReceiver br = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle results = RemoteInput.getResultsFromIntent(intent);
        if (results != null) {
            CharSequence result = results.getCharSequence("quick_notification_reply");
            if (TextUtils.isEmpty(result)) {
                ((TextView) findViewById(R.id.tv)).setText("no content");
            } else {
                ((TextView) findViewById(R.id.tv)).setText(result);
            }
        }
        nm.cancelAll();
        unregisterReceiver(this);
    }
};

As in the above code, through the getResultsFromIntent() method of RemoteInput, the content of the input box in Intent is taken out, and then the content of the input box is obtained by the method getCharSequence() through key, and the content is displayed on TextView.

The results are as follows:

Bring great user experience with Hands Up

Android updates the function of direct reply notification, in order to give users a convenient and fast operation experience, but users also need to drop down the notification bar to reply. If users are using devices, the convenience of direct reply notification can be fully reflected through Heands Up.

Combined with Hands Up, only need to set setFullScreenIntent() on the original basis, to achieve the following:

Notification notification = new Notification.Builder(this)
        .setSmallIcon(Icon.createWithResource(this.getPackageName(), R.drawable.my_default_icon_label))
        .setContentText("You can reply on notification.")
        .setContentTitle("Test Notification")
        .setAutoCancel(true)
        //Hands Up
        .setFullScreenIntent(PendingIntent.getActivity(this, 1,                     
            quickIntent,PendingIntent.FLAG_ONE_SHOT), true)
        .addAction(
                new Notification.Action.Builder(
                        null,
                        "MyAction",
                        PendingIntent.getBroadcast(this, 1, quickIntent,
                                PendingIntent.FLAG_ONE_SHOT))
                        //Direct response input box
                        .addRemoteInput(new RemoteInput.Builder("quick_notification_reply").setLabel("Please input here!").build())
                        .build())
        .build();

The results are as follows:

summary

This paper describes the new function of Android N, which directly replies to notifications. This function brings convenience and fast operation to users. It is very useful for instant messaging, short message and other functions. By combining Hands Up notification, the benefits of direct reply can be really brought into play, which undoubtedly brings great experience to users. However, at present, rapid response notifications only support text, that is, plain text, and hope to have more rich types in the future.

Posted by jovankau on Wed, 03 Apr 2019 12:00:29 -0700