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.

4.6 (14) · 1 Review

Download materials
Save for later
Share

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.

Sign in to the Developer Account

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.

Enable automatic signing

Set the following values:

  1. Select your development team from the Team drop-down.
  2. In Bundle Identifier, enter a unique Bundle ID.
  3. Under App Groups, click the +. Leave the group prefix, and enter the same bundle ID used in the previous step.
  4. Make sure Push Notifications is present in the capabilities list.

Signing and Capabilities tab

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:

  1. Select Certificates, ID, & Profiles from the left sidebar.
  2. From the Certificates, Identifiers, & Profiles screen, select Keys.
  3. Click the + button.
  4. Fill out the Key Name field, select Apple Push Notifications Service (APNs) and click Continue.
    Registering a new key
  5. Click Register.
  6. Make a note of your Key ID. You’ll need it for a later step.
  7. Click Download to save the .p8 file to disk.
  8. 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.

Main podcast screen with notification prompt

Tap into any episode in the list, and you’ll see a detail screen. The selected podcast should immediately begin playing.

Podcast detail screen

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:

  1. Download and install the Push Notifications Tester app.
  2. Launch the app.
    Note: If macOS complains it can’t launch the app, Right-click the app in Finder and choose Open.
  3. Select the iOS tab.
  4. Choose Token under Authentication.
  5. Click Select P8 and then select the .p8 file you saved to disk.
  6. Enter the .p8 key ID into the Enter key id field. You copied it right before downloading the p8 file.
  7. Enter your team ID in the Enter team id field.
  8. Enter your app’s bundle ID under Body.
  9. Enter the device token you saved from the previous step.
  10. Leave Collapse id blank.
  11. Leave the payload in the body as is.
Note: If macOS complains it can’t launch the app, Right-click the app in Finder and choose Open.

Where you’re finished, it should look like this:

Testing a push notification

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.

Receiving a push notification

Hooray! Now that you’re all set up, it’s finally time to dive into the code.