Mobile Push Notifications with PubNub
In this tutorial, you'll discover how to add mobile push notifications to a chat application.
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)
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.
git clone https://github.com/PubNubDevelopers/ios-push-notifications-tutorial.git
Checkout the base-application branch to begin working with the source application.
git checkout base-application
Watch the video or follow the steps below to set up your environment.
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.
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>.p8will 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.
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.
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.
https://github.com/pubnub/chat-components-ios
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.
import PubNub //Ensure the PubNub library is imported
import UserNotifications //Add this one.
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.
Create keys
My own keys
To use your own keys, create a free account and a keyset with Presence, Message Persistence, Stream Controller, and Mobile Push Notifications on.
You can create the keyset with the Admin API, PubNub's REST API for account management. An Account Owner or Account Admin creates a Service Integration and its API key in the Admin Portal under Organization Settings → API Management. Give it read & write access to apps and keysets, plus read access to secret keys, which keyset creation requires. With that key, create an app, then create a keyset in it and pass the keyset's configuration in the same request. The response includes the publish and subscribe keys. Push credentials aren't part of the keyset configuration: after you create the keyset, upload your APNs authentication token with the Admin API's upload operation. See the Admin API documentation for the requests and keyset settings.
Build and run
Congratulations - what's next?
In this tutorial, you looked at how to add mobile push notifications to an iOS Swift chat application using PubNub. You created an APNs Authentication Token File, set up a PubNub Account, configured and integrated mobile push notifications using APNs into an existing chat application, and successfully generated mobile push notifications to be received in a physical device running the application. The complete application of this tutorial is available in the GitHub Repository, in the push-notifications branch. The source code contains everything performed in this tutorial.
Mobile push notifications are a great way to notify online and offline users about appointments, updates, previews of conversations, special offers, and so much more. Combine this with PubNub’s real-time communication platform, and you have a complete application experience, giving you a way to continually engage and offer value to your users. PubNub’s Mobile Push Gateway makes it easy to send mobile push notifications to your users, by acting as a bridge between the service providers and your application, meaning you don’t have to use different frameworks and SDKs to send different types of mobile push notifications: you can do it all in one place, and let PubNub take care of the rest.
See how PubNub compares
PubNub Vs SendbirdPubNub Vs PusherPubNub Vs StreamExplore our chat docs
PubNub iOS Swift SDK Documentation HomePubNub iOS Chat Components Documentation HomeiOS Push Notifications Documentation OverviewReady to get beyond Proof of Concept?
Schedule a 15 minute chat with one of our experts to get your project moving
Talk to an ExpertTake it to production on your own keys
Create a free PubNub account to get permanent keys for this app.