[MUSIC] Okay, in this lecture we are going to talk about doing push notifications and there's a particular kind of push notification that we're gonna talk about which is local push notifications. At a high-level, you're probably very familiar with notifications, these are the things that beep and buzz on your phone. And bring up alerts telling you that you need to attend to the software that's on your phone. They're an important way of alerting a user that something important has happened within your application. It's used when your application isn't in focus though, so when the user's on the home screen, when the screen is locked, or when you're in a different app. Notifications can generally be notified by your local app doing something or something changing. Or it can be created, a notification can be created by a remote server out on the Internet somewhere, generating an alert and then sending that remotely to your phone. A remote alert is really beyond the scope of our course because it requires doing some work with a remote server. So we will probably just show you the most basic approach to that. But the local notification is definitely within the scope of what we're talking about in terms of getting secure notifications to your user. So, let's talk about that. What that looks like at the high level is that we're going to create and what we're doing here is we're preparing for our pair of view assignment, so this is gonna be a coding lecture. At a high level what we're doing is we're going to be creating a UILocalNotification object. There is some steps we have to go through in order to create that object but once it's created, we're then gonna pass it off to the operating system. And it's gonna be in the operating system until the point at which the notification fires, or the moment at which the notification becomes active. At that moment, the user is going to get the notification, possibly a badge is gonna show up, that little red number being a badge, and the app is going to become activated again, and it's going to receive that local notification and it's gonna do whatever it needs to do in response to that. Possibly alerting the user of some important thing they have to attend to, right away. So the steps that we're gonna go through in order to do this are as follows. The first thing that we have to do, is we have to request permission to notify the user. So, just because your app wants to notify the user, doesn't mean that the user necessarily wants notifications from the app. So, the first thing that we have to do is, we have to ask the operating system if the user is willing to accept notifications from our application. Then, once we're done with that we need to have the ability to create a notification object. We need to be able to execute that notification, meaning we need to give it to the operating system in order to keep track of it and then fire it off when it becomes time. We then need to prepare our application in order to receive that notification back. And this is relevant to the life cycle lecture because there are two different life cycle stages, in which your application will get different kinds of signals. One, if your application is suspended and not running and the other is if it's running in some kind of a frozen or suspended state. And then finally, unlike the other applications, the other peer review assignment set we've walked through, in this one we're gonna wire up the user interface last. Because the user interface isn't a very important piece of what we're demonstrating here in this functionality. All right, so let's go ahead and do that. So, the first thing that we're gonna do is we're gonna request permission to notify the user. I'm gonna have to create a project for us. We are going to have to identify what types of permissions we are requesting. And so we're gonna request permissions to put a badge on our app. We're gonna request permissions to make a sound, and we're gonna request permission to throw up a notification. After we put those types together, we're going to build the settings object from those types and then we're going to ask the user for permission based on those types of we just said put together. This is a permission that only needs to be asked once per app. Once the user has given permission for the app to have these sort of notifications, we don't have to do it anymore. And if in the future, the user wants to rescind those permissions, they will do it through the settings panel, settings app in the iOS systems preferences. So, and the way that we request this from the user is we pass it to the UI application object which will then pass it off to iOS for us. All right. So let's go ahead and start a new project. Here's Xcode. And we want, oops that's the wrong thing. We want a new project. And it is going to be a single-view application. And we will call it Pushy App. [SOUND] Okay, Pushy App, objective C, that's good. And we'll keep it around there. All right. Now, the permissions that we're gonna be asking for. We're gonna go ahead and put those into our view controller, because we're not gonna ask for permissions until the first time, in which the user does something that needs those permissions. There are different styles and different approaches to this. Another approach would be to do it in your Application Delegate. When your application first loads, so that immediately when your application loads, it asks the user for permission just as a style thing I'm gonna for now put it in the view controller. And the way that I'm gonna do that is I'm going to create a function and it's not gonna return anything. And is going to be called request permission to notify. And so notify. And I'm gonna declare that up there. And then down here in the view controller, I'm going to go ahead and quit. Now, the first thing that we said we needed to do, was we needed to put together our types. And, so our types are going to be user, UI, user, notification types, type. [SOUND] And we need to construct the type, which is a collection of bitwise ored permissions. So, it's going to be a UI user notification type. We want to be able to put a badge up on the app. And we want to be able to put a sound up on the app. And we want to be able to put an alert up on the app. So those things [INAUDIBLE] together will give us our types. The next thing that we want is, we want to do user notification settings. And this is going to be the request that we put together. So it's gonna be settings four types, types and in this case, we're not gonna use any categories. In the future we may find that categories are something that we wanna use. That's when we wanna put some custom actions in our notification, but we're not gonna do that in this lecture. All right. We compile it okay. Get that capital S. Ready. And call that settings. Okay, so we have our type, we have our settings, object, and now, what we need to do is we need to request our permission to do that from our shared application object. And we are going to register user notification settings here. When we register those settings, that's going to cause IOS to ask our user for those permissions. Okay, great! So, we've done that. The next thing we need to do is, when it becomes time to actually create a notification, we need to create that notification object. So, to create the notification object we are going to instantiate an object. We are gonna create it. We're then gonna decide when it's going to execute. This one we're gonna cause to execute sometime in the future, some period of time based on the number of seconds in the future. We're gonna pick which timezone we care about, and we'll talk about that briefly. We're gonna decide what the notification is gonna say when it comes live and then optionally, we're gonna set a badge number like here in the graphic, we show a badge number of one. So let's do that. All right, so let's create another function here that we're gonna work with. And this is gonna be called create a notification. And for this one, we are actually gonna have one parameter which is gonna be the seconds in the future that we are going to execute our notification. So down here, we'll go ahead and we'll create this function. And the first thing that we need to do is we're gonna create a UI local notification object called local notif. And we will initialize it with the local notification object. And allocate it and then we're gonna admit it in the standard way that you allocate, and then initialize an object on the heap. And then now that we have it, we just need to declare when it is we want it to fire. And we're going to do that with, sorry, we're gonna set a property variable. And that's going to be equal to an NSDate object, which is gonna be the current date. We're gonna get the current date. And then we're gonna adjust it by adding a time interval. And the time interval that we're going to add to the current date is going to be the parameter that gets passed to our method. And we need to also spell everything correctly, right. If we're worrying about moving around the globe, we have to worry about time zones. And the way this works is when we set the time of about which point in the future we want our notification to go off, it assumes that it's going to go off in the local time zone. And if you move to a different time zone, it's going to fire that notification at the same relative time, in that new time zone. So, for example, if you say I want this notification to happen at 2:30 in the morning and you're in the Pacific U.S., and you move to the East coast, and that alarm hasn't gone off yet, then it's gonna go off at 2:30 in the morning on the East coast time, if you do the following. If you set the local notification, time zone, equal to no. Now it's not clear you want that behavior. If you perhaps want the notification to go off on an actual globally unique moment in time, so even if you're on the East coast you still want it to be going off at 2:30 am on the Pacific coast. Then you would set your times on equal to the Pacific coast. So if time zone is nill the time will actually change as you move, the notification time will actually change as you move through time zones. If you specify that it's going to stick with one particular place on the globe. All right, that's good. That tells us when our notification is gonna go off. What we need to do now is we need to say, what is it gonna say? Well, the body, we can set the title of our alert. Oh sorry, alert title. And we'll set that equal to, let's see. We'll just set an equal to alert title so that when it happens, we can see which part is the title and which part is not. The next thing we wanna do is we wanna set the alert body. And we'll do the same thing and say, this is the alert body. And then, we're going to have an action that the user can do in response to this. So this could be delete or it could be okay, or it could be close, whatever it is. But we're gonna go ahead and this is what's gonna be on the button for it. The actual action that we take depends on what we do in our callback. And we'll say okay to respond to it. All right. We also ask for permission to play a sound so if you have ability to hear sound on your device, if it hasn't been turned off, then we're going to go ahead and set our sound that we're going to play and we're just going to use a built in sound. There we go. Notification sound. And the last thing that I think is kind of fun is that we get to set the badge number. When the notification goes off, we're gonna set a badge number. And so we can set it to whatever we want. You might be used to having a badge number on your email client that says thousands and thousands. Or you might have a to-do list, in which you have hundreds and hundreds on your badge. So we could do whatever we want and usually you want it to mean something, but just for fun, we're gonna set the icon badge number equal to 4,123. All right. That's great. That has created our notification object. But it hasn't loaded it into our application. We haven't delivered it to the operating system to execute. And we're gonna go ahead and have this function do that work. We're gonna create it based on the seconds in the future parameter and we're gonna deliver it to the operating system to take care of. So, the way we do that is we get our UI application object and we are going to schedule a local notification, and we are going to just one. We want to schedule the local notification. There we go. And we want it to be local notif. All right. No errors. We're good to go. Okay, so that was the creation of the notification object with a function now that does that. The next thing that we have to do is we have to prepare our app to receive the notification. So lets do that next. [MUSIC]