{"id":823,"date":"2014-11-16T22:30:36","date_gmt":"2014-11-17T04:30:36","guid":{"rendered":"http:\/\/dev.iachieved.it\/iachievedit\/?p=823"},"modified":"2016-01-29T08:04:24","modified_gmt":"2016-01-29T14:04:24","slug":"push-notifications-with-parse-and-swift","status":"publish","type":"post","link":"https:\/\/dev.iachieved.it\/iachievedit\/push-notifications-with-parse-and-swift\/","title":{"rendered":"Push Notifications with Parse and Swift"},"content":{"rendered":"<p><b>Update 1\/29\/2016:<\/b>  Parse announced yesterday <a href=\"http:\/\/blog.parse.com\/announcements\/moving-on\/\">in their blog<\/a> that they will be fully shutting down operations on January 28, 2017.  Consider using a service like Amazon.com <a href=\"https:\/\/aws.amazon.com\/sns\/\">SNS<\/a> or <a href=\"https:\/\/pushover.net\">Pushover<\/a>.<\/p>\n<p><i>Editor&#8217;s Note:  This is part one in a three part series.  The goal in this series is to develop a fully functional (and useful) iOS application that is capable of pushing weather alerts based on your location to your iOS device.  The series is broken down as follows:<\/p>\n<ul>\n<li>Part One &#8211; Developing a Push Notification-capable application with Parse and Swift\n<li>Part Two &#8211; Using CoreLocation with Swift\n<li>Part Three &#8211; Integrating in a Weather Service API\n<\/ul>\n<p><\/i><\/p>\n<p>In this first part of our three part series we&#8217;ll be developing an application which is capable of receiving iOS push notifications.  Rather than invent our own push notification application server, we&#8217;ll leverage <a href=\"https:\/\/parse.com\">Parse<\/a> to provide this capability.  Our first warning:  this tutorial is rather lengthy and involved, and it requires some knowledge about using the <a href=\"https:\/\/developer.apple.com\">Apple Developer Portal<\/a> to create certificates, provisioning profiles, and register iOS devices for development.  If you&#8217;ve never gone through this process before I highly recommend a quick read of <a href=\"http:\/\/dojo4.com\/blog\/beginner-tutorial-i-os-certificates-provisioning-profiles\">this post<\/a>.<\/p>\n<h2>Our Application<\/h2>\n<p>There are a number of iOS weather applications on the AppStore.  If you think about it, a large number of mobile phone applications mirror the sections of the newspaper you were used to growing up as a kid (because as an adult I&#8217;m betting you don&#8217;t read the newspaper).  The Business section had stocks, the Sports section had all the football scores, and of course the Weather page had the weather and the forecast.  The basic information we desire to receive hasn&#8217;t changed much; what has changed is how we receive it and how timely and contextual it is.  I personally use <b>The Weather Channel<\/b> application and have my <b>Severe Weather<\/b> set to Dallas, TX.  Whenever there are severe weather alerts for North Texas I receive a push notification to my phone alerting me to the fact.<\/p>\n<p>But what I really want is weather alerts based upon my current location.  If I&#8217;m traveling across the country, whether by plane, train, or automobile, it would be nice to know if there are sudden weather alerts based upon where I am, not where I live.  This is the type of application we&#8217;re going to develop in this tutorial, and we&#8217;ll do it using very basic iOS development techniques, and we&#8217;ll do it all in Swift.  At the end of this tutorial you won&#8217;t have an application that you&#8217;ll be able to rush out and publish to the AppStore, because it won&#8217;t have a functional UI (it won&#8217;t have a UI at all), but it will give you a framework to build something upon if you choose to do so.  What it will provide are all the details necessary to learn how to use Parse for push notifications, CoreLocation for location, and integrating with a REST API for receiving weather alerts posted by NOAA.<\/p>\n<h2>Getting Started<\/h2>\n<p>To get started let&#8217;s create a new project in Xcode (we are using Xcode 6.1).  Select the <b>Single View<\/b> iOS application template, and choose Swift as your language.  Name the <b>Product Name<\/b> <code>weatheralerts<\/code>.  Make note of the bundle identifier, in our case its <code>it.iachieved.weatheralerts<\/code>, but yours will undoubtedly be different (like <code>com.yourcompany.weatheralerts<\/code>).<\/p>\n<p><a href=\"http:\/\/dev.iachieved.it\/iachievedit\/wp-content\/uploads\/2014\/11\/tut_new_project_options.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/dev.iachieved.it\/iachievedit\/wp-content\/uploads\/2014\/11\/tut_new_project_options.png\" alt=\"tut_new_project_options\" width=\"735\" height=\"433\" class=\"alignnone size-full wp-image-862\" \/><\/a><\/p>\n<p>Normally I wouldn&#8217;t harp on this, but when dealing with push notifications it is important to ensure that you make note of the <b>bundle identifier<\/b>.  It is important when configuring push notifications in the developer portal that you use the same bundle identifier as what you used in your project.<\/p>\n<p>Since we are going to be using API keys in our application and don&#8217;t want to commit them to Github, we&#8217;re going to use our documented technique for storing API keys in a <i>property list<\/i> file.  Take a moment to follow <a href=\"http:\/\/dev.iachieved.it\/iachievedit\/?p=762\">our tutorial<\/a> for creating your <code>ApiKeys.plist<\/code> and <code>ApiKeys.swift<\/code> files for this project.  The remainder of this tutorial will assume you&#8217;ve used this technique.<\/p>\n<h2>Signing Up With Parse<\/h2>\n<p>We&#8217;ll be using Parse for sending push notifications to our application.  Parse requires you to sign up to use it&#8217;s service, so head on over <a href=\"https:\/\/www.parse.com\/#signup\">sign up<\/a>.<\/p>\n<p><a href=\"http:\/\/dev.iachieved.it\/iachievedit\/wp-content\/uploads\/2014\/11\/tut_signup_for_parse.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/dev.iachieved.it\/iachievedit\/wp-content\/uploads\/2014\/11\/tut_signup_for_parse.png\" alt=\"tut_signup_for_parse\" width=\"578\" height=\"367\" class=\"alignnone size-full wp-image-864\" \/><\/a><\/p>\n<p>Once you&#8217;ve signed up browse over to the <a href=\"https:\/\/parse.com\/apps\/quickstart\">QuickStart<\/a> page and create a basic application.  Name the application <code>weatheralerts<\/code> and then select the big <b>Push<\/b> icon:<\/p>\n<p><a href=\"http:\/\/dev.iachieved.it\/iachievedit\/wp-content\/uploads\/2014\/11\/tut_hello_push.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/dev.iachieved.it\/iachievedit\/wp-content\/uploads\/2014\/11\/tut_hello_push.png\" alt=\"tut_hello_push\" width=\"259\" height=\"202\" class=\"alignnone size-full wp-image-865\" \/><\/a><\/p>\n<p>Select the big iOS icon (they make it so cute), and then select <b>Existing Project<\/b>.<\/p>\n<p><a href=\"http:\/\/dev.iachieved.it\/iachievedit\/wp-content\/uploads\/2014\/11\/tut_parse_existing_project.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/dev.iachieved.it\/iachievedit\/wp-content\/uploads\/2014\/11\/tut_parse_existing_project.png\" alt=\"tut_parse_existing_project\" width=\"440\" height=\"306\" class=\"alignnone size-full wp-image-866\" \/><\/a><\/p>\n<p>Here you should see a pleasant blue background outlining steps to obtain the Parse SDK, adding it to your application, etc.  Follow the instructions for downloading, unzipping, and adding the Parse SDK to your project, but stop at the section titled <b>Connect your app to Parse<\/b> and instead follow these instructions for using Parse with Swift.  To be clear, before proceeding with the next section you should have:<\/p>\n<ul>\n<li>Downloaded and unzipped the Parse SDK\n<li>Dragged and dropped <code>Parse.framework<\/code> and <code>Bolts.framework<\/code> into your Xcode project, following the instructions to copy files\n<li>Added all of the required iOS frameworks as outlined in the Parse instructions (there are quite a few)\n<\/ul>\n<h2>Using Parse<\/h2>\n<p>To use Parse with our Swift application we&#8217;re going to need to create a <a href=\"https:\/\/developer.apple.com\/library\/ios\/documentation\/swift\/conceptual\/buildingcocoaapps\/MixandMatch.html\">bridging header<\/a>.  This is due to the fact that Parse is (currently) an Objective-C framework, as is 99% of the established frameworks out there as of this writing.  Swift simply hasn&#8217;t been around long enough to have a large number of native frameworks.  To create a bridging header with Xcode go to <b>File &#8211; New &#8211; File<\/b> and in the iOS Source templates choose <b>Header File<\/b>.  Name it <b>bridgingHeader<\/b> (the name doesn&#8217;t matter actually as long as your project is configured properly to use it) and Xcode will place it in your project.<\/p>\n<p>In <code>bridgingHeader.h<\/code> add <code>#import <Parse\/Parse.h><\/code> such that the file looks something like:<\/p>\n<p>[objc]<br \/>\n#ifndef weatheralerts_bridgingHeader_h<br \/>\n#define weatheralerts_bridgingHeader_h<\/p>\n<p>#import &lt;Parse\/Parse.h&gt;<\/p>\n<p>#endif<br \/>\n[\/objc]<\/p>\n<p>Our bridging header is in place, but you have to configure it in your project.  Click on your application target and select the <b>Build Settings<\/b> tab.  In the search box type <code>bridging<\/code> to find the configuration entry quickly.  For the bridging header path you can type in <code>$(SRCROOT)\/$(PROJECT_NAME)\/bridgingHeader.h<\/code>.<\/p>\n<p><a href=\"http:\/\/dev.iachieved.it\/iachievedit\/wp-content\/uploads\/2014\/11\/tut_configure_bridging_header.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/dev.iachieved.it\/iachievedit\/wp-content\/uploads\/2014\/11\/tut_configure_bridging_header.png\" alt=\"tut_configure_bridging_header\" width=\"884\" height=\"321\" class=\"alignnone size-full wp-image-875\" \/><\/a><\/p>\n<p>Now, because this is a multipart tutorial that will culminate in an application that can send you NOAA weather alerts, we&#8217;re going to go ahead and add things that aren&#8217;t necessary for a basic push notification application.  The pattern is a useful one, however, and can be adapted for your own specific project needs.<\/p>\n<p>We&#8217;re going to use a <i>push notification controller<\/i> to encapsulate our push notification needs.  Go ahead and create a Swift file named <code>PushNotificationController<\/code> and add the following boilerplate code to it:<\/p>\n<p>[objc]<br \/>\nimport Foundation<\/p>\n<p>class PushNotificationController : NSObject {<\/p>\n<p>  override init() {<br \/>\n    super.init()<br \/>\n  }<\/p>\n<p>}<br \/>\n[\/objc]<\/p>\n<p>Simple enough.  Now we can pick up where we left off with Parse.<\/p>\n<p>The first step in our application is going to be to set Parse&#8217;s application ID and client key to those values provided to us.  Whereas the Parse tutorial has us placing these values in our application delegate&#8217;s <code>didFinishLaunchingWithOptions<\/code> function, we&#8217;re going to place them in the constructor (in other words, the <code>init<\/code> function) of our <code>PushNotificationController<\/code>.  First, however, we want our keys in our <code>ApiKeys.plist<\/code> file.  We&#8217;ll refer to them as <code>PARSE_APPLICATION_ID<\/code> and <code>PARSE_CLIENT_KEY<\/code>.  Again, we&#8217;re using a specific technique for managing API keys outlined <a href=\"http:\/\/dev.iachieved.it\/iachievedit\/?p=762\">here<\/a>.<\/p>\n<p><a href=\"http:\/\/dev.iachieved.it\/iachievedit\/wp-content\/uploads\/2014\/11\/tut_parse_api_keys.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/dev.iachieved.it\/iachievedit\/wp-content\/uploads\/2014\/11\/tut_parse_api_keys.png\" alt=\"tut_parse_api_keys\" width=\"641\" height=\"108\" class=\"alignnone size-full wp-image-868\" \/><\/a><\/p>\n<p>Using our <code>valueForAPIKey<\/code> function provided in <code>ApiKeys.swift<\/code> we can configure Parse as follows:<\/p>\n<p>[objc]<br \/>\n  override init() {<br \/>\n    super.init()<\/p>\n<p>    let parseApplicationId = valueForAPIKey(keyname: &quot;PARSE_APPLICATION_ID&quot;)<br \/>\n    let parseClientKey     = valueForAPIKey(keyname: &quot;PARSE_CLIENT_KEY&quot;)<\/p>\n<p>    Parse.setApplicationId(parseApplicationId, clientKey: parseClientKey)<\/p>\n<p>  }<br \/>\n[\/objc]<\/p>\n<p>Now we must switch over to the <code>AppDelegate<\/code> class and register for push notifications.  We want to create an instance of the <code>PushNotificationController<\/code> in our application delegate, and we can do so by adding:<\/p>\n<p>[objc]<br \/>\nvar pushNotificationController:PushNotificationController?<br \/>\n[\/objc]<\/p>\n<p>right beneath the <code>var window: UIWindow?<\/code> declaration, and then in the <code>application(application:didFinishLaunchingWithOptions:)<\/code> function add:<\/p>\n<p>[objc]<br \/>\n    self.pushNotificationController = PushNotificationController()<\/p>\n<p>    \/\/ Register for Push Notitications, if running iOS 8<br \/>\n    if application.respondsToSelector(&quot;registerUserNotificationSettings:&quot;) {<\/p>\n<p>      let types:UIUserNotificationType = (.Alert | .Badge | .Sound)<br \/>\n      let settings:UIUserNotificationSettings = UIUserNotificationSettings(forTypes: types, categories: nil)<\/p>\n<p>      application.registerUserNotificationSettings(settings)<br \/>\n      application.registerForRemoteNotifications()<\/p>\n<p>    } else {<br \/>\n      \/\/ Register for Push Notifications before iOS 8<br \/>\n      application.registerForRemoteNotificationTypes(.Alert | .Badge | .Sound)<br \/>\n    }<br \/>\n[\/objc]<\/p>\n<p>Note that due to differences between push notification registration in iOS 8 and iOS 7 we have to determine if the new iOS 8 method is available (<code>registerUserNotificationSettings:<\/code>).  If it is we use the iOS 8 method, otherwise iOS 7.  See <a href=\"http:\/\/stackoverflow.com\/questions\/25909568\/ios-8-enabled-device-not-receiving-push-notifications-after-code-update\">Stackoverflow<\/a> for additional information.<\/p>\n<p>Finally, in our application delegate (<code>AppDelegate<\/code>) we&#8217;re going to add 3 more functions:<\/p>\n<ul>\n<li><code>didRegisterForRemoteNotificationsWithDeviceToken<\/code>\n<li><code>didFailToRegisterForRemoteNotificationsWithError<\/code>\n<li><code>didReceiveRemoteNotification<\/code>\n<\/ul>\n<p>It&#8217;s important to go ahead and implement <code>didFailToRegisterForRemoteNotificationsWithError<\/code> because odds are there will be some misconfiguration with your first attempt at push notifications, and it&#8217;s helpful to know right away what that might be.<\/p>\n<p>Here&#8217;s the implementation for these routines:<\/p>\n<p>[objc]<br \/>\n  func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {<br \/>\n    println(&quot;didRegisterForRemoteNotificationsWithDeviceToken&quot;)<\/p>\n<p>    let currentInstallation = PFInstallation.currentInstallation()<\/p>\n<p>    currentInstallation.setDeviceTokenFromData(deviceToken)<br \/>\n    currentInstallation.saveInBackgroundWithBlock { (succeeded, e) -&gt; Void in<br \/>\n      \/\/code<br \/>\n    }<br \/>\n  }<\/p>\n<p>  func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError) {<br \/>\n    println(&quot;failed to register for remote notifications:  \\(error)&quot;)<br \/>\n  }<\/p>\n<p>  func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {<br \/>\n    println(&quot;didReceiveRemoteNotification&quot;)<br \/>\n    PFPush.handlePush(userInfo)<br \/>\n  }<br \/>\n[\/objc]<\/p>\n<p>We&#8217;ll look at each of these in turn momentarily.  Go ahead and try to run the application as it is on your iPhone.  If everything has been configured correctly thus far you should get a popup requesting push notification access.<\/p>\n<p><a href=\"http:\/\/dev.iachieved.it\/iachievedit\/wp-content\/uploads\/2014\/11\/tut_would_like_to_send.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/dev.iachieved.it\/iachievedit\/wp-content\/uploads\/2014\/11\/tut_would_like_to_send.png\" alt=\"tut_would_like_to_send\" width=\"320\" height=\"568\" class=\"alignnone size-full wp-image-870\" \/><\/a><\/p>\n<p>However, even after pressing on <b>OK<\/b> you will likely see in the console log:<\/p>\n<pre>\nfailed to register for remote notifications:  Error Domain=NSCocoaErrorDomain Code=3000 \"no valid 'aps-environment' entitlement string found for application\" UserInfo=0x1464c610 {NSLocalizedDescription=no valid 'aps-environment' entitlement string found for application}\n<\/pre>\n<p><b>An Aside:  <\/b>If you ever find yourself testing push notifications and would like to trigger the dialog requesting allowance to send push notifications a second time, you&#8217;re in for the real treat of <a href=\"https:\/\/developer.apple.com\/library\/ios\/technotes\/tn2265\/_index.html#\/\/apple_ref\/doc\/uid\/DTS40010376-CH1-TNTAG42\">changing the time on your iPhone<\/a>.  No kidding.<\/p>\n<p>To actually allow for push notifications to be sent to the device we need to turn to the Apple Developer portal and follow a multi-step process (and in this order):<\/p>\n<ul>\n<li>Create a new AppId for our application\n<li>Enable Push Notifications for our newly created application\n<li>Create our Push Notification security certificate\n<li>Create a Provisioning Profile for our application\n<li>Download and install the Provisioning Profile\n<li>Configure our project to use our new Provisioning Profile\n<\/ul>\n<h2>Provisioning in the Developer Portal<\/h2>\n<p>Let&#8217;s start with our AppId.  Go to the <a href=\"https:\/\/developer.apple.com\/membercenter\/index.action\">Apple Developer Portal Member Center<\/a> and navigate to the <b>Certificates, Identifiers &amp; Profiles<\/b> page.  Select <b>Identifiers<\/b> and choose <b>App Ids<\/b>.<\/p>\n<p><a href=\"http:\/\/dev.iachieved.it\/iachievedit\/wp-content\/uploads\/2014\/11\/tut_new_app_id.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/dev.iachieved.it\/iachievedit\/wp-content\/uploads\/2014\/11\/tut_new_app_id.png\" alt=\"tut_new_app_id\" width=\"242\" height=\"211\" class=\"alignnone size-full wp-image-834\" \/><\/a><\/p>\n<p>You should see a plus icon to the right of the title <b>iOS App IDs<\/b>, select that plus sign to register a new AppId.<\/p>\n<p><a href=\"http:\/\/dev.iachieved.it\/iachievedit\/wp-content\/uploads\/2014\/11\/tut_create_app_id.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/dev.iachieved.it\/iachievedit\/wp-content\/uploads\/2014\/11\/tut_create_app_id.png\" alt=\"tut_create_app_id\" width=\"696\" height=\"596\" class=\"alignnone size-full wp-image-835\" \/><\/a><\/p>\n<p>There are some key things to note here:<\/p>\n<ul>\n<li>The <b>App ID Description<\/b> can be a plain-text description, such as Weather Alert Application\n<li>The <b>App ID Prefix<\/b> is chosen for you based upon what iOS development team you are a member of\n<li>Select <b>Explicit App ID<\/b> for this tutorial and ensure your <b>Bundle ID<\/b> is the same as the bundle id of your application\n<\/ul>\n<p>The last item cannot be stressed enough, if the bundle identifiers do not match you are in for a headache.  In our example we used <code>it.iachieved.weatheralerts<\/code>.  Scroll down to the bottom (we are going to ignore <b>App Services<\/b> for the moment and configure it later) and click <b>Continue<\/b>.  On the <b>Confirm your App ID<\/b> page click <b>Submit<\/b>.<\/p>\n<h3>Configuring the application for push notifications<\/h3>\n<p>Although after creating the application ID the portal tells us <i>This App ID is now registered to your account and can be used in your provisioning profiles.<\/i> we need to actually configure it for push notifications.  Go back to the App IDs page and select your application.  Scroll to the bottom of the <b>Application Services<\/b> list and click on the <b>Edit<\/b> button.<\/p>\n<p>Scroll down again until you find the <b>Push Notifications<\/b> service and click on the checkbox to the left to enable it, and then click on <b>Create Certificate<\/b> in the section marked <b>Development SSL Certificate<\/b>.<\/p>\n<p><a href=\"http:\/\/dev.iachieved.it\/iachievedit\/wp-content\/uploads\/2014\/11\/tut_enable_push_notifications.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/dev.iachieved.it\/iachievedit\/wp-content\/uploads\/2014\/11\/tut_enable_push_notifications.png\" alt=\"tut_enable_push_notifications\" width=\"687\" height=\"398\" class=\"alignnone size-full wp-image-840\" \/><\/a><\/p>\n<p>If you have never created a Certificate Signing Request the process is not difficult, but it is cumbersome and requires a number of manual steps.  Before continuing further I suggest you read in detail the text on the developer portal page that is presented to you.  That is, the page that begins with <i>To manually generate a Certificate Signing Request&#8230;<\/i>.  As the page instructs you need to open <b>KeyChain Access<\/b> and use the <b>Request a Certificate from a Certificate Authority<\/b> feature.<\/p>\n<p>As depicted below enter your e-mail address, and choose a <b>Common Name<\/b> that is descriptive for the purposes that this certificate will serve.  In our case I chose <b>WeatherAlertsDevelopmentPush<\/b> to remind me what I&#8217;m using this certificate for.  Choose <b>Saved to Disk<\/b>.<\/p>\n<p><a href=\"http:\/\/dev.iachieved.it\/iachievedit\/wp-content\/uploads\/2014\/11\/tut_request_cert.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/dev.iachieved.it\/iachievedit\/wp-content\/uploads\/2014\/11\/tut_request_cert.png\" alt=\"tut_request_cert\" width=\"609\" height=\"432\" class=\"alignnone size-full wp-image-839\" \/><\/a><\/p>\n<p>Continue the prompts in <b>Keychain Access<\/b> and once you&#8217;ve saved your certificate request to disk, click <b>Continue<\/b> in the development portal and then <b>Choose File&#8230;<\/b> to upload the request to the portal.  Once the file is uploaded click <b>Generate<\/b>.  The developer portal will then generate your certificate and prompt you to <b>Download, Install and Backup<\/b>.  Click <b>Download<\/b>.<\/p>\n<p><a href=\"http:\/\/dev.iachieved.it\/iachievedit\/wp-content\/uploads\/2014\/11\/tut_download_and_install.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/dev.iachieved.it\/iachievedit\/wp-content\/uploads\/2014\/11\/tut_download_and_install.png\" alt=\"tut_download_and_install\" width=\"708\" height=\"308\" class=\"alignnone size-full wp-image-841\" \/><\/a><\/p>\n<p>The file downloaded will be named something like <code>aps_development.cer<\/code> and placed in your <b>Downloads<\/b> folder.  Navigate to the file and open it.  Keychain Access will import the file and match it against the private key used by the certificate signing request.  In Keychain Access click on <b>My Certificates<\/b> and you can use the search field to find your new certificate:<\/p>\n<p><a href=\"http:\/\/dev.iachieved.it\/iachievedit\/wp-content\/uploads\/2014\/11\/tut_push_certificate.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/dev.iachieved.it\/iachievedit\/wp-content\/uploads\/2014\/11\/tut_push_certificate.png\" alt=\"tut_push_certificate\" width=\"675\" height=\"178\" class=\"alignnone size-full wp-image-842\" \/><\/a><\/p>\n<p>If you thought you were finished with push notification configuration, you are mistaken!  We told you there were a lot of steps involved.  Continuing in the portal we need to create our application provisioning profile.<\/p>\n<h3>Creating a Provisioning Profile<\/h3>\n<p>Select <b>Development<\/b> under <b>Provisioning Profiles<\/b> and click the plus icon to the right of <b>iOS Provisioning Profiles<\/b>.  When prompted choose a Development provisioning profile (the type used for installing development apps on test devices):<\/p>\n<p><a href=\"http:\/\/dev.iachieved.it\/iachievedit\/wp-content\/uploads\/2014\/11\/tut_what_type_of_pp.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/dev.iachieved.it\/iachievedit\/wp-content\/uploads\/2014\/11\/tut_what_type_of_pp.png\" alt=\"tut_what_type_of_pp\" width=\"739\" height=\"372\" class=\"alignnone size-full wp-image-844\" \/><\/a><\/p>\n<p>Click <b>Continue<\/b> to select the <b>App ID<\/b> of the application we will use with this provisioning profile.  In particular, select the application that matches the one you just created for <code>weatheralerts<\/code>.<\/p>\n<p><a href=\"http:\/\/dev.iachieved.it\/iachievedit\/wp-content\/uploads\/2014\/11\/tut_select_app_id.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/dev.iachieved.it\/iachievedit\/wp-content\/uploads\/2014\/11\/tut_select_app_id.png\" alt=\"tut_select_app_id\" width=\"736\" height=\"320\" class=\"alignnone size-full wp-image-845\" \/><\/a><\/p>\n<p>Now choose the developer certificate(s) you want to be able to use with this provisioning profile.  It is important to choose a <b>Development<\/b> certificate here, and one that you have already set up.  Finally, choose all the devices that you want to be able to run the application on.<\/p>\n<p><a href=\"http:\/\/dev.iachieved.it\/iachievedit\/wp-content\/uploads\/2014\/11\/tut_name_pp.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/dev.iachieved.it\/iachievedit\/wp-content\/uploads\/2014\/11\/tut_name_pp.png\" alt=\"tut_name_pp\" width=\"733\" height=\"379\" class=\"alignnone size-full wp-image-847\" \/><\/a><\/p>\n<p>Name the profile (I chose <b>WeatherAlerts Development<\/b>) and click <b>Generate<\/b>.<\/p>\n<p><a href=\"http:\/\/dev.iachieved.it\/iachievedit\/wp-content\/uploads\/2014\/11\/tut_download_and_install_pp.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/dev.iachieved.it\/iachievedit\/wp-content\/uploads\/2014\/11\/tut_download_and_install_pp.png\" alt=\"tut_download_and_install_pp\" width=\"670\" height=\"242\" class=\"alignnone size-full wp-image-848\" \/><\/a><\/p>\n<p>Download and install the provisioning profile using the instructions on the screen.<\/p>\n<p>Now that your provisioning profile is downloaded and installed go to your application target in Xcode, and choose <b>Build Settings<\/b>.  In the <b>Code Signing<\/b> section, choose your code signing identity (which matches the certificate you chose to use for the provisioning profile),<\/p>\n<p><a href=\"http:\/\/dev.iachieved.it\/iachievedit\/wp-content\/uploads\/2014\/11\/tut_codesigning_identity.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/dev.iachieved.it\/iachievedit\/wp-content\/uploads\/2014\/11\/tut_codesigning_identity.png\" alt=\"tut_codesigning_identity\" width=\"746\" height=\"255\" class=\"alignnone size-full wp-image-849\" \/><\/a><\/p>\n<p>If everything is configured correctly this time, you should see <code>didRegisterForRemoteNotificationsWithDeviceToken<\/code> in your console log.<\/p>\n<h2>Configuring Your Application in Parse<\/h2>\n<p>To allow Parse to push notifications to your app, you first have to configure it with your application&#8217;s <b>push notification certificate<\/b>.  This is the private key and certificate you created above in <b>Keychain Access<\/b>.  Locate the certificate in Keychain Access by selecting <b>My Certificates<\/b> and the search field.  Right-click the row that the certificate is on and choose <b>Export &#8220;Apple Development IOS Push Services:&#8230;&#8221;<\/b>.  Name the file WeatherAlertsDevPushCert and ensure that the file format for the export is <b>Personal Information Exchange<\/b>.  Save it to your Desktop, and <b>do not<\/b> enter a password to protect the exported items (this is important!).  Keychain Access may prompt you for your login keychain password to export the certificate.<\/p>\n<p>Go to your application&#8217;s Push Settings in your Parse console.  The screen should look something like this:<\/p>\n<p><a href=\"http:\/\/dev.iachieved.it\/iachievedit\/wp-content\/uploads\/2014\/11\/tut_parse.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/dev.iachieved.it\/iachievedit\/wp-content\/uploads\/2014\/11\/tut_parse.png\" alt=\"tut_parse\" width=\"1060\" height=\"535\" class=\"alignnone size-full wp-image-853\" \/><\/a><\/p>\n<p>In the section title <b>Apple Push Certificates<\/b> click on <b>Select your certificate<\/b> and choose the <b>WeatherAlertsDevPushCert.p12<\/b> file you just created.  You should now see something like this in your Parse portal.<\/p>\n<p><a href=\"http:\/\/dev.iachieved.it\/iachievedit\/wp-content\/uploads\/2014\/11\/tut_devpushcert_uploaded.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/dev.iachieved.it\/iachievedit\/wp-content\/uploads\/2014\/11\/tut_devpushcert_uploaded.png\" alt=\"tut_devpushcert_uploaded\" width=\"800\" height=\"472\" class=\"alignnone size-full wp-image-854\" \/><\/a><\/p>\n<p>Now, let&#8217;s send a test push notification.  Again, the only way this will work is if all of the previous steps have been followed correctly.  In your Parse portal, select the <b>Push<\/b> menu item for your application.<\/p>\n<p><a href=\"http:\/\/dev.iachieved.it\/iachievedit\/wp-content\/uploads\/2014\/11\/tut_send_test_push.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/dev.iachieved.it\/iachievedit\/wp-content\/uploads\/2014\/11\/tut_send_test_push.png\" alt=\"tut_send_test_push\" width=\"1151\" height=\"97\" class=\"alignnone size-full wp-image-882\" \/><\/a><\/p>\n<p>Click <b>Send a push<\/b> and keep the Parse defaults with the exception of writing <b>This is a test push notification<\/b> in the field marked for writing your message.  Scroll all the way to the bottom of the page and select <b>Send now<\/b>.<\/p>\n<p>If your application was running in the foreground when you sent the test push, Parse did you the kind favor of creating an alert dialog box with given text.  If your application was running in the background you should have received a standard iOS push notification dialog box.  An example of our push notification received while the application is in the background:<\/p>\n<p><a href=\"http:\/\/dev.iachieved.it\/iachievedit\/wp-content\/uploads\/2014\/11\/tut_push_in_background.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/dev.iachieved.it\/iachievedit\/wp-content\/uploads\/2014\/11\/tut_push_in_background.png\" alt=\"tut_push_in_background\" width=\"320\" height=\"568\" class=\"alignnone size-full wp-image-856\" \/><\/a><\/p>\n<p>An example of receiving a push notification while the application is in the foreground:<\/p>\n<p><a href=\"http:\/\/dev.iachieved.it\/iachievedit\/wp-content\/uploads\/2014\/11\/tut_push_in_foreground.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/dev.iachieved.it\/iachievedit\/wp-content\/uploads\/2014\/11\/tut_push_in_foreground.png\" alt=\"tut_push_in_foreground\" width=\"320\" height=\"568\" class=\"alignnone size-full wp-image-857\" \/><\/a><\/p>\n<h2>Bitbucket Xcode Project<\/h2>\n<p>The work done thus far is available on <a href=\"https:\/\/bitbucket.org\/joeiachievedit\/weatheralerts\/\">Bitbucket<\/a> on a branch named <b>part1<\/b>.  To use the project you can directly download the <a href=\"https:\/\/bitbucket.org\/joeiachievedit\/weatheralerts\/get\/part1.zip\">zip file<\/a> and open the enclosed Xcode project.  There are a few steps you&#8217;ll need to complete to use the project:<\/p>\n<ul>\n<li>Sign up for Parse and obtain your own API keys\n<li>Change the <b>Bundle Identifier<\/b> to reflect your organization (that is, change <code>it.iachieved.<\/code> to <code>com.yourcompany<\/code>)\n<li>Create and add your own <code>ApiKeys.plist<\/code> file and use your Parse API keys\n<li>Create and configure your own provisioning profile\n<\/ul>\n<p>To get the full experience, however, I recommend creating your project from scratch and working through the tutorial one step at a time.<\/p>\n<h2>Part Two<\/h2>\n<p>Part two of this series will introduce using the <b>CoreLocation<\/b> capabilities of iOS to receive location information.  This location information will in turn be used in Part Three to lookup whether or not there are any weather alerts in the area.  Stay tuned!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Update 1\/29\/2016: Parse announced yesterday in their blog that they will be fully shutting down operations on January 28, 2017. Consider using a service like Amazon.com SNS or Pushover. Editor&#8217;s Note: This is part one in a three part series. The goal in this series is to develop a fully functional (and useful) iOS application [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11,5,2],"tags":[],"class_list":["post-823","post","type-post","status-publish","format-standard","hentry","category-apple","category-swift","category-xcodetips"],"_links":{"self":[{"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/posts\/823"}],"collection":[{"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/comments?post=823"}],"version-history":[{"count":44,"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/posts\/823\/revisions"}],"predecessor-version":[{"id":2613,"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/posts\/823\/revisions\/2613"}],"wp:attachment":[{"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/media?parent=823"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/categories?post=823"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/tags?post=823"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}