This article has authorized the public number: Yang (hongyangandroid) in the public platform original debut.
This is < is the time to understand the Android7> series of the third article, the previous two articles respectively introduced the Multi-window mode and shortcut function, today we come to a simple, say a notification direct reply function. Many friends may not have the opportunity to touch the new features of Android7, so before starting, to introduce what is the notification of direct response , take the SMS app, before we receive a new text message will have a notice to remind, However, if we want to reply to the text message will click the text message application or click on the notification into the text message content can be, very troublesome. Starting from Android7, we don't have to be so troublesome, we can reply directly in the notice! It seems like iOS and some native-made ROMs already have this feature, but for the first time in native Android, this feature is convenient for both users and our developers. Take a look at the picture.
This is what we want to complete today, a small demo, first of all, the demo process, first, we click on the "Send notification" will be simulated to send a text message notification, and then when we finish editing the reply content Click Send, will start a service to simulate the sending process of SMS, This simulation process first has a delay, after the delay continues to send a "send success" notification, and finally cancel the notification.
The process is simple, the whole process is simulated, the main thing is to learn a direct response to this new feature.
That's the point? How do I send a notification with a direct reply function? Let's go through the process of sending such a notification:
- In the first step, we need to create a remoteinput
- The second step is to create a pendingintent, a pendingintent that refers to what we call when we click "Send".
- Step three, create a direct reply to the action
- Fourth step, create notification
- Fifth step, send notification
In a total of 5 steps we can create a notification with a "direct reply" function, and this 5 step 2,4,5 should be familiar with the code, below we will step by step to complete the code.
First, create a remoteinput
new RemoteInput.Builder(RESULT_KEY) .setLabel("回复这条消息") .build();
Very simple builder, first builder constructs we need a string type key, what is this key for? In fact, it is convenient for us to take the content when we deal with the reply. The next setLabel
method, you can compare the above figure to see which part of the content, plainly, this is actually a EditText Hint value.
Next move on to the second step, create one PendingIntent
, and this PendingIntent
is where the function is to start a service, so the code is this:
new Intent(this, SendMsgService.class);PendingIntent pi = PendingIntent.getService(this1, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Are familiar with the code, we then look at the third step, create a direct reply to theAction
new"回复", pi) .addRemoteInput(remoteInput).build();
is a builder, the builder of the structure has three parameters, the first parameter is a logo, has made the notification of the friend must be familiar with the logo notice, the second parameter is the Action
label, corresponding to the above demo we want to click on the button, The third parameter is what to do when we click on "Reply". The next addRemoteInput
method is to pass in the one we created above RemoteInput
.
The fourth step, create a notification, this process everyone knows is also a builder, but this builder certainly has our unfamiliar method, Action
how to set up above?
new NotificationCompat.Builder(this) .setSmallIcon(R.mipmap.ic_launcher) .setContentTitle("请问是否需要信用卡?") .setContentText("您好,我是XX银行的XX经理, 请问你需要办理信用卡吗?") .setColor(Color.CYAN) .addAction(act) .setCategory(Notification.CATEGORY_MESSAGE);
From the top down to the next, slightly ... Slightly... The function of the setColor
method is to set a accent
color, corresponding to the above demo is the "Reply" and click "Reply" after the input box background color. addAction
The method is to enter the settings we created above, which is the type of the Action
setCategory
specified notification, which we specify as the message type.
Last step, send a notification
NotificationManager nm = getSystemService(NotificationManager.class);nm.notify(NOTIFICATION_ID, builder.build());
Not to say, so simple steps, a "direct response" function of the notification issued, but not finished, although the notice came out, but the user reply to the content we have not dealt with. The processing of these logic is carried out in the service mentioned above.
What about this process? or first to list the next process.
- Get what the user has entered
- Latency for analog send
- Impersonation sent successfully, cancellation notification
Again step by step with the code, first we want to get the user input content, we can go through RemoteInput
a static method getResultsFromIntent
directly to get a Bundle
, and then we can be built according to RemoteInput
the key used to get the content.
Bundle replyBundle = RemoteInput.getResultsFromIntent(intent);ifnull) { // 根据key拿到回复的内容 String reply = replyBundle.getString(MainActivity.RESULT_KEY); reply(reply);}
After we get the content, we'll simulate the response, which is done in the reply
method.
privatevoidreply(final String reply) { mHandler.postDelayed(new Runnable() { @Override publicvoidrun() { Log.d("reply""reply: " + reply); onReply(); } 1000);}}
Here we delay the 1000ms after the reply is successful and then call the onReply
method.
Private void onreply() {FinalNotificationmanager nm = Getsystemservice (Notificationmanager.class); Mhandler.post (NewRunnable () {@Override Public void Run() {//Update notification as "Reply succeeded"Notification Notification =NewNotificationcompat.builder (Sendmsgservice. This). Setsmallicon (R.mipmap.ic_launcher). Setcontenttext ("Reply Success"). build (); Nm.notify (mainactivity.notification_id, NOTIFICATION); } });//FINAL notification cancellationMhandler.postdelayed (NewRunnable () {@Override Public void Run() {nm.cancel (mainactivity.notification_id); } }, -);}
onReply
we'll do the rest in there, including sending a "send successful" message, and then delaying the cancellation of the notification.
The code is very simple, the main is to create a direct response to the notification and how to get the user input content , if you have such a requirement in the app, you can consider the adaptation of Android7 when the direct response to the function.
Finally, a digression, starting from the ANDROID5, in fact, Android also provides a floating notification function, this floating notification is actually very simple, just need to be built in Notification
the time of use setFullScreenIntent(PendingIntent pi, boolean highPriority)
can be achieved, Now Android7 the latest SMS application is a combination 悬浮式通知
直接回复
of functions, but I myself in the implementation of the time still encountered some problems, such as: How to control the disappearance of floating notifications , how to control the floating notification After the disappearance of a general notice, the two issues I do is to issue a floating notification, using handler delay to send a general notification , but there is still a problem, in the floating notification when the direct reply, the notification may have been canceled , Here I have not found the source of Google text message to see how it is handled, if you have to know how to deal with the native message or from where can find the source of Google's original text messaging application can be in the following message to me, thank you ~
Recently gave me the demo source: https://github.com/qibin0506/N_notification_quick_reply
It's time to get to know ANDROID7: Notification Direct reply