| copyright | lastupdated | ||
|---|---|---|---|
|
2018-11-14 |
{:java: #java .ph data-hd-programlang='java'} {:swift: #swift .ph data-hd-programlang='swift'} {:ios: #ios data-hd-operatingsystem="ios"} {:android: #android data-hd-operatingsystem="android"} {:shortdesc: .shortdesc} {:new_window: target="_blank"} {:codeblock: .codeblock} {:screen: .screen} {:tip: .tip} {:pre: .pre}
Learn how easy it is to quickly create an iOS Swift application with high-value mobile service {{site.data.keyword.mobilepushshort}} on {{site.data.keyword.Bluemix_short}}.
This tutorial walks you through the creation of a mobile starter application, adding a mobile service, setting up client SDK, importing the code to Xcode and then further enhance the application. {:shortdesc: .shortdesc}
{:#objectives}
-
Create a mobile app with {{site.data.keyword.mobilepushshort}} and {{site.data.keyword.mobileanalytics_short}} services from Basic Swift starter kit.
-
Obtain APNs credentials and configure {{site.data.keyword.mobilepushshort}} service instance.
-
Download the code and setup client SDK.
-
Send and monitor {{site.data.keyword.mobilepushshort}}.
{:#products}
This tutorial uses the following products:
{: #prereqs}
- Apple Developers
account to send remote notifications from {{site.data.keyword.mobilepushshort}} service instance on {{site.data.keyword.Bluemix_short}} (the provider) to iOS devices and applications.
- Xcode for importing and enhancing your code.
{: #get_code}
- Navigate to Mobile Dashboard to create your
Appfrom pre-definedStarter Kits. - Click on Starter Kits and scroll down to select Basic Starter Kit.

- Enter an app name which will also be the Xcode project and app name.
- Select
iOS Swiftas your platform and click Create.
- Click on Add Resource > Mobile > Push Notifications and select the location you want to provision the service, resource group and Lite pricing plan.
- Click Create to provision {{site.data.keyword.mobilepushshort}} service. A new App will be created under Apps tab.
Note: {{site.data.keyword.mobilepushshort}} service should already be added with the Empty Starter.
{: #download_code}
If you haven't downloaded the code yet, Click on Download Code under Apps > Your Mobile App
The downloaded code comes with {{site.data.keyword.mobilepushshort}} Client SDK included. The Client SDK is available on CocoaPods and Carthage. For this solution, you will use CocoaPods.
-
To install CocoaPods on your machine, Open the
Terminaland run the below command.sudo gem install cocoapods{: pre:}
-
Unzip the downloaded code and using the terminal, navigate to the unzipped folder
cd <Name of Project>{: pre:}
-
The folder already includes a
podfilewith required dependencies. Run the below command to install the dependencies (Client SDKs) and the required dependencies will be installed.
pod install
{: pre:}
{: #obtain_apns_credentials}
For iOS devices and applications, Apple Push Notification Service (APNs) allows application developers to send remote notifications from {{site.data.keyword.mobilepushshort}} service instance on {{site.data.keyword.Bluemix_short}} (the provider) to iOS devices and applications. Messages are sent to a target application on the device.
You need to obtain and configure your APNs credentials. The APNs certificates are securely managed by {{site.data.keyword.mobilepushshort}} service and used to connect to APNs server as a provider.
The App ID (the bundle identifier) is a unique identifier that identifies a specific application. Each application requires an App ID. Services like the {{site.data.keyword.mobilepushshort}} service are configured to the App ID.
Ensure that you have an Apple Developers account. This is a mandatory prerequisite.
- Go to the Apple Developer
portal, click
Member Center, and selectCertificates, IDs & Profiles. - Go to
Identifiers> App IDs section. - In the
Registering App IDspage, provide the App name in the App ID Description Name field. For example: ACME {{site.data.keyword.mobilepushshort}}. Provide a string for the App ID Prefix. - For the App ID Suffix, choose
Explicit App IDand provide a Bundle ID value. It is recommended that you provide a reverse domain-name style string. For example: com.ACME.push. - Select the
{{site.data.keyword.mobilepushshort}}check-box and clickContinue. - Go through your settings and click
Register>Done. Your App ID is now registered.

Before you obtain an APNs certificate, you must first generate a certificate signing request (CSR) and submit it to Apple, the certificate authority (CA). The CSR contains information that identifies your company and your public and private key that you use to sign for your Apple {{site.data.keyword.mobilepushshort}}. Then, generate the SSL certificate on the iOS Developer Portal. The certificate, along with its public and private key, is stored in Keychain Access. You can use APNs in two modes:
-
Sandbox mode for development and testing.
-
Production mode when distributing applications through the App Store (or other enterprise distribution mechanisms).
You must obtain separate certificates for your development and distribution environments. The certificates are associated with an App ID for the app that is the recipient of remote notifications. For production, you can create up to two certificates. {{site.data.keyword.Bluemix_short}} uses the certificates to establish an SSL connection with APNs.
- Go to the Apple Developer website, click Member Center, and select Certificates, IDs & Profiles.
- In the Identifiers area, click App IDs.
- From your list of App IDs, select your App ID, then select
Edit. - Select the {{site.data.keyword.mobilepushshort}} check-box, and then on Development SSL certificate pane, click Create Certificate.
-
When the About Creating a Certificate Signing Request (CSR) screen displays, follow the instructions shown to create a Certificate Signing Request (CSR) file. Give a meaningful name that helps you identify whether it is a certificate for development (sandbox) or distribution (production); for example, sandbox-apns-certificate or production-apns-certificate.
-
Click Continue and on the Upload CSR file screen, click Choose File, and select the file CertificateSigningRequest.certSigningRequest you just created. Click Continue.
-
On the Download, Install and Backup pane, click Download. The aps_development.cer file is downloaded.

-
On your mac, open Keychain Access, File, Import and select the downloaded .cer file to install it.
-
Right-click on the new certificate and private key, and then select Export and change the File Format to Personal information exchange format (
.p12format).
-
In the Save As field, provide the certificate a meaningful name. For example,
sandbox_apns.p12or production_apns.p12, then click Save.
-
In the Enter a password field, enter a password to protect the exported items, then click OK. You can use this password to configure your APNs settings on the {{site.data.keyword.mobilepushshort}} service console.

-
The Key Access.app prompts you to export your key from the Keychain screen. Enter your administrative password for your Mac to allow your system to export these items, and then select the
Always Allowoption. A.p12certificate is generated on your desktop.
For Production SSL, On **Production SSL certificate** pane, click **Create Certificate** and repeat Steps 5 to 12 above. {:tip}
The provisioning profile works with the App ID to determine which devices can install and run your app and which services your app can access. For each App ID, you create two provisioning profiles: one for development and the other for distribution. Xcode uses the development provisioning profile to determine which developers are allowed to build the application and which devices are allowed to be tested on the application.
Ensure that you have registered an App ID, enabled it for {{site.data.keyword.mobilepushshort}} service, and configured it to use a development and production APNs SSL certificate.
Create a development provisioning profile, as follows:
- Go to the Apple Developer portal, click
Member Center, and selectCertificates, IDs & Profiles. - Go to the Mac Developer Library, scroll to the
Creating Development Provisioning Profilessection, and follow the instructions to create a development profile. Note: When you configure a development provision profile, select the following options:
- **iOS App Development**
- **For iOS and watchOS apps**
Use the store provisioning profile to submit your app for distribution to the App Store.
- Go to the Apple Developer, click
Member Center, and selectCertificates, IDs & Profiles. - Double-click the downloaded provisioning profile to install it into Xcode. After obtaining the credentials, the next step is to Configure a service instance.
To use the {{site.data.keyword.mobilepushshort}} service to send notifications, upload the .p12 certificates that you had created in the above Step. This certificate contains the private key and SSL certificates that are required to build and publish your application.
Note: After the .cer file is in your key chain access, export it to your computer to create a .p12 certificate.
- Click on {{site.data.keyword.mobilepushshort}} under Services section or Click on the three vertical dots next to the {{site.data.keyword.mobilepushshort}} service and select
Open dashboard. - On the {{site.data.keyword.mobilepushshort}} Dashboard, you should see
Configureoption underManage.
To set up APNs on the Push Notification services console, complete the steps:
- Choose the
Mobile optionto update the information in the APNs Push Credentials form. - Select
Sandbox/Development APNs ServerorProduction APNs Serveras appropriate and then upload the.p12certificate that you have created. - In the Password field, enter the password that is associated with the .p12 certificate file, then click Save.
{: #configure_push}
- Push initialization code (under
func application) and notification registration code can be found inAppDelegate.swift. Provide a unique USER_ID(Optional). - Run the app on a physical device as notifications can't be sent to an iPhone Simulator.
- Open {{site.data.keyword.mobilepushshort}} service under
Mobile Services> Existing services on {{site.data.keyword.Bluemix_short}} Mobile dashboard and to send basic {{site.data.keyword.mobilepushshort}}, complete the following steps:
-
Select
Messages, and compose a message by choosing a Send to option. The supported options are Device by Tag, Device Id, User Id, Android devices, iOS devices, Web Notifications, All Devices and other browsers.Note: When you select the All Devices option, all devices subscribed to {{site.data.keyword.mobilepushshort}} will receive notifications.
-
In the
Messagefield, compose your message. Choose to configure the optional settings as required. -
Click
Sendand verify that your physical devices has received the notification.
-
You should see a notification on your iPhone.
-
You can monitor your sent notifications by navigating to
Monitoringon the {{site.data.keyword.mobilepushshort}} Service.
The IBM {{site.data.keyword.mobilepushshort}} service now extends capabilities to monitor the push performance by generating graphs from your user data. You can use the utility to list all the sent {{site.data.keyword.mobilepushshort}}, or to list all the registered devices and to analyze information on a daily, weekly, or monthly basis.

{: #related_content}




