Lei feng's network (search for "Lei feng's network" public attention): this article by nuggets translation translator DeadLion translated from Ian Lake of the Notifications in Android n, proofreader: danke77, xcc3641. This started re the nuggets without permission prohibited reprint.
Android notification is often crucial forms of interaction between applications and users. In order to provide a better user experience, Android has made many improvements on the n in the notification: when a message is received the Visual Refresh, improved support for custom view, expand more useful reply message form, new MessagingStyle, and bundled notification.
Also notice different "look"
First of all, the most obvious change is notified of the default appearance has changed significantly. In addition to the application's icon and name will be fixed in the notice, many scattered around the notification fields are also being folded into the new header line. This change is to ensure as far as possible make more room for titles, text and icons, so the notice is slightly larger than it is now, more readable.
Given the header row, this information is more important and useful than ever before. When Android n is specified, the default time will be hidden – notification of time-sensitive (such as message class), setShowWhen (true) set to re-enable the display time. In addition, the subtext will now replace the role of content and the number of messages: number won't appear on the Android n equipment, unless you specify a previous Android version, and does not contain any subtext, message will be displayed. In all cases, make sure that subtext is relevant and meaningful. For example, if the user has only one account, do not add email accounts as a subtext.
Notice received after the operation was also redesigned, and now the vision is under a separate notification bar.
You will note that icon did not appear in the new notification, instead of, will be notified within the limited space available to the label itself. However, in older versions of Android devices, notification icons are still needed and continue to use, such as Android Wear.
If you use NotificationCompat.Builder to create your own alerts, you can use the standard style, there's no need to modify any default new code will look like.
Better support for custom views iPhone hello kitty
If custom RemoteViews creates its own notification to accommodate any new style has always been very challenging. With the new header, extension behavior, actions, and as the main elements from the notification icon positions separating content title, we have introduced a new DecoratedCustomViewStyle and DecoratedMediaCustomViewStyle provide all of these elements, so you can use the new setCustomContentView () method, focus on content.
This also ensures that future changes in appearance, with platform can easily update and match these styles, without modifying the code of the app-end.
Direct replies
Although notification can be used to start an Activity, or one Service, BroadcastReceiver to work in the background, reply directly allows you to use the notify action embedded in the input box, direct response.
Reply to RemoteInput using the same API, originally for Android Wear an Action, in order to be able to directly receive the user's input.
RemoteInput contains information, such as the secret key used for recovery after entry, information before the user starts typing.
Where should direct replies be put in the intent bundle (can be any string) private static final String KEY_TEXT_REPLY = "key_text_reply"; Create the RemoteInput specifying this key String replyLabel = getString(R.string.reply_label); RemoteInput remoteInput = new RemoteInput.Builder(KEY_TEXT_REPLY) .setLabel(replyLabel) .build();
Once the RemoteInput is already constructed, can be properly named addRemoteInput () methods, which are attached on to the Action. Calls can also be considered setAllowGeneratedReplies (true) method allows Android Wear 2.0 build intelligent responses to facilitate users to quickly respond.
Add to your action, enabling Direct Reply for it NotificationCompat.Action action = new NotificationCompat.Action.Builder(R.drawable.reply, replyLabel, pendingIntent) .addRemoteInput(remoteInput) .setAllowGeneratedReplies(true) .build();
Remember, Marshmallow, pendingIntent by incoming Action should be an Activity. Earlier versions of the device does not support a direct reply (you may want to unlock the screen, start an Activity, then focus to the user-input box of the reply), Android on the n device Service (if you want to run in a separate thread) or BroadcastReceiver (running on the UI thread) even if it is in a locked state, the background can also handle text input. (Has a separate user option in the system settings, you can enable/disable the locking device of the reply function. )
Extract the text entered in the Service/BroadcastReceiver, you may need to RemoteInput.getResultsFromIntent ()) for help.
private CharSequence getMessageText(Intent intent) { Bundle remoteInput = RemoteInput.getResultsFromIntent(intent); if (remoteInput != null) { return remoteInput.getCharSequence(KEY_TEXT_REPLY); } return null; }
After processing the text, you must update the notification. This will trigger a hidden reply UI, it can be used as a technique to confirm whether the user receives a reply and handled appropriately.
For most of the template, which will involve the use of new setRemoteInputHistory () method to append responses to notice at the bottom. More replies should catch up with historical records until the main content updates (like others ' replies).
However, if you are doing a news application, look forward to the "back-and-forth" conversation, then you should use MessagingStyle, to append additional messages.
MessagingStyle
We have optimized message displayed in the dialog State, with the new MessagingStyle reply.
Through the addMessage () methods increase more messages, which provides built-in formatting. Supported the adoption of the text itself, a time stamp for each message, and the message sender to increase (making it easy to support conversations).
builder.setStyle(new NotificationCompat.MessagingStyle("Me") .setConversationTitle("Team lunch") .addMessage("Hi", timestampMillis1, null) // Pass in null for user. .addMessage("What's up?", timestampMillis2, "Coworker") .addMessage("Not much", timestampMillis3, null) .addMessage("How about lunch?", timestampMillis4, "Coworker"));
You may have noticed, this style can support the showing of special user message, fill in their name (in the example above the "Me"), which sets an optional dialog title.
Although you can manually via "BigTextStyle" to complete, using this style of Android Wear 2.0 users can now have a built-in response and will not be "kicked out" extension tells the view, without creating a full dress (Android Wear) will be able to achieve a seamless experience.
Bundled notification
If you want to build a "big ass" notifications by using the new Visual design, direct response, MessagingStyle and all the best practices before, but notice is important to the overall experience, especially sending multiple notifications (every conversation or e-mail for each new thread).
Bundled notification provides the best of both worlds: a separate summary notification when users look at other notice all notices or wants to operate on individual notification when you extend the set of actions (including the use of action and direct response).
If you have created notification for Android Wear, the API used here is exactly the same. Simply setGroup ()) is added to each in a separate notice, those notices of "bound" together. Not limited to bound into a group, all bundled notification is very flexible. For mail application may consider each account's messages "bundle" into a group.
Create a summary notification is also very important. Summary this notice by setGroupSummary (true)) display notification, which is the only device that supports Marshmallow and earlier notification will put together all the individual notification (you guessed it). This is the best time to use InboxStyle, although there is no requirement to use it. In the Android version n or higher on devices from outlined to extract some of the information (such as a subtext, and delete content intent intent), to bundle the notification generation collapsed notice, so you should continue to build on all API-level summary notifications. Hello Kitty iPhone case
In order to enhance the user experience of all Android n devices, sending 4 or more notification do not as a group, these notifications are automatically merged into a group
Born to inform Android n
Notification on Android is constantly improved features. Click the target from the Gingerbread age, to scalable notification operation, MediaStyle and now reply directly, binding notice. Notification play an integral part in the Android user experience.
With many new tools to use (NotificationCompat helps maintain backwards compatibility), I can't wait to see how we can use these tools to create better applications.