From the course: iOS and watchOS App Development: Notifications

Code the action response

From the course: iOS and watchOS App Development: Notifications

Start my 1-month free trial

Code the action response

- [Instructor] Let's add the code for the actions. This will be a bit of a review of everything we've learned so far. So for the cancel action, it's pretty simple. We're just going to remove the notification if it's still pending. So we're going to put in here, UN UserNotificationCenter .current .remove pending notification request. And this time, we have our request already, so we just have to use the request identifier. So I'm going to do a bracket here and put request dot identifier. The second one is the snooze. And the snooze action changes the interval of a notification of five seconds, and then it'll add the notification back in. So I'm going to do let trigger equal UN TimeIntervalNotificationTrigger. And our time interval will be five seconds. And repeat as false. So I made myself a new trigger. And I'm just going to add that to a new notification request. Let request equal UN NotificationRequest and there we go. We have the same request identifier, so it's going to replace the old one. The content's the same. And the trigger will be the trigger which is now five seconds. And finally, we're just going to add it in. And so that'll be again, UNUserNotificationCenter .current .add. And we're going to add the request. And the completion handler, we're going to have error for our parameter. And I'm going to use self. I'm going to use my lovely print error function I created for you. And just say it's in snooze action. That makes the first two of our three actions, one deleting a notification and one changing the trigger.

Contents