Notifications
are incoming messages to an android application from a server or within the application. It will be shown in the notification bar even if the app is not open. Here in this tutorial i will explain you how to create direct reply notifications.
Direct Reply Capability Of Notifications
Direct Reply is one of the coolest feature rolled out by Android N. From API Level 24, As a part of improving user experience of messaging applications Android allows developers to add reply input option to a notification. They have introduced this feature as an additional Action
to the existing Notification Builder
.
Android N Notification comes with 3 types. They are Direct Reply
, a new MessagingStyle
, and bundled notifications
respectively. Here we are going to create a Direct Reply
notification.
Here i am going to show you sample direct reply notification.
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="replynotification.direct.app.directreplynotification"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".LaunchActivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".NotificationActivity" android:label="@string/notification_activity_label" android:parentActivityName=".LaunchActivity"> <meta-data android:name="android.support.PARENT_ACTIVITY" android:value=".LaunchActivity" /> </activity> </application> </manifest>
activity_launch.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="replynotification.direct.app.directreplynotification.LaunchActivity"> <TextView android:id="@+id/replyMessage" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:gravity="center" android:text="Check Notification Drawer" android:textAppearance="@style/TextAppearance.AppCompat.Display1" android:textColor="#000" /> </RelativeLayout>
Launch Activity
Let’s start this activity by creating a notification with reply option.
import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.support.v4.app.NotificationCompat; import android.support.v4.app.RemoteInput; import android.support.v4.app.TaskStackBuilder; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; public class LaunchActivity extends AppCompatActivity { // Key for the string that's delivered in the action's intent. private static final String KEY_TEXT_REPLY = "key_text_reply"; // mRequestCode allows you to update the notification later on. int mRequestCode = 1000; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_launch); // Create the RemoteInput specifying this key String replyLabel = getString(R.string.reply_label); RemoteInput remoteInput = new RemoteInput.Builder(KEY_TEXT_REPLY) .setLabel(replyLabel) .build(); Intent resultIntent = new Intent(this, NotificationActivity.class); TaskStackBuilder stackBuilder = TaskStackBuilder.create(this); // Adds the back stack for the Intent (but not the Intent itself) stackBuilder.addParentStack(NotificationActivity.class); // Adds the Intent that starts the Activity to the top of the stack stackBuilder.addNextIntent(resultIntent); PendingIntent resultPendingIntent = stackBuilder.getPendingIntent( 0, PendingIntent.FLAG_UPDATE_CURRENT ); // Add to your action, enabling Direct Reply for it NotificationCompat.Action action = new NotificationCompat.Action.Builder(R.drawable.ic_stat_social_notifications_on, replyLabel, resultPendingIntent) .addRemoteInput(remoteInput) .setAllowGeneratedReplies(true) .build(); NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this) .addAction(action) .setAutoCancel(true) .setSmallIcon(R.drawable.ic_stat_social_notifications_on) .setContentTitle("DevDeeds Says") .setContentText("Do you like my tutorials ?"); mBuilder.setContentIntent(resultPendingIntent); NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); //Show it mNotificationManager.notify(mRequestCode, mBuilder.build()); } }
notification_activity.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_notification" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="replynotification.direct.app.directreplynotification.NotificationActivity"> <TextView android:id="@+id/replyMessage" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:gravity="center" android:text="" android:textSize="15sp" android:textColor="#000" /> </RelativeLayout>
Notification Activity
This is the opening/resulting activity when you give reply to the notification. Here we update the created notification with “Thank you” message. Updation is performed based on the request code, which is 1000.
From Intent
we will get entered message by key key_text_reply
import android.app.NotificationManager; import android.content.Context; import android.content.Intent; import android.support.v4.app.NotificationCompat; import android.support.v4.app.RemoteInput; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.TextView; public class NotificationActivity extends AppCompatActivity { // Key for the string that's delivered in the action's intent. private static final String KEY_TEXT_REPLY = "key_text_reply"; // mRequestCode allows you to update the notification. int mRequestCode = 1000; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_notification); TextView textView = (TextView) findViewById(R.id.replyMessage); textView.setText(getMessageText(getIntent())); String returnMessage = "Thank you"; NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this) .setSmallIcon(R.drawable.ic_stat_toggle_check_box) .setContentText(returnMessage); NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); //update notification mNotificationManager.notify(mRequestCode, mBuilder.build()); } private CharSequence getMessageText(Intent intent) { Bundle remoteInput = RemoteInput.getResultsFromIntent(intent); if (remoteInput != null) { return remoteInput.getCharSequence(KEY_TEXT_REPLY); } return null; } }