Push Notifications Tutorial for iOS: Rich Push Notifications
Learn how to modify and enhance push notifications before they are presented to the user, how to create custom UI around your push content, and more! By Mark Struzinski.
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Contents
Push Notifications Tutorial for iOS: Rich Push Notifications
30 mins
- Getting Started
- Setting up New App Values
- Creating an Authentication Key
- Running the App
- Testing a Push Notification
- Modifying Push Content
- Introducing Service Extensions
- Adding a Service Extension
- Exposing Files to the Extension
- Saving Files
- Downloading an Image
- Modifying the Push Content From the Server
- Updating the Title
- Adding an Image
- Creating a Custom UI
- Adding the Target
- Configuring Info.plist
- Adding the App Group
- Building the Custom UI
- Setting up NotificationViewController
- Adding Shared Files
- Customizing the UI
- Customizing the Notification
- Implementing the Favorite Action
- Implementing the Play Action
- Where to Go From Here?
If you’ve used a mobile device for the last decade, you’ve likely encountered innumerable of push notifications. Push notifications allow apps to broadcast alerts to users — even if they’re not actively using the devices.
While notifications can present helpful information to the user, the true power of notifications comes from the concept called rich notifications. Rich notifications allow you to intercept notification payloads and gives you time to dress them up in a way that best suits your user’s needs. This allows you to show custom UI that can include button actions that offer shortcuts to your users.
This tutorial assumes you have some knowledge of push notifications. If you need to brush up on the basics, check out Push Notifications Tutorial: Getting Started. That tutorial will teach you how to send and receive push notifications and utilize actions inside your push content.
This tutorial will take that knowledge further. You’ll learn how to modify and enhance incoming content, how to create custom UI around your push content and more!
Getting Started
Download the starter project by clicking the Download Materials button at the top or bottom of this tutorial.
Because push notifications only work on a physical device, you’ll need to configure the Apple developer portal with several properties to work through this tutorial. Xcode can handle most of this for you through automatic provisioning.
Setting up New App Values
Open the starter project in Xcode. If you haven’t signed into your development team yet, go to Preferences, select the Accounts tab and then sign in using the + button.
Next, select the Wendercast project node in the File navigator. Be sure that the Wendercast target is selected. In the middle pane, go to the Signing & Capabilities tab and check the Automatically manage signing box.
Set the following values:
- Select your development team from the Team drop-down.
- In Bundle Identifier, enter a unique Bundle ID.
- Under App Groups, click the +. Leave the group prefix, and enter the same bundle ID used in the previous step.
- Make sure Push Notifications is present in the capabilities list.
If you see two distinct Signing (debug) and Signing (release) sections, configure Automatically manage signing, Team and Bundle Identifier on both. Use the same values.
Finally, open DiskCacheManager.swift. Then update the groupIdentifier
property to your new app group ID:
let groupIdentifier = "[[group identifier here]]"
Creating an Authentication Key
For this step, log in to the Apple developer portal. Click the Account tab, and then follow these instructions:
- Select Certificates, ID, & Profiles from the left sidebar.
- From the Certificates, Identifiers, & Profiles screen, select Keys.
- Click the + button.
- Fill out the Key Name field, select Apple Push Notifications Service (APNs) and click Continue.
- Click Register.
- Make a note of your Key ID. You’ll need it for a later step.
- Click Download to save the .p8 file to disk.
- Make another note of your team ID; it’s displayed in the right corner of the page (below your name or company name).
Phew! That was a lot. Now that you’ve completed the portal work, it’s time to return to configuring the starter app.
This step will let the app read and write to the shared app container. It’s necessary when you add an extension so the app and extension both have access to the Core Data store.
Oh, and in case you’re wondering: Yes, you’ll create not one but two extensions, and the app will use Core Data. :]
Running the App
The Wendercast app fetches and parses the Ray Wenderlich podcast feed. It then displays the results in a UITableView
. Users can tap on any episode to open a detail view and begin streaming the episode. They also have the ability to favorite any episode.
As mentioned, the app uses Core Data for persistence between sessions. During a feed refresh, the app only inserts new episodes.
To start, ensure your iPhone is the selected device. Then build and run. You should see a list of podcasts and an immediate prompt to enable notifications. Tap Allow.
Tap into any episode in the list, and you’ll see a detail screen. The selected podcast should immediately begin playing.
Awesome! Now, you’ll send a test push notification.
Open the Xcode console. You should see the device token printed in the logs:
Permission granted: true
Notification settings:
<UNNotificationSettings: 0x2808abaa0; authorizationStatus: Authorized,
notificationCenterSetting: Enabled, soundSetting: Enabled,
badgeSetting: Enabled, lockScreenSetting: Enabled,
carPlaySetting: NotSupported, announcementSetting: NotSupported,
criticalAlertSetting: NotSupported, alertSetting: Enabled,
alertStyle: Banner,
groupingSetting: Default providesAppNotificationSettings: No>
Device Token: [[device-token-here]]
Save this value because you’ll need it, well, right now.
Testing a Push Notification
Before you can test push notifications, you need to be able to send them.
Sending a push notification requires you to invoke a REST API on the Apple Push Notification Server (APNS). It’s definitely not the easiest way — especially if you have to do it manually.
Fortunately, there’s another way. Just have an app do that for you. :]
Follow these instructions:
- Download and install the Push Notifications Tester app.
- Launch the app.
Note: If macOS complains it can’t launch the app, Right-click the app in Finder and choose Open.
- Select the iOS tab.
- Choose Token under Authentication.
- Click Select P8 and then select the .p8 file you saved to disk.
- Enter the .p8 key ID into the Enter key id field. You copied it right before downloading the p8 file.
- Enter your team ID in the Enter team id field.
- Enter your app’s bundle ID under Body.
- Enter the device token you saved from the previous step.
- Leave Collapse id blank.
- Leave the payload in the body as is.
Where you’re finished, it should look like this:
Send the app to the device background, but leave the device unlocked. Click Send in the Push Notifications Tester app. You’ll receive a push notification along with a success message.
Hooray! Now that you’re all set up, it’s finally time to dive into the code.