Mobile Push Notifications for iOS Swift Environment Setup

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.

Prerequisites

You'll need the following before you begin:

  • Xcode (The latest stable version)

  • Git

  • Apple Developer Account with access to the Apple Developer Program.

    • 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 Source Application

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.

1

Checkout the base-application branch to begin working with the source application.

1

Watch the video or follow the steps below to set up your environment.

1

Create APNs Authentication Token

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.

  1. Navigate to your Apple Developer Account.

  2. In the Program Resources section, click Certificates, IDs & Profiles.

  3. Click on the Keys tab. You will create your APNs Authentication Token.

  4. Register a new key by providing a name. Enable Apple Push Notifications service (APNs) by checking the checkbox. Click Continue.

  5. Click Continue, and download the token. The token file AuthKey_<key id>.p8 will be used later on when creating your PubNub keys.

Enable Mobile Push Notifications in Xcode

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.

  1. Open your project in Xcode. Sign in using your developer account if you have not done so.

  2. In the project navigator, click on the project file. In the Targets section, click on your project.

  3. 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.

  4. Click on the Signing & Capabilities tab.

  5. Ensure that the checkbox for "Automatically manage signing" is checked.

  6. In the Signing section, ensure that your developer account is selected in the Team drop-down.

  7. Ensure the Bundle Identifier is the same as that in the General tab.

  8. 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.

Xcode project settings with selected 'Signing & Capabilities' tab showing team and bundle identifier configuration.

Add Packages

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 PubNubChat and 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 Device

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.

BackStep 1 of 4Next