Learn what the Service extension is and the reasons for using it, and add the Service extension to the project.
- [Instructor] You just set up code to move payload content around in a local notification. It would make a lot of sense to take all that user info you just used for a local notification to populate the remote notification. That's what the Notification Service Extension is for. It's a small program running in the background that intercepts your payload before Notification Center presents your push notification. In the service extension, you an modify your content with a UN mutable notification content object and get the same results you've seen with the local notification on Snooze.
Let's explore the service extension. To set up the Notification Service Extension, you'll need a new target in your application. In Xcode, go to File > New > Target. Select iOS, and you'll find in the list the Notification Service Extension. Select it, click Next, and it will ask for a product name. I usually name this by suffixing Service Extension to my app name, and hit Finish.
Then activate the extension. You'll find in the navigator a new group, which is the service extension. Open it up, and you'll find two new files. Click on the NotificationService.swift file. This is a class with only two functions and two properties. The core function is didReceive request withContentHandler. Apple even started coding it for you. The method assigns that content handler closure to one of the two properties.
The other property, bestAttemptContent, is a mutable copy of your push notification's content. In this method, you modify the content for the push notification, loading all that rich content you couldn't fit in the payload, or do any change to the content you wish. For example, Apple's template changes the title. Change their modified to push ... Pizza.
Go over to the payload. To use the notification extension, you must have the following key value pair in the payload's dictionary. After category, put a comma, and type "mutable-content":1. Copy the payload. In the top, change the scheme to PushHuliPizza, and run the app.
Paste the payload in the content. Launch the notification. You'll see on the device the new notification with the modified title. Let's add code so the remote and snooze local notifications look the same. You'll need the change pizza notification function in this target. While there's a lot more elegant methods to do this, for brevity and simplicity, copy and paste it, so go over to AppDelegate ...
To changePizzaNotificationContent ... Copy, go to NotificationService ... And paste. I have one category in this application, but you might have more than one. To show you how to have different content change for different categories, I'll only use this content if this is a pizza.category category of notification. Under the bestAttemptContent = (request.content.mutableCopy() as UNMutableotificationContent) phrase on line 37, add the following: if bestAttemptContent?.categoryIdentifier equals...
Quote, P-I-Z-Z-A, pizza.category. We're going to change that to bestAttemptContent equals ... ChangePizzaNotificationContent, and that will be a request.content. Stop the app, run again, and send the notification.
The notification appears with all of our new content. That's the basics of the service extension. In the next lesson, we'll add more content to the service extension.
- Reviewing local notifications, APNs, and push notifications
- Starting a push notification project
- Registering for a notification
- Getting a certificate
- Sending a notification
- Working with JSON payload push notifications
- Adding a notification service extension
- Altering the UI
- Coding the view controller
- Running the notification