Changes in the Android N notification bar and the quick notification bar

Source: Internet
Author: User

Changes in the Android N notification bar and the quick notification bar

Android N introduces a number of new APIs that allow applications to publish notifications with high visibility and interactivity.
Android N extends the existing Remoteinput notification API to support inline replies on handheld devices. This feature allows users to quickly reply from the notification bar without having to access the app.

In addition, Android N allows you to bundle similar notifications and display them as a notification. In order to implement this feature, Android N uses the existing NotificationCompat.Builder.setGroup () method. Users can expand notifications from the notification bar, and each notification will be responded to and purged.

Finally, Android N also adds some new APIs that allow you to use system decoration elements in your app's custom notification view. These APIs help to ensure that the notification view is consistent with the presentation of the standard template.

This article highlights some important changes that you should consider when you use the new notification feature in your app.

Direct reply

With the direct reply function in Android N, users can quickly reply to text messages or update task lists directly within the notification interface. On a handheld device, an inline reply can be done via an additional button in the notification. When the user replies via the keyboard, the text reply is appended to the Intent that you specified for the notification action, and the Intent is sent to the handheld device app. need to reply directly.
To create a notification action that supports direct replies:

Creates a Remoteinput.builder instance that can be added to the notification action. The constructor of the class accepts a string that the system uses as the text input key. The handheld device app then uses the key to retrieve the input text.

 //远程的输入控件构造一个   RemoteInput remoteInput = new RemoteInput.Builder(KEY_TEXT_REPLY).setLabel("回复点什么呢?").build();

Use Addremoteinput () to attach the Remoteinput object to the operation.

    // Create the reply action and add the remote input.Intent intent = new Intent();intent.putExtra("id", id);intent.setClass(MainActivity.this, MainActivity.class);PendingIntent replyPendingIntent = PendingIntent.getActivity(MainActivity.this, 0, intent, PendingIntent.FLAG_ONE_SHOT);Notification.Action action = new Notification.Action.Builder(R.mipmap.ic_launcher, "点击这里回复东西", replyPendingIntent).addRemoteInput(remoteInput).build();

Apply actions to notifications and send notifications.

 // Build the notification and add the action.final Notification newMessageNotification = new Notification.Builder(MainActivity.this).setSmallIcon(R.mipmap.ic_launcher).setContentTitle("通知栏标题").setContentText("通知栏内容").setPriority(Notification.PRIORITY_HIGH).setDefaults(Notification.DEFAULT_VIBRATE).addAction(action).build();// Issue the notification.NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);notificationManager.notify(id++, newMessageNotification);

The user will then be able to see our notifications and respond to them in the notification bar.

Get user input data

We can get our data by intent so that the components are swapped from pendingintent. Then send a notification with the same ID to update our response.

Bundle remoteInput = RemoteInput.getResultsFromIntent(intent);String res = null;if (remoteInput != null) {res = (String) remoteInput.getCharSequence(KEY_TEXT_REPLY);}Notification repliedNotification = new Notification.Builder(this).setSmallIcon(R.mipmap.ic_launcher).setContentText(res + "回复成功").build();// Issue the new notification.使用这个相同的ID冲掉以前的notifacationNotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);int id = intent.getIntExtra("id", 1);Log.d("EDSHENG", "onNewIntent: " + id);notificationManager.notify(id, repliedNotification);

Send a grouped notification

Group notifications There are 2.1 ideas to construct a grouped message and then group the child notifications.

Note: If there are no groups in the normal notification bar on n more than 4 systems will be grouped into a group by default.

 The remote input control constructs a remoteinput remoteinput = new Remoteinput.builder (key_text_reply). SetLabel ("Group reply point what?" "). Build ();//Create The Reply action and add the remote input. Intent Intent = new Intent () Intent.putextra ("id", id); Intent.setclass (Mainactivity.this, Mainactivity.class); Pendingintent replypendingintent = pendingintent.getactivity (mainactivity.this, 0, intent, Pendingintent.flag_one_ SHOT); Notification.action Action = new Notification.Action.Builder (r.mipmap.ic_launcher, "group Click here to reply to things", replypendingintent). Addremoteinput (Remoteinput). build ();//build the notification and add the Action.final Notification Group = new Notification.builder (mainactivity.this). Setsmallicon (R.mipmap.ic_launcher). Setcontenttitle ("Group notification Bar Header"). Setgroup ("group Information"). Setautocancel (True). SetStyle (New Notification.inboxstyle ()). Setgroupsummary (True). Setcontenttext ("Group notification bar Contents"). Addaction (Action). build (); Notification newmessagenotification = new Notification.builder (mainactivity.this). Setsmallicon (R.mipmap.ic_launcheR). Setcontenttitle ("Group sub-notification bar Header"). Setgroup ("group Information"). Setautocancel (True). SetStyle (New Notification.inboxstyle ()). Setcontenttext ("Group sub-notification bar contents"). Addaction (Action). build ();//Issue the notification. Notificationmanager Notificationmanager = (notificationmanager) getsystemservice (Notification_service); Notificationmanager.notify (id++, newmessagenotification); notificationmanager.notify (group);

By constructing a grouping call setgroupsummary method is divided into a group, and then the second notification bar call Setgroup is divided into this group, if there are other notifications come over also call this setgroup for the same group can be inside this group.

Message Delivery Style

Ndroid N introduces a new API to customize the notification style. Using the Messagestyle class, you can change multiple labels that appear in the notification, including the conversation title, other messages, and the content view of the notification. Often used in conversations.

The following code snippet demonstrates how to use the Messagestyle class to customize the notification style.

Notification notification = new Notification.Builder(MainActivity.this).setSmallIcon(R.mipmap.ic_launcher).setStyle(new Notification.MessagingStyle("Me").setConversationTitle("Team lunch").addMessage("Hi", System.currentTimeMillis(), null) // Pass in null for user..addMessage("What‘s up?", System.currentTimeMillis(), "Coworker").addMessage("Not much", System.currentTimeMillis(), null).addMessage("How about lunch?", System.currentTimeMillis(), "Coworker")).build();// Issue the notification.NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);notificationManager.notify(id++, notification);

Quick Notification Bar

Android N also added a class named Tileservice, which is mainly for the extension of the quick notification bar, so that developers can customize the drop-down notification bar shortcut settings icon.

We can place some of the shortcut portals in our application here. So let's take a look at how we can implement this quick notification bar.
We inherit a tileservice and then implement its OnClick method. We are here to activate our activity and then close our drop-down notification bar. However, it is important to note that the configuration is within our androidmainifest. One is the right to close and expand the notification bar


This is the statement of service.

   <serviceandroid:name=".TitleService"android:label="免费wifi"android:icon="@drawable/wifi_panel_signal_good"android:permission="android.permission.BIND_QUICK_SETTINGS_TILE"><intent-filter><action android:name="android.service.quicksettings.action.QS_TILE" /></intent-filter></service>

And then here is our implementation.

  public class Titleservice extends Tileservice {public titleservice () {} @Overridepublic void ontileadded () { Super.ontileadded ();} @Overridepublic void ontileremoved () {super.ontileremoved ();} @Overridepublic void OnClick () {Super.onclick (); Intent Intent = new Intent (); Intent.setclass (Getapplicationcontext (), Mainactivity.class)//intent.setcomponent (New ComponentName ("COM.TENCENT.MTT", " Com.tencent.mtt.external.wifi.WifiLaunchActivity ")); Intent.addflags (Intent.flag_activity_new_task); Getbasecontext (). StartActivity (intent); Collapsestatusbar (Getbasecontext ());} public static void Collapsestatusbar (context context) {Try{object Statusbarmanager = Context.getsystemservice (" StatusBar "); Method collapse;if (Build.VERSION.SDK_INT <=) {collapse = Statusbarmanager.getclass (). GetMethod ("collapse");} Else{collapse = Statusbarmanager.getclass (). GetMethod ("Collapsepanels");} Collapse.invoke (Statusbarmanager);} catch (Exception localexception) {localexception.printstacktrace ();}}}  

If you want to learn more about this class and method, you can visit this blog: https://medium.com/@KieronQuinn/quick-settings-in-android-n-ea8ad8ce2eea#.aevgyn5o6.

Demo: http://download.csdn.net/detail/shengbo1992/9573890

Changes in the Android N notification bar and the quick notification bar

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.