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 .
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
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
35 mins
iOS developers love to imagine people using their awesome app constantly. But, of course, users will sometimes have to close the app and perform other activities. Laundry doesnât fold itself, you know! Happily, push notifications allow developers to reach users and perform small tasks â even when users arenât actively using the app! In this tutorial, youâll learn how to:
What are push notifications? They are messages sent to your app through the Apple Push Notification service (APNs) even if your app isnât running or the phone is sleeping. What can you use push notifications for?
This tutorial covers many of these uses to help you get started creating push notifications in your apps. To complete this tutorial, youâll need the following:
To send and receive push notifications, you must perform three main tasks:
Sending push notifications is a responsibility of your appâs server component. Many apps use third parties to send push notifications. Others use custom solutions or popular libraries (such as Houston). Youâll only touch on sending push messages in this tutorial, so be sure to check out the Where to Go From Here? section to build on your knowledge of push notifications.
To get started, download the WenderCast starter project using the Download Materials button at the top or bottom of this tutorial. WenderCast is everyoneâs go-to source for raywenderlich.com podcasts and breaking news.
In the starter folder, open WenderCast.xcodeproj. Select WenderCast in the Project navigator, then select the WenderCast target. In the General & Capabilities tab, select your development team. Build and run in a simulator.
WenderCast displays a list of raywenderlich.com podcasts and lets users play them. But it doesnât let users know when a new podcast is available and the News tab is empty! Youâll soon fix these issues with the power of push notifications.
Security is very important for push notifications. You donât want anyone else to send push notifications to your users through your app. Youâll need to perform several tasks to configure your app to securely receive push notifications.
First, you have to change the bundle identifier. In Xcode, highlight WenderCast in the Project navigator, then select the WenderCast target. Select General, then change Bundle Identifier to something unique so Appleâs push notification server can direct pushes to this app.
Next, you need to create an App ID in your developer account and enable the push notification entitlement. Xcode has a simple way to do this: With the WenderCast target still selected, click the Signing & Capabilities tab and then click the + Capability button. Type âpushâ in the filter field and press Enter.
After adding the push notifications entitlement, your project should look like this:
Behind the scenes, this creates the App ID and then adds the push notifications entitlement to it. You can log into the Apple Developer Center to verify this:
Thatâs all you need to configure for now. You are ready to start enhancing the app.
There are two steps you take to register for push notifications. First, you must get the userâs permission to show notifications. Then, you can register the device to receive remote (push) notifications. If all goes well, the system will then provide you with a device token, which you can think of as an âaddressâ to this device.
In WenderCast, youâll register for push notifications immediately after the app launches. Ask for user permissions first.
Open AppDelegate.swift and add the following to the top of the file:
import UserNotifications
Then, add the following method to the end of AppDelegate:
func registerForPushNotifications() {
//1
UNUserNotificationCenter.current()
//2
.requestAuthorization(options: [.alert, .sound, .badge]) { granted, _ in
//3
print("Permission granted: \(granted)")
}
}
What this code does:
UNUserNotificationCenter handles all notification-related activities in the app, including push notifications.requestAuthorization(options:completionHandler:) to (you guessed it) request authorization to show notifications. The passed options indicate the types of notifications you want your app to use â here youâre requesting alert, sound and badge.requestAuthorization(options:completionHandler:) can include any combination of UNAuthorizationOptions:
Add the following near the end of application(_:didFinishLaunchingWithOptions:), just before the return:
registerForPushNotifications()
Calling registerForPushNotifications() here ensures the app will attempt to register for push notifications any time itâs launched.
Build and run. When the app launches, you should receive a prompt that asks for permission to send you notifications.

Tap Allow, and poof! The app can now display notifications. Great! But what if the user declines the permissions? Add this method inside AppDelegate:
func getNotificationSettings() {
UNUserNotificationCenter.current().getNotificationSettings { settings in
print("Notification settings: \(settings)")
}
}
First, you specified the settings you want. This method returns the settings the user has granted. For now, youâre printing them, but youâll circle back here shortly to do more with this.
In registerForPushNotifications(), replace the call to requestAuthorization(options:completionHandler:) with the following:
UNUserNotificationCenter.current()
.requestAuthorization(
options: [.alert, .sound, .badge]) { [weak self] granted, _ in
print("Permission granted: \(granted)")
guard granted else { return }
self?.getNotificationSettings()
}
Youâve added a call to getNotificationSettings() in the completion handler. This is important because the user can, at any time, go into the Settings app and change their notification permissions. The guard avoids making this call in cases where permission wasnât granted.
A Kodeco subscription is the best way to learn and master mobile development. Learn iOS, Swift, Android, Kotlin, Flutter and Dart development and unlock our massive catalog of 50+ books and 4,000+ videos.
Learn more