Setup Apple Push Notification
Basic about setting up push notification (2019)
Enable Push Notification
The first step is to enable push notification under Target -> Capabilities. See screenshot below.

Then you will need to create an authentication key file for push notification under your developer team account. Note that you will need an account with admin privilege to do this. Note that this has been changed a little bit compare to the old time. It has become much simpler.

Create a key:
Remote Notification Registration
So you have finished all the preparation, now its time to implement the APN in your project. The first thing you will need to do is to ask for notification permission. Then you will need to register device for the remote notification after user gives the permission. Waiting for callback to extract the device token.
For all these, here is a sample code written in AppDelegate.swift:
1 | func registerForPushNotifications() { |
For the above code:
- Call registerForPushNotification in didFinishLaunchingWithOptions
- Call getNotificationSettings to get current notification setting and if its .authorized, registerForRemoteNotification
- Get device token in didRegisterForRemoteNotificationWithDeviceToken and upload to your own server if needed. (Upload to my server is not include in above sample code)
🧪 At this stage, you will be able to send a APN to your testing device using the device token and able to see the push notification popup.
Testing
Now you can test to see if your implementation works.
Testing Utility Tool
This is a very useful tool to test your push notification to make sure it works.
For sending an APN, you will need following information:
- Team ID
- Key ID
- Key File (created above)
- Device Token
You can get the team id by login to your development account and select Membership on the left panel. You can get the key id when you create the key file.
Send APN Via HTTP Protocol (Restful Api)
Sending Notification Requests to APNs | Apple Developer Documentation
See above official documentation to construct a notification post request. Note that you will need to encrypt your key file into Authentication header value. See below for detail for encryption.
Establishing a Token-Based Connection to APNs | Apple Developer Documentation
