Skill Level Beginner
- [Instructor] If you've worked with notifications before you've probably dealt with user permissions. In iOS 12, there's a new type of user notification to handle the situation that happens often. Download the exercise file and you'll find a project I adapted from chapter three of my course Learning iOS Notifications. I tidied up the storyboard if you go over there and I converted the file to iOS 12 for you. So open it up in Xcode 10 and I'm using a beta at the time of this recording and I don't expect much to change, but be prepared for some changes between this and the production version of Xcode.
We'll need a clean simulator to demonstrate this correctly. So go ahead and open up your simulator, go to Hardware and Erase All Contents and Settings. And I'm gonna be using iPhone 8 Plus for this, so make sure you're using iPhone 8 Plus. It'll ask you if you wanna erase content and settings, go ahead and erase it, so it's nice and clean. That gets rid of all of our statuses about notifications all correct, since we're working on permissions here. Now go ahead, over to the ViewController and I'm going to use instead of the dark theme, I'm gonna use presentation theme here.
And I don't need any of this anymore, 'cause we're gonna stay right in the code. And here in my ViewController, head down to viewDidLoad, 'cause all the work we're gonna be doing is actually in viewDidLoad. And you can see I've set permissions here in this application. So that's how we set permissions. That's how you request authorization for anytime before this. Now what I'm going to do is run the application on the iPhone 8 Plus.
And the first thing you notice, which is what this whole request authorization thing is, is it gives us this little box here that says, Would Like to Send You Notifications. Go ahead and hit Allow. And then I can hit some of these buttons here to Schedule A Pizza, Make A Pizza and stuff like that. And you'll start seeing them show up on top here, like so. And I'm gonna schedule a few more pizzas and make a few more pizzas and you can see 'em in different places. I hit Command + L, for example and as I lock the screen, up comes the screen with more notifications.
I can hit home. I can also see those notifications here by dragging down from the top in the Notification Center. The other thing you can do is if you tap on these you get an Open button and you can open them up that way or you can drag 'em down. Drag to the left from the right and you get three different buttons, one of which is a View button, which views this particular notification. You drag it again, you can also hit the Manage button and that comes up with another possibility called Deliver Quietly.
And if you hit the Deliver Quality, now what happens when you go into your app, I'll schedule a couple more pizzas, make a few more pizzas, so I've got some stuff running here. But as I'm running, I can be anywhere in this thing. I can go Command + L to lock my phone and I don't see 'em. What the quietly does is, is it does not give you a notification on the screen itself. But if you go into home and now drag down here, you'll see that they're all in the Notification Center.
Another iOS 12 improvement is you'll see that there's only one here, and you can sort of see that there's a little bit of stack here, and what it will do is stack your notifications, so that you can scroll through these and check your notifications that all have to do with Huli Pizza in one place. If you wanna look at all them, you can do it like this. Okay, so that's how it works now, with request authorization here, just the way we have it.
The new permission, which I'm gonna stop the app for this, is a very simple change. You go to just after sound here, and you put in the options .provisional. And with the provisional methodology, I'm gonna go back on my phone here and simulator and I'm gonna reset everything so I can starting completely from scratch. With the provisional situation, it goes to a quiet immediately, does not even ask you about presenting notifications.
You won't know they are there unless you go into the Notification Center. So let's let this reboot and once it's back up, I'm gonna run this application. I'll schedule a few pizzas, make a few pizzas, hit it on the Next Pizza Step. Any one of these will give me some notifications. So I've now thrown a whole bunch of notifications out there and you notice right away there's nothing here. If I lock the phone with a Command + L, I don't see anything, it's staying blank.
It's in quiet mode. Go ahead and click the home button and click it again so you're back in here. I can drag down anywhere in the application or in my home screen for that matter. I can go to home screen, drag down from notification and here's the new stack of notifications. Now I'm gonna go to the very first one, 'cause that's gonna give me something interesting. For your very first one, and you can see it on some of these others too, it's gonna give you a new message. It says, keep receiving notifications from the Huli Pizza app? You can keep receiving them or turn them off and that's all you'll need to do as a user to turn on or off notifications completely.
Now I'm gonna keep this, and then it asks me do I wanna Deliver Prominently or continue quietly? I'm gonna go ahead for prominently and now it's all set for regular prominent notification. I can hit some of these others, let's go to the one up here. Now if I go to Manage, you'll see it say Deliver Quietly again. I can click that. I'm now back into quietly. You can hit it again, Deliver Prominently. So you can switch it back and forth. All you need to do, and it's a really cool thing to start using, is this provision option in the Notification Center requestAuthorization.
This will let your user sample your notifications without pesky permission questions. With a single change to this code you get a new function you will want to use if your app uses notifications.