From the course: iOS and watchOS App Development: Notifications

Unique identifiers

From the course: iOS and watchOS App Development: Notifications

Start my 1-month free trial

Unique identifiers

- [Instructor] You might've noticed something a little odd about what I just did. Go back and schedule five pizzas. And then go to Manage Notifications and check the Pending. Notice we only get one request, not five. So go ahead and stop the app, and let's talk about this. In the ViewController, head to makePizza. Now we're sending the same identifier for every notification. That only gets us one request. And the newer notification replaces the old one. To see more requests, you change the name of the request identifier. Let's do this by tacking on a number to the end of the identifier for the makePizza interval notification. You'll first need a variable to keep track of the number. So above this, I'm going to put right here, another variable. And I'll do var pizzaNumber = 0. And then in the makePizza, when I've created my content here, I'm going to increment this, self.pizzaNumber += 1. And then under that, I'm going to add more content, called the subtitle, which appears between the title and the body of the text. I'm going to do content.subtitle = "Pizza, and then I'll do a pound sign, Pizza #\ self.pizzaNumber. And what I'm also going to do is take the pizzaNumber and add it to the identifier. So I'm going to put a period here and a slash, and I'll put self.pizzaNumber here, like that. Another common way to set a unique identifier is a date stamp. I'm going to go up to the Scheduled Pizza now, and go up to the content there and my identifier there, and I'm going to change the message.scheduled and I'll put another period here and then we'll do a slash, and then I'm going to use the date. And I'm going to do it in seconds here, so I'm going to use the timeIntervalSinceReferenceDate. So I get a number of seconds since the reference date. Now, with all that in place, go ahead and build and run. Get a whole bunch of pizzas from Schedule and Make. Go ahead and manage the notifications. Now view the Pending, and you'll see you've got a lot of notifications now, and now they're starting to pop up here too. And you can see that they're each individually a notification. You may have also noticed that it took a long time when I hit it a couple times earlier, and that's because it was read making that whole notification over again. So every time I hit it, it would replace the data that was already there in the current notification.

Contents