Push Notifications Tutorial: Getting Started

Push notifications allow developers to reach users, even when users aren’t actively using an app! In this tutorial, you’ll learn how to configure your app to receive push notifications and to display them to your users or perform other tasks. By Chuck Krutsinger .

4.9 (27) · 2 Reviews

Download materials
Save for later
Share
You are currently viewing page 4 of 4 of this article. Click here to view the first page.

Sending to a Real Device

If you don’t want to send push notifications to a real device or you don’t need silent push notifications yet, you can skip this section and go to Where to Go From Here?

However, if you want to get a feel for how to send push notifications to a real device and try silent push, then you need to do some additional setup. Download the PushNotifications utility. You’ll use this utility app to send notifications to a real device. To install it, follow the instructions under How to install. Pay special attention to how to open the app because you’ll have to change some settings to run this utility.

Head over to the Apple Developer Member Center and log in.

Sending push notifications requires an Authentication Key. In the member center, select Certificates, Identifiers & Profiles, then find Keys on the left pane. To the right of the Keys title is a + button. Click it to create a new key.

Give the key a name, such as Push Notification Key. Under Key Services, select Apple Push Notifications service (APNs).

Example push notification key

Click Continue and then Register on the next screen to create your new key. Tap Download. The downloaded file will have a name something like AuthKey_4SVKWF966R.p8. Keep track of this file — you’ll need it to send your notifications! The 4SVKWF966R part of the file name is the Key ID. You’ll also need this.

The final piece that you need is your Team ID. Navigate to the Membership Details page of the member center to find it.

You did it! With your new key, you are now ready to send your first push notification! You need just one more thing.

Run the app on your real device and copy the device token from the debugger console and have it ready.

Launch PushNotifications and complete the following steps:

  1. Under Authentication, select Token.
  2. Click the Select P8 button and select the .p8 file from the previous section.
  3. Enter your Key ID and Team ID in the relevant fields.
  4. Under Body, enter your app’s Bundle ID and your device token.
  5. Change the request body to look like this:
{
  "aps": {
    "alert": "Breaking News!",
    "sound": "default",
    "link_url": "https://raywenderlich.com"
  }
}

Click the Send button in PushNotifications.

Push notifications tester

You should receive your push notification:

Your First Push Notification

Troubleshooting Common Issues

Here are a couple problems you might encounter:

  • Some notifications arrive, but not all: If you’re sending many push notifications simultaneously but you receive only a few, fear not! That is by design. APNs maintains a QoS (Quality of Service) queue for each device. The size of this queue is 1, so if you send multiple notifications, the last notification is overridden.
  • Problem connecting to Push Notification Service: One possibility could be that there is a firewall blocking the ports used by APNs. Make sure you unblock these ports.

Using Silent Push Notifications

Silent push notifications can wake your app up silently to perform some tasks in the background. WenderCast can use this feature to quietly refresh the podcast list.

With a proper server component this can be very efficient. Your app won’t need to constantly poll for data. You can send it a silent push notification whenever new data is available.

To get started, select the WenderCast target again. Now click the Signing & Capabilities tab and add the Background Modes capability. Then check the Remote notifications option:

Check the remote notifications option for background modes entitlement

Now, your app will wake up in the background when it receives one of these push notifications.

In AppDelegate.swift, find application(_:didReceiveRemoteNotification:fetchCompletionHandler:). Replace the call to NewsItem.makeNewsItem() with the following:

// 1
if aps["content-available"] as? Int == 1 {
  let podcastStore = PodcastStore.sharedStore
  // 2
  podcastStore.refreshItems { didLoadNewItems in
    // 3
    completionHandler(didLoadNewItems ? .newData : .noData)
  }
} else {
  // 4
  NewsItem.makeNewsItem(aps)
  completionHandler(.newData)
}

Going over the code:

  1. You check to see if content-available is set to 1. If so, this is a silent notification.
  2. You refresh the podcast list, which is an asynchronous network call.
  3. When the refresh is complete, call the completion handler to let the system know whether the app loaded any new data.
  4. If it isn’t a silent notification, then it is a news item, so make a news item.

Be sure to call the completion handler with the honest result. The system measures the battery consumption and time that your app uses in the background and may throttle your app if needed.

That’s all there is to it. To test it, build and run, foreground the app and push the following payload via the PushNotifications utility:

{
  "aps": {
    "content-available": 1
  }
}

If all goes well, nothing should happen, unless a new podcast was just added to the remote database. To confirm the code ran as expected, set a breakpoint in application(_:didReceiveRemoteNotification:fetchCompletionHandler:) and step through it after the notification is sent.

Where to Go From Here?

Congratulations! You’ve completed this tutorial and made WenderCast a fully featured app with push notifications!

You can download the completed project using the Download Materials button at the top or bottom of this tutorial.

Want to dive deeper into everything you can do with push notifications, such as building custom UIs and sending critical alerts? Our Push Notifications by Tutorials book will teach you the advanced features of push notifications.

Another resource is the Push Notifications Tutorial for iOS: Rich Push Notifications tutorial.

Even though push notifications are an important part of modern apps, it’s also very common for users to decline permissions to your app if you overdo the notifications. But with thoughtful design, push notifications can keep your users coming back to your app again and again!

This cat received a push notification that his dinner was ready!

Cute cat notification bell

I hope you’ve enjoyed this push notifications tutorial. If you have any questions, feel free to leave them in the discussion forum below.

Chuck Krutsinger

Contributors

Chuck Krutsinger

Author

Keegan Rush

Author

Mark Powell

Tech Editor

Luke Freeman

Illustrator

Adrian Strahan

Final Pass Editor

Richard Critz

Team Lead

Over 300 content creators. Join our team.