Detailed explanation of adaptation of notification bar in Android 8.0 system

Hello everyone, today we continue to learn the adaptation of Android 8.0 system.

As we have mentioned before, there are two main adaptation areas for Android 8.0 system: application icon and notification bar. In the last article, we learned the adaptation of application icons in Android 8.0 system. Friends who have not read this article can first read the micro skills of Android application icons and the adaptation of application icons in 8.0 system.

In this article, we will naturally focus on the notification bar. Learn the notification bar adaptation of Android 8.0 system.

In fact, before the 8.0 system, there was another version with large changes in the notification bar, the 5.0 System. About the content that the 5.0 system needs to adapt to the notification bar, I also sorted out an article. Interested friends can read the Android notification bar micro skills, small details that you haven't paid attention to.

So let's start to get to the main topic of this article.

Why should notification bar adaptation be performed?

I have to say that the notice board is really a thing that people love and hate.

The notification bar is an original function of the Android system. Although jobs has always believed that the Android system is a product completely copied from IOS, the notification bar is indeed original of the Android system. On the contrary, Apple has added a similar notification bar function after IOS 5.

The design of the notification bar is indeed very ingenious. By default, it does not occupy any space. The content of the notification bar will be displayed only when users slide their fingers down the status bar when they need it. This greatly solves the problems of too small mobile phone screen and insufficient content display area in the early stage of Smartphone development.

However, with the gradual maturity of the development of smart phones, the notice bar has become more and more unpopular. Each app hopes to seize the space of the notice bar to publicize and promote their products as much as possible. Now I often wake up in the morning and pick up my mobile phone. The notice bar is full of all kinds of APP push, which is very annoying.

Although I am an Android application developer, I am also a senior user of Android phones. I have been using Android phone for 8 years. At present, I have zero tolerance for this kind of spam push in the notice bar. Now whenever I install a new app, I will go to the settings to find out if there is a push switch. If so, I will turn it off at the first time. If an app often pushes spam to me but cannot be closed, I will directly turn off its notification master switch. If it is not an important app, I may uninstall it directly.

Why is a good notification bar function now so annoying to users? A large part of the reason is because developers use it without restraint. Just like keeping the app alive, until today, people continue to ask me how to keep the app alive. Imagine how everyone can keep their app alive, so who is the ultimate victim? Not yet users of Android phones. Everyone's mobile phones will only get more and more stuck. Finally, they just want to throw away their mobile phones and become iPhone users. Also because developers use it uncontrollably, each version of Android will continue to shrink the background permissions.

Back to the notice bar, it's the same. Every developer just wants to publicize his app as much as possible. Finally, the user's mobile phone is as chaotic as a chicken nest. But the notice bar is still useful. For example, when we receive wechat, SMS and other messages, we really need the notice bar to remind us. Therefore, according to the analysis, the biggest problem of the notification bar is that it can't make users distinguish between interested and uninterested messages. For example, I want Taobao to push me the information about the seller's delivery and logistics, but I don't want to receive such information as discounts and promotions or asking me to buy clothes. At present, there is no way to distinguish these messages. I either agree to accept all messages or block all messages. This is the pain point of the current notification bar.

Then in Android 8.0 system, Google also started from this pain point.

8.0 notification bar adaptation of the system

Starting from Android 8.0 system, Google introduced the concept of notification channel.

What is the notification channel? As the name suggests, each notification should belong to a corresponding channel. Each app can freely create which notification channels the current app has, but the control of these notification channels is in the hands of users. Users are free to choose the importance of these notification channels, whether to ring, whether to vibrate, or whether to close the notification of this channel.

With these control rights, users no longer have to be afraid of the interruption of spam push messages, because users can choose which notifications they care about and which notifications they don't care about. For example, I hope to receive the information from Alipay immediately, because I do not want to miss any profit, but I do not want to receive the delicacy recommended by Alipay, because I have no money to afford the company canteen. In this case, Alipay can create two kinds of notification channels, one revenue and expenditure, one recommendation, and I am not interested in the notification of recommendation class, so I can close the recommendation channel directly, so that I do not affect the notification I care about, nor do I bother with those notifications that I do not care about.

For each app, the division of notification channels needs to be carefully studied, because once the notification channel is created, it can no longer be modified. Therefore, developers need to carefully analyze the types of notifications in their app, and then create the corresponding notification channel. Here, let's refer to the notification channel division of Twitter:

It can be seen that Twitter has made a very detailed division of notification channels according to its own notification types, so that users have a high degree of autonomy and selectivity, which greatly reduces the probability of users unloading the app due to the harassment of their spam notifications.

Do I have to fit?

Google's attitude towards the promotion of 8.0 system notification channel is still relatively tough.

First, if you upgrade the appcompat library, all places that use the appcompat library to build notifications will be prompted for obsolete methods, as shown below:

The figure above tells us that this method is obsolete and needs to use the method with notification channel.

Of course, Google is not completely done. Even if the method is marked as abandoned, it can still be used normally. However, if you specify the targetsdkversion in the project to 26 or higher, the Android system will think that your app has done the adaptation of the 8.0 system, including the adaptation of the notification bar. If the notification channel is not used at this time, your app's notification will not pop up at all. Therefore, the suggestion here is that we must adapt.

Well, I've introduced so much background knowledge to you. Now let's officially get to the point and learn how to adapt the notice bar in the 8.0 system.

Create notification channel

First, we use Android studio to create a new project, which is called notificationtest.

After creating the project, open the app / build.gradle file and check to ensure that targetsdkversion has been specified to 26 or higher, as shown below:

You can see that when I create a new project, the default targetsdkversion is 26. If you are lower than 26, it means that your Android SDK is a little old. You'd better update it. Of course, if you are too lazy to update, it doesn't matter. Just change it to 26 manually.

Next, modify the code in mainactivity as follows:

The code is not long. Let me explain it briefly. Here, we have created two notification channels in mainactivity. First of all, we must ensure that the system version of the current mobile phone must be Android 8.0 system or higher, because the lower version mobile phone system does not have the function of notification channel, and failure to check the system version will cause a crash on the lower version mobile phone.

The way to create a notification channel is very simple. Here I encapsulate a createnotificationchannel () method. I believe everyone can understand the logic. It should be noted that creating a notification channel requires at least three parameters: channel ID, channel name and importance level. The channel ID can be defined arbitrarily as long as global uniqueness is guaranteed. The channel name is for users to see. It needs to be able to clearly express the purpose of this channel. Different importance levels will determine different behaviors of notification. Of course, this is only the importance level in the initial state. Users can manually change the importance level of a channel at any time, and app cannot intervene.

The above code simulates such a scenario. Imagine that we are developing an app similar to wechat. App notifications can be divided into two categories. One is chat messages between me and others. Such messages are very important, so I set the importance level as import_ HIGH。 The other is subscription to official account. Such information is not so important, so I set it up for IMPORTANCE_. DEFAULT。 In addition, the importance level can also be set to import_ LOW、IMPORTANCE_ Min, corresponding to lower notification importance respectively.

Now you can run the code. After running successfully, we close the app, enter settings - > Application - > notification, and view the notification interface of notificationtest app, as shown in the following figure:

The two notification channels we created just now are shown here. It can be seen that due to the different importance levels of the two notification channels, the notification behavior is also different. Chat messages can send a prompt tone and pop up notifications on the screen, while subscription messages can only send a prompt tone.

Of course, users can also click in to modify the notification channel arbitrarily, such as reducing the importance level of chat messages, or even completely closing the notification of the channel.

As for the code to create the notification channel, you can write it in MainActivity or in Application, which can actually be written anywhere in the program, and only need to make sure that it can be called before the notification pops up. Moreover, the code for creating the notification channel will be created only when it is executed for the first time. After each execution of the code, the system will detect that the notification channel already exists, so it will not be created repeatedly and will not affect any efficiency.

Let the notification display

The code that triggers the notification is basically no different from the previous version, but when building the notification object, you need to pass in an additional notification channel ID to indicate which channel the notification belongs to.

Then let's show the notice.

First modify the activity_ The code in main.xml is as follows:

Here, we add two buttons to the layout file. Obviously, one is used to trigger chat message channel notification and the other is used to trigger subscription message channel notification.

Next, modify the code in mainactivity as follows:

Here, we trigger two notifications in sendchatmsg() and sendsubscribemsg() methods respectively. The code for creating notifications will not be explained any more. It is no different from the traditional method of creating notifications, but we need to pass in one more notification channel ID in notificationcompat.builder. Here, we pass in chat and subscribe, two newly created channel IDS respectively.

Now run the code again and click the send chat message button. The effect is shown in the figure below:

Since this is a high-level important notification, this screen pop-up will be used to notify the user of the arrival of a message. Then we can pull down and expand the notification bar, where we can also view the details of the notification:

Users can turn off this notification by swiping quickly to the left or right.

Next, click the send subscription message button, and you will find that a notification reminder will not pop up on the screen, and only a small notification icon will be displayed on the status bar:

Because the importance level of subscription message notification is the default level, this is the display form of default level notification. Of course, we can drop down and expand the notification bar to view the details of the notification:

However, the above demonstration is all the traditional functions of the notification bar. Next, let's take a look at the unique functions of the notification bar in Android 8.0 system.

As mentioned earlier, swiping left or right quickly can close a notification, but if you slowly swipe left or right, you will see two buttons:

Among them, the button of the clock icon on the left can delay the display of notifications. For example, this is an important notice, but I don't have time to read it for the time being, and I don't want it to be displayed in the status bar to disturb me. I can delay it for a period of time, so that I can focus on the things I focus on for the time being. When there will be time, this notice will be displayed again, and I won't miss any information. As follows:

The button on the right setting icon can be used to screen and configure the notification channel. Users have absolute control over each notification channel of each app and can configure and modify it according to their own preferences. As follows:

For example, I feel that subscribing to news always recommends advertisements to me. It's too annoying. I can close the notification channel of subscribing to news. In this way, I will not receive any messages under this notification channel in the future, but the chat messages will not be affected. This is the biggest feature of the 8.0 system notification channel.

In addition, click all categories in the figure above to enter the complete setting interface of current application notification.

Manage notification channels

In the previous content, we have learned that once the notification channel is created, it can no longer be modified through code. Since you can't modify it, how can you manage it? For this reason, Android gives developers the right to read the notification channel configuration. If one of our functions can only be used if the notification channel must be configured according to the specified requirements, users can be prompted to manually change the notification channel configuration.

It's always difficult to understand just talking about concepts. Let's learn from specific examples. Think about it. We are developing an app similar to wechat. Chat messages are very important. If users accidentally close the notification channel of chat messages, won't all important information be lost? Therefore, we must ensure that users open the notification channel of chat messages.

Modify the code in mainactivity as follows

Here, we modify the sendchatmsg () method, get the notificationchannel object through the getnotificationchannel () method, and then we can read all the configurations under the notification channel. Here, we judge if the importance of the notification channel is equal to import_ None means that the user has closed the notification of the channel. At this time, it will jump to the notification setting interface to remind the user to open it manually.

Now run the program again, and the effect is shown in the following figure:

It can be seen that when we close the notification channel of the chat message, the next time we send the chat message again, we will directly jump to the notification setting interface to remind the user to open the notification manually.

In addition to the above methods of managing notification channels, Android 8.0 also gives us the function of deleting notification channels by using the following code:

But this function is not recommended. In order to prevent the application from randomly creating spam notification channels, Google will display the number of deleted notification channels in the notification setting interface, as shown in the following figure:

This is very unsightly, so the best way for developers is to carefully plan the notification channel rather than easily use the deletion function.

Display unread corner mark

As we mentioned earlier, Apple introduced the notification bar function from IOS 5. How did the iPhone notify messages before IOS 5? The unread corner mark function is used, and the effect is as follows:

In fact, the Android system has never provided this corner function similar to IOS before, but because many domestic mobile phone manufacturers like to follow IOS, various domestic mobile phone ROMs have launched their own corner function.

However, although domestic mobile phone manufacturers can customize ROM, they do not have the ability to formulate API. Therefore, for a long time, there has not been a standard API to realize corner mark function. Many of them have to be realized by sending broadcasts to the system, and the broadcasting standards of various mobile phone manufacturers are different, which often leads to extremely mixed codes.

It is gratifying that, starting with the 8.0 system, Google has formulated the corner mark specification on the Android system and provided a standard API. The problem that has long been a headache for developers can now be solved.

So let's learn how to achieve the effect of unread corner marks on Android system.

Modify the code in mainactivity as follows:

As you can see, we have mainly modified two places here. The first is to call the setshowbadge (true) method of notificationchannel when creating a notification channel, indicating that notifications under this channel are allowed to display corner markers. The second is that when creating the notification, the setnumber () method is called and the number of unread messages is passed in.

Now run the program again, click the send subscription message button, and then find the notificationtest application in the launcher, as shown in the following figure:

You can see that there is a green corner mark in the upper right corner of the icon, indicating that the corner mark function we have written has taken effect.

It should be noted that even if we do not call the setshowbadge (true) method, the Android system will display the corner marker by default. However, if you want to disable the corner marker function, remember to call the setshowbadge (false) method.

But why is the unread number not displayed? This function also requires us to press and hold the icon. The effect is shown in the figure below:

In this way, you can see that the number of unread notifications is 2.

Maybe some friends are used to the unread corner mark on IOS and think it's troublesome to press it for a long time on Android. There is no way, because this is the native Android system after all. Google can't wantonly imitate IOS like domestic mobile phone manufacturers, otherwise it may suffer a lawsuit. However, I believe that domestic mobile phone manufacturers will definitely customize these functions, and the style should be similar to IOS. However, this is not important. For our developers, the best gospel is that there is a unified API standard. No matter how domestic mobile phone manufacturers customize ROM in the future, they will customize it according to the API standard. We only need to use this API for programming.

OK, the next two articles on Android 8.0 system adaptation are over here. Thank you for reading.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>