Android O introduces a number of improvements to notifications. I’ll show you some of these by using a sample app you can download from Android Studio 2.4 Preview's "Welcome" dialog.
- [Instructor] Android O introduces a number of improvements to notifications. I'll show you some of these by using a sample app that you can download from the Welcome screen. I'll click on Import an Android code sample and then from the list under Android O preview, I'll choose Notification Channels. You can also click this link to jump to the sample code in GitHub. I'll go back to Android Studio and I'll click Next and then Finish. And that downloads the sample app and opens the Android Studio project.
If you see the Android Gradle Plugin Update dialog, click Update. And then if you see this update dialog, click Update again. Now this version of this sample app has a couple of problems I have to fix before I can run it. First of all, I see an error that there's no launcher icon. That's easy to fix. I'll go to the Project window to the Application module. I'll right-click and choose New, Image Asset. I don't care what the launcher icon looks like and the new adaptive icon functionality isn't working yet in this preview.
So, I'll just click Next and Finish to create a default icon. Then to make sure that's working, I'll go to the Menu and choose Build, Make Project. Now there's one other critical thing you have to fix in this particular sample app. Notice here under uses-sdk in the Application manifest that the minimum SDK version and the target are both set to O in quotes and that's correct if you want to set it that way. But there's a duplicate setting in the build.gradle file in the Application module.
I'll scroll down to ways here and show you that the targetSDKversion has been set to android-O and that's not correct. It should just be upper case O, like that. I'll make that change and resync and once again, I'm prompted to update the gradle plugin and I'll click the Update button. And now I'll try to run the app on a device. I'm running a virtual device that emulates the Pixel XL and is running Android O.
And the app opens on that device. One of the most important new capabilities of notifications is the use of notification channels. These are known to the user as notification categories. Before I send anything from this application, I'll drag down my Notifications display and show that I have a notification from the system telling me about a Virtual SD card. I'll click and hold on the notification and I see a listing that says, "1 out of 5 categories from this app." I'll click on All Categories and I come to the Settings screen and I see the purpose of categories.
In the past, you could silent or otherwise control notifications for an entire app at a time but now you can do this for each individual category, or as the developer knows them, each individual channel, and this sample app shows you how to do it. First of all, under java, I'll go to my NotificationHelper class. Don't worry about the error messages. That has to do with the minimum SDK and I'm not going to worry about that now.
What I care about is this method called createNotificationChannel. This is informing the framework of a new channel that this application is going to support and there are two different channels. So in the application, there are two channels called simply Primary and Secondary. I'll click Send 1 for the first channel and Send 1 for the second channel. And the second one actually shows the channel as a pop-up. Now I'll drag down my notifications and I'm going to click and hold on the Secondary Channel.
I'll open the categories. I see my two channels that were set programatically and a third simply called Miscellaneous. I'll click on the Secondary Channel and block everything. Now I'll open the app again and I'll get rid of all these notifications and I'll send from the first channel and I see the notification appear and I'll send from the second channel and I don't see anything.
Simply by separating the channels, you're giving the user the ability to more finely decide when they want to see various notifications. You'll find all the code you need for this tool in the NotificationHelper and in the MainActivity. You'll find code that provides ids for each notification and you'll find code here that's sending the notification. And notice that there are methods called getNotification1 and getNotification2. These are for the two different channels which are identified here with these constants.
This sample app can help you understand this new feature. The device running on Android O knows how to handle these different channels and how to let the user control them. But it's up to you as the developer to create the channels and then assign them to your new notifications.
- Installing Android Studio 2.4 Preview
- Targeting Android O in a new project
- Setting up Android O testing devices
- Exploring the new features for users
- Using display notifications in channels
- Using Autofill EditText components
- Displaying video as picture-in-picture
- Exploring other new features for developers