[MUSIC] The other kind of user notification that we'll talk about in this lesson, are notifications that appear in the notification area. And as I said earlier, this kind of user notification appears in the system control area at the top of the device, called the notification area or the status bar. Applications and the Android system itself, can use this area, to inform users about the occurrence of various events. The notification area also provides a user interface element, called a drawer, and the user can pull down on the notification area, to open the drawer. And once it's open you can see additional information about the various notifications, that have been placed in the notification area. Let's see an example of how these notifications are used in Android. And I'll open up the phone application, and now I'll start dialing a phone number. And now I'll hit the dial button, and the phone will start dialing the number and connecting the phone call. Now let's say that, in the middle of this phone call, I need to get some information off the internet. So, I hit the Home button to go back to my home screen. And from there, I'll open up the browser application. And now notice that up at the top-left of my device, a new icon has appeared, that's a notification. When I backed out of the phone application, Android created this notification object, and put it in the notification area. And this notification serves as a reminder to me that the phone call is still connected, and it also serves as a way for me to quickly return to that phone call. Now back in the browser, I'll go to www.google.com and I'll execute a search. Let's assume at this point I'm armed with the information that I needed, and I want to go back to my phone call. So now, I'll pull down on the notification area to open the notification drawer. [SOUND] And once it's open, I can see a view, which shows me some information about the call. And it allows me to either reconnect to the call, or to hang up. Now in this case, I want to continue the call. So I'll click on the notification area. And that brings up the phone application, brings it back into the foreground, and allows me to keep talking. When you want to send a notification, there are several things that you need to consider. First, there's the basic notification itself, which must have at least text for its title and content, and also a small icon. When the notification is sent, it will eventually arrive in the notification area, where the small icon will be displayed. Additionally, you can set the notification's ticker text, in which case that text will also be displayed when the notification first appears in the notification area. Finally, if the user opens the notification drawer, there's a view that they'll see. Now, by default, this includes the title, the detail or content text, the small icon, and a time stamp. You'll also need to define any action that will occur should the user click on the notification draw, drawer view. Now once you've created the notification you may want to send it. Update it, cancel it or things like that. These operations are managed by an Android system service called the Notification Manager. So let's take a look at two applications that send notifications, and then we'll look at some source code to see how all of this is implemented. So here I'll open the notification status bar application. And this user interface displays a single button labeled Notify. When I click this button, a notification will be created and sent, and it will eventually appear in the notification are at the top of the phone. So, let me click on the button now. [SOUND] And the notification is now arriving, and that rooster crowing you hear is the sound that I attached to that notification. You can also see the ticker text scrolling up in the notification area. Now after the ticker text finishes scrolling, it will go away and the notifications icon will remain visible. So, now open the notification drawer. You can see the drawer view showing the icon, the notification title text, the notification detail text, which shows the number one in parentheses, indicating that the notify button has been pressed one time. And finally, there's also a time stamp. Now at this point, I'll just close the, the notification drawer and go back, and hit the Notify button one more time. [SOUND] Now, I'll open the Notification drawer one more time, and you can see that the detail text has been updated to show that this is the second time that the Notify button has been pressed. At this point, I'll click on the notification, and you can see that a new activity has started, printing out the words: Got the intent. And the point here of course is that you can attach an intent, to the notification drawer view. In order to bring the user to the application that should handle whatever follow on action that notification was intended to provoke. Let's look at a second application notification status bar, with custom view. This application does the same thing as the last example. However it shows a custom view when the notification drawer is opened. Now I'll start the application and hit the notify button just like before. The notification is created and eventually [SOUND] appears in the notification area. However, when I open the notification drawer, I'm not going to to see the standard view, I'll see my own custom view. Let me open the notification drawer now. And, there's our eyeball and the words, you've been notified, with the number in parenthesis. Now, I'll close the drawer and I'll hit the Notify button again. [SOUND] I'll open the drawer again, and you can see that everything is the same except that the number one has become the number two, to show that this is the second notification, which updated the first one. And finally, I'll click on the notification drawer view, which starts up a new activity displaying the words Got The Intent. Okay, so let's look at the code for that second application, notification status bar with custom view. And here I'm back in the IDE, and I'll open the application's main activity. Starting at the top, this code creates an ID for the notification that's, that it's going to send. And this allows the notification manager to update this notification, after it's first been sent. And next, there's some variables that hold the text elements of the notification, including its ticker text, title, and content. After that, the code sets up some information that is used to play a sound, and to vibrate the device when this notification arrives. Next, the code creates the custom view that will be displayed in the notification drawer. The layout for that view is in the custom_notification.xml file. Let's take a look at that. As you can see, this view is a linear layout, with two child views. One is an image view, the one that displays the eyeball. And the other is a text view, and it displays the text, you've been notified. Back in the main activity, let's look at the on create method. And here the code creates an intent, called M notification intent. This intent will explicitly activate the notification sub-activity. The next line of code, is something that we haven't talked about before. This line creates something called the pending intent. Based on the M notification intent, that was created on the previous line. A pending intent is essentially a permission slip that allows one piece of code to stand in for another piece of code. And what I mean by that, is that this permission slip allows the second piece of code, to activate the underlying intent as if it were the first piece of code. And that is, it does it with its, the permissions and the identity of that first piece of code. So, moving on, the notify button's listener first updates the content text. They indicate the number of times the buttons been pressed. Then it constructs the actual notification, using the notification.builder class. A code makes new notification.Builder object, and then it sets the ticker text, sets the small icon and then sets auto cancel to true. Now this tells Android to cancel the notification, is the user clicks on the drawer view. Next it sets the content intent, and this is the pending intent that defines the. action to take, when the user clicks on the drawer view. Next, it sets the sounds, and vibration patterns that should play when the notification arrives. And lastly, it sets the custom view that should be displayed when the user pulls down the notification drawer. Now the notification is set up, the code then gets a reference to the notification manager by calling get system service, passing in the ID of the notification service. Then finally, the code calls notify on the notification manager, passing in the ID of the notification. Which allows existing notifications to be updated, and it also passes in the result of calling build on the notification .builder object. And that build method is what actually generates, the actual notification object.