This tutorial will guide you through how to add mobile push notifications to an iOS Swift chat application. You'll be using PubNub to construct and send push notification payloads to the Apple Push Notification Service (APNs) to deliver the notifications to your device.
You'll need the following before you begin:
Xcode (The latest stable version)
You'll need to be enrolled in this program in order to access services that allow your application to receive mobile push notifications from APNs.
Please note that this is a paid program.
Physical iOS Device (iPhone, iPad, iPod Touch, etc)
Simulated devices in Xcode cannot be registered in APNs, and thus cannot receive a device token, which is necessary when sending mobile push notifications across the PubNub network.
Download PubNub's getting started iOS Swift chat app from the GitHub Repository. This 1:1 chat application utilizes the community supported Chat Components, which are pre-built UI and data building blocks to quickly build a proof of concept or get your application to market.
In a terminal, navigate to an empty folder of your choosing. Enter the following command to clone the Repository.
Checkout the base-application branch to begin working with the source application.
Watch the video or follow the steps below to set up your environment.
You will be using the Apple Push Notification Service (APNs) as the push service provider to deliver mobile push notifications to the application. You need to first create an APNs Authentication Token which is required if you want to receive mobile push notifications.
Navigate to your Apple Developer Account.
In the Program Resources section, click Certificates, IDs & Profiles.
Click on the Keys tab. You will create your APNs Authentication Token.
Register a new key by providing a name. Enable Apple Push Notifications service (APNs) by checking the checkbox. Click Continue.
Click Continue, and download the token. The token file
AuthKey_<key id>.p8 will be used later on when creating your PubNub keys.
For APNs to be able to deliver mobile push notifications to your application, you'll need to uniquely identify your application and enable the Push Notifications Capability in the Xcode project.
Open your project in Xcode. Sign in using your developer account if you have not done so.
In the project navigator, click on the project file. In the Targets section, click on your project.
In the General tab, enter a unique Bundle Identifier by appending something like -push at the end of the ID. APNs identifies your application in a provisioning profile by combining a Team ID (generated by Apple) and your Bundle Identifier as an explicit App ID to be able to use certain capabilities, including receiving mobile push notifications for your application.
Click on the Signing & Capabilities tab.
Ensure that the checkbox for "Automatically manage signing" is checked.
In the Signing section, ensure that your developer account is selected in the Team drop-down.
Ensure the Bundle Identifier is the same as that in the General tab.
Add Push Notification support to your application by clicking on the
+ symbol, search for "push notifications", and then double click the Capability to add to your application.
This application utilizes the PubNub Swift SDK and Chat Components for iOS libraries. Add these packages by clicking on File -> Add Packages and entering the following Package URL.
Ensure you select both the
PubNubChatComponents library package products. Click Add Package to add them to your application.
Navigate to AppDelegate.swift. At the top of the file, ensure that the PubNub library necessary to be able to use the PubNub Swift SDK is already imported. Add the UserNotifications library to be able to use mobile push notifications in your project.
Connect your physical Apple Device to your computer. A pop-up in Xcode will ask if you want to register your device. Be sure to register to do so, as it will be linked to your Apple Developer Account.