[MUSIC] Let's prepare our app in order to receive this notification. By that I mean, we're going to interact with the lifecycle process of the phone. Because this notification is particularly interesting when our application isn't in the foreground. And so if the user responds to a notification and our app is suspended, it will be brought into the foreground. If this notification is received and our application is not running, our application will be started. Now as part of the life cycle process it's possible for us to capture the fact that the reason why our application was launched was because of one of these notifications. And that might be particularly interesting to the logic of our application. So let's take a look at that. We have two cases that we need to handle. We need to handle when it's not running and when it's in any other life cycle state. So let's take a look at that, so now this is a callback function that's part of the life cycle and we're going to find that in our app delegate code. So let's go to our app delegate code, we have all the template code that was created for us here. I'm just going to go ahead and get rid of all this template code, because we're not going to override those functions. And what I wanna start with is, I wanna start with the situation when our app is anything but not running. So, if it's suspended, it's in the foreground, we're going to receive one particular response and that's going to be a predefined callback function that's part of our application. Oops, application, there we go. Not chained to change, that's status bar, why did we get that? It is going to be did receive local notification. There we go. All right, and we're going to have a function there. All right, that wasn't too bad. So now the question is, what are we gonna do if we get this notification? Well, for now, since we don't have a particular logic that we're interested in, we're just going to create an alert that indicates that, to us as a developer and us as a user of our application, that everything's going as we expected it. So let's create a UI alert controller that will give us a little pop up window and, we will create UI alert controller. Great. And, we will do an alert controller with title. What we're going to do is we're going to specify our title here to indicate that we received this while we were running, because this function has everything except when we were not running. We'll say, received while running. That would be our title and our message will just be the body of whatever our notification is that we received. So now what's interesting here is that we have received that vary as part of this callback function, we have received the notification that we created previously and gave to the operating system. So we can look at the, for example, the body of our alert. There we go. And we will just put that as our message. So we'll just transfer the body of our notification into our pop up window that we're gonna show. And we are just going to use UIAlertControllerStyleDefault. All right, that's going to create our UI alert controller. We need to create an action, so that we can clear that window when it comes up. Action, great. All right, and we want the title of that to probably just be okay. Next file will be UI with action style default. And for handler, we're just gonna have no. We're not gonna do anything when it's done. All right, so we have our alert controller, we have our action. We need to add our action to our alert controller. And then the last thing that we need to do, is we need to run our application. So we need to run our alert. So the way I want to do this is I want to do this by putting it on the main dispatch thread. So we're gonna use this dispatch thread to make sure that the code that we're running is going to be run on the UI thread. So the dispatch_async, we're gonna get the main queue. And we're going to create a block that we're going to execute when the main thread has the opportunity to execute it. And what we're going to do is we're going to get the main view controller key window dot root view controller. And we are going to present, we're going to call the presentation controller on the alert action that we just created. We it to be, oh sorry not presentation colon. We want to present our view controller. And we want it to be animated, yes, and on completion we're not going to do anything. All right. Wrong default. UIAlertControllerSyleDefault. Nope, what's wrong with that. UI alert controller. Style. Oh there it is. Alert. Okay. All right. So now we have created a, there is a call back function, did receive local notification. When we receive that local notification we're going to create a pop up window who's title is receive while running, and the body's going to be the same as the notification that we created. We're going to have one button on it that says okay, so we can get rid of it. We added that action to it. And then we said that we want to present that pop up and we wanna do it on the main thread. Okay. So that's one way in which we can receive our notification. The other way that we can receive our notification is up here. This is the function that gets called whenever our app gets launched for the first time. So we can get our app maybe launched. And it may be launched because of our notification. And so what we're gonna do is we're just going to check that if we received a notification as part of our launch options, but UI, local notification, local Notif. As part of this launch we have a parameter called launch options. And one of the things that might be in that launch options is a value associated with a key. So what this code is doing is it's requesting the notification object, associated with a predefined key. And if our application was launched as a result of a notification, then there will be something here. If not, then it's going to be null. So we will just check, if local notification, so if it's anything but null. Then we're going to do whatever we want to do. Well what we want to do is basically the same thing that we did down below. We want to create a UI alert controller. We want to add an action and we want to display it. So we can add the same thing, the only thing that we need to do is we wanna say, received. We need something to indicate that it was some other situation. So, received on launch. And then the message that we're going to pass is going to be the local notice alert body. All right. Let's see if that compiles it. Okay. Stop. Okay. Great. So now we're handling the case where our application gets launched fresh, and we get the case where our application is already running and both cases the notification gets received. Now just as a house keeping measure, when our notification got fired, not only did the notification get shown to the user, but we also set our badge. So in both of these cases we want to clear our badge as well. And there is a field within our application that we can set. Why is it doing that? Application, ike and badge number we can set it back to zero and that can clear it. We'll do that regardless of how our notification is responded to, so that we always get zero. Alright, so that's what we've done, we've prepared our app to receive that notification and then so we did that by checking our options when we were launched. If the options had a local notification then when knew that's why we were launched. If we launch because of a notification, then we did something. In this case, we made a little special pop up window. When it's in any other life cycle state, for example, if it is suspended or in the foreground, then we are just going to make that pop up window, we are not going to check to see if we are launched with it. Finally, unlike the other applications that we've done. We're gonna put together the user interface last. And let's do that now. So it's gonna be very simple. All we're gonna do is have a button that when clicked schedules a notification. So let's do that. So to do that, we're gonna go to our main storyboard. And cuz I'm into it we're gonna turn off size classes. Wait. There we go. Turn off size classes and we're going to get a button. Oh my caps lock is on, that's what's going on. All right, we're gonna go ahead and put. That in the center of the container. That looks pretty good, but I don't wanna see. Sorry. Oh, that is it. That's the view control. Okay, so now what I need to do is I need to wire that. We don't need to change anything. All we need to do is we need to make sure that we have something that responds to the tap. So, we'll call it a schedule tapped. And, when we get that, the first thing we need to do is we need to check to see if we have permissions. And so we'll call self request permission to notify and then we are going to create a notification and it's gonna be five seconds in the future. All right. And that's it, that's our application for schedule and alert five seconds in the future. Now what I want to do is I want to do a little bit of testing of it, to make sure it's working like we expect it to work. And show you how it works in different scenarios. So what are we gonna run it on? Let's run it on an iPhone 6. Get that going. PushyApp being launched, our simulator coming up, our build succeeded. Good news. Always good news to see a build succeeded. All right. Scheduling alert in five seconds. When I click this, I better see something that requests permission. Great. PushyApp would like to send you notifications. Notifications may include alerts, sounds and icon badges. These can be configured in settings. I'm going to allow it. Five seconds later, which was very quick, that was a very quick five seconds, we received our alert while running. And our alert body was the same as our notification. Okay, that's fine. Let's do it again, this time without asking for permissions. One, two, three, four, five, and we received our alert, great. You can see that this is what happens when your application is running. It is absolutely in the foreground. Let's see what happens when we get our alert and we are suspended. So what we're gonna do is we're gonna double click the home button and then pick a different app and put that in the foreground. All right, one, two, three, four, five. Five. Five. Did I not click the button? Something happen? Did we get an error? No error. Did our simulator crash? Well it looks like we got the alert, but the screen was too big so I didn't see it. All right let's land. All right, let's shrink the screen a little bit, so we can see it. And let's try that again. Double click the home button, go to our application. Scheduler at one, two, three, four, five. There we go. There's our alert. And if we click on it, we get kicked back to our application and we see that we received our alert while running. Okay we were running, but we were suspended, because we had another app in the foreground. So now what I want to do is I want to schedule this alert, but I want to remove the app from memory. I want to put it in a not running state. So to do that, I am going to swipe it up from the multitask stage. One. Two. Three. Four. Five. We get our alert. I click on my alert. And now we can see a different message. It says received on launch. That means that we saw two different ways in which our local notification was received by our application, one in which our app was launched, because it was completely out of memory, and one it which it was either foregrounded or in the background. So we're gonna go ahead and click okay, and that's the basis for doing a local notification. In the next lecture what we're gonna do is we're gonna look at a way to customize the kinds of responses that you can offer a user in the responses to your notification. So, all right. So we wired up the user interface. So in summary, local push notifications allow an app to alert a user at a given time. It's great for calendars and to do items. They don't require a remote server, it's really terrific for that. And they can also create those little red badges. Oh, we didn't see the badges! We gotta see the badges. Let's see that. Five seconds, one, two, three, four, five. There we go. PushyApp, 4,123 things to attend to. And then when we go, and attend to it, if we go back out, we can see that our notifications for PushyApp were cleared. So glad I didn't forget to show you guys that that's totally cool. Okay, so they can create badges 4,123, that's very exciting. Interestingly enough just setting a notification based on time isn't the only thing you can do. You can also set a notification based on coming or going into a geographic region. This is called a geo fence and it has to do with the physical motion of your phone. So you can have a location activate based on moving your phone into a particular predefined region. We're gonna look at that in a future course, but for now we'll stick with time based to dos. Okay. Thank you for your attention. See you in the next lecture. [MUSIC]