Skip to content

Commit 9bba9ae

Browse files
author
BobbySchmidt2
committed
edit pass: notification-hubs-ios-push-notifications-swift-apps-get-started
1 parent ff138ed commit 9bba9ae

File tree

3 files changed

+50
-50
lines changed

3 files changed

+50
-50
lines changed

articles/notification-hubs/notification-hubs-ios-push-notifications-swift-apps-get-started.md

Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
2-
title: Push notifications to Swift iOS apps using Azure Notification Hubs | Microsoft Docs
3-
description: Learn how to push notifications to Swift iOS apps using Azure Notification Hubs.
2+
title: Push notifications to Swift iOS apps that use Azure Notification Hubs | Microsoft Docs
3+
description: Learn how to push notifications to Swift iOS apps that use Azure Notification Hubs.
44
services: notification-hubs
55
documentationcenter: ios
66
author: mikeparker104
@@ -17,13 +17,13 @@ ms.date: 05/21/2019
1717
ms.author: miparker
1818
---
1919

20-
# Tutorial: Push notifications to Swift iOS apps using the Notification Hubs REST API
20+
# Tutorial: Push notifications to Swift iOS apps that use the Notification Hubs REST API
2121

2222
> [!div class="op_single_selector"]
2323
> * [Objective-C](notification-hubs-ios-apple-push-notification-apns-get-started.md)
2424
> * [Swift](notification-hubs-ios-push-notifications-swift-apps-get-started.md)
2525
26-
In this tutorial, you use Azure Notification Hubs to push notifications to a Swift-based iOS application using the [REST API](/rest/api/notificationhubs/). You also create a blank iOS app that receives push notifications by using the [Apple Push Notification service (APNs)](https://developer.apple.com/library/content/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/APNSOverview.html#//apple_ref/doc/uid/TP40008194-CH8-SW1).
26+
In this tutorial, you use Azure Notification Hubs to push notifications to a Swift-based iOS application by using the [REST API](/rest/api/notificationhubs/). You also create a blank iOS app that receives push notifications by using the [Apple Push Notification service (APNs)](https://developer.apple.com/library/content/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/APNSOverview.html#//apple_ref/doc/uid/TP40008194-CH8-SW1).
2727

2828
This tutorial takes you through the following steps:
2929

@@ -33,7 +33,7 @@ This tutorial takes you through the following steps:
3333
> * Create a provisioning profile for the app.
3434
> * Create a notification hub.
3535
> * Configure the notification hub with APNs information.
36-
> * Connect your iOS app to Notification Hubs.
36+
> * Connect your iOS app to a notification hub.
3737
> * Test the solution.
3838
3939
## Prerequisites
@@ -46,20 +46,20 @@ To follow along, you need:
4646
- A Mac running Xcode along with a valid developer certificate installed into your Keychain.
4747
- A physical iPhone device you can run and debug with, as you can't test push notifications with the simulator.
4848
- Your physical iPhone device registered in the [Apple Portal](https://developer.apple.com) and associated with your certificate.
49-
- An [Azure Subscription](https://portal.azure.com) where you can create and manage resources.
49+
- An [Azure subscription](https://portal.azure.com) where you can create and manage resources.
5050

5151
Even if you have no prior experience, you should be able to follow along the steps for creating this first-principles example. However, you'll benefit from familiarity with the following concepts:
5252

5353
- Building iOS apps with Xcode and Swift.
54-
- Configuring an [Azure Notification Hub](notification-hubs-ios-apple-push-notification-apns-get-started.md) for iOS.
54+
- Configuring an [Azure Notification hub](notification-hubs-ios-apple-push-notification-apns-get-started.md) for iOS.
5555
- The [Apple Developer Portal](https://developer.apple.com) and the [Azure portal](https://portal.azure.com).
5656

5757
> [!NOTE]
5858
> The notification hub will be configured to use the **Sandbox** authentication mode only. You should not use this authentication mode for production workloads.
5959
6060
[!INCLUDE [Notification Hubs Enable Apple Push Notifications](../../includes/notification-hubs-enable-apple-push-notifications.md)]
6161

62-
## Connect your iOS app to Notification Hubs
62+
## Connect your iOS app to a notification hub
6363

6464
In this section, you'll build the iOS app that will connect to the notification hub.
6565

@@ -79,18 +79,18 @@ In this section, you'll build the iOS app that will connect to the notification
7979

8080
1. Create a new folder called **SupportingFiles**.
8181

82-
1. Create a new p-list file called **devsettings.plist** in the **SupportingFiles** folder. Be sure to add this folder to your **gitignore** file so it isn't committed when working with a git repo. In a production app, you would likely be conditionally setting these secrets as part of an automated build process. However, such settings aren't covered in this walkthrough.
82+
1. Create a new p-list file called **devsettings.plist** in the **SupportingFiles** folder. Be sure to add this folder to your **gitignore** file so it isn't committed when working with a git repo. In a production app, you would likely be conditionally setting these secrets as part of an automated build process. Such settings aren't covered in this walkthrough.
8383

84-
1. Update **devsettings.plist** to include the following configuration entries, using your own values from the notification hub that you provisioned:
84+
1. Update **devsettings.plist** to include the following configuration entries by using your own values from the notification hub that you provisioned:
8585

8686
| Key | Type | Value |
8787
|--------------------------------| -------------------------| --------------------------|
88-
| notificationHubKey | String | *hubKey* |
89-
| notificationHubKeyName | String | *hubKeyName* |
90-
| notificationHubName | String | *hubName* |
91-
| notificationHubNamespace | String | *hubNamespace* |
88+
| notificationHubKey | String | <hubKey> |
89+
| notificationHubKeyName | String | <hubKeyName> |
90+
| notificationHubName | String | <hubName> |
91+
| notificationHubNamespace | String | <hubNamespace> |
9292

93-
You can find the required values by navigating to the notification hub resource in the Azure portal. In particular, the **notificationHubName** and **notificationHubNamespace** values are in the top-right corner of the **Essentials** summary within the **Overview** page.
93+
You can find the required values by navigating to the notification hub resource in the Azure portal. In particular, the **notificationHubName** and **notificationHubNamespace** values are in the upper-right corner of the **Essentials** summary within the **Overview** page.
9494

9595
![Notification Hubs Essentials summary](./media/notification-hubs-ios-push-notifications-swift-apps-get-started/hub-essentials.png)
9696

@@ -102,7 +102,7 @@ In this section, you'll build the iOS app that will connect to the notification
102102
Endpoint=sb://<namespace>.servicebus.windows.net/;SharedAccessKeyName=<notificationHubKeyName>;SharedAccessKey=<notificationHubKey>
103103
```
104104

105-
To keep it simple, specify `DefaultFullSharedAccessSignature` so you can use the token to send notifications as well. However, in practice the `DefaultListenSharedAccessSignature` would be a better choice for situations where you only want to receive notifications.
105+
To keep it simple, specify `DefaultFullSharedAccessSignature` so you can use the token to send notifications. In practice the `DefaultListenSharedAccessSignature` would be a better choice for situations where you only want to receive notifications.
106106

107107
1. Under **Project Navigator**, select the **Project Name** and then select the **General** tab.
108108

@@ -249,7 +249,7 @@ In this step, you'll create a set of models to represent the [Notification Hubs
249249
}
250250
```
251251

252-
1. Add a new swift file called **TokenData.swift** to the **Models** folder. This model will be used to store a SAS token along with its expiration.
252+
1. Add a new Swift file called **TokenData.swift** to the **Models** folder. This model will be used to store a SAS token along with its expiration.
253253

254254
```swift
255255
import Foundation
@@ -283,11 +283,11 @@ The process itself involves the same six key steps:
283283
1. Preparing the **StringToSign**, which is formatted as `'<UrlEncodedResourceUrl>\n<ExpiryEpoch>'`.
284284
1. Computing and Base64-encoding the **Signature** by using the HMAC-SHA256 hash of the **StringToSign** value. The hash value is used with the **Key** part of the **Connection String** for the respective **Authorization Rule**.
285285
1. Formatting the Base64 encoded **Signature** so it's percent encoded.
286-
1. Constructing the token in the expected format using the **UrlEncodedSignature**, **ExpiryEpoch**, **KeyName**, and **UrlEncodedResourceUrl** values.
286+
1. Constructing the token in the expected format by using the **UrlEncodedSignature**, **ExpiryEpoch**, **KeyName**, and **UrlEncodedResourceUrl** values.
287287

288-
See the [Azure Service Bus documentation](../service-bus-messaging/service-bus-sas.md) for a more thorough overview of shared access signature and how it's used by Azure Service Bus and Notification Hubs.
288+
See the [Azure Service Bus documentation](../service-bus-messaging/service-bus-sas.md) for a more thorough overview of shared access signature and how Azure Service Bus and Notification Hubs use it.
289289

290-
For the purposes of this Swift example, you're going to use Apple's open-source **CommonCrypto** library to help with the hashing of the signature. As it's a C library, it isn't accessible in Swift out of the box. However, you can make the library available by using a bridging header.
290+
For the purposes of this Swift example, you're going to use Apple's open-source **CommonCrypto** library to help with the hashing of the signature. As it's a C library, it isn't accessible in Swift out of the box. You can make the library available by using a bridging header.
291291

292292
To add and configure the bridging header:
293293

@@ -407,10 +407,10 @@ Follow these steps to call the **installations** API:
407407
| Key | Value |
408408
| ------------- | ---------------- |
409409
| Content-Type | application/json |
410-
| Authorization | *sasToken* |
410+
| Authorization | <sasToken> |
411411
| x-ms-version | 2015-01 |
412412

413-
1. Select the **Code** button that appears on the top-right under the **Save** button. The request should look similar to the following example:
413+
1. Select the **Code** button that appears on the upper-right under the **Save** button. The request should look similar to the following example:
414414

415415
```html
416416
GET /<hubName>/installations/<installationId>?api-version=2015-01 HTTP/1.1
@@ -422,9 +422,9 @@ Follow these steps to call the **installations** API:
422422
Postman-Token: <postmanToken>
423423
```
424424

425-
1. Select the **Send** button
425+
1. Select the **Send** button.
426426

427-
No registration exists for the specified **installationId** at this point. However, the verification should result in a "404 Not Found" response rather than a "401 Unauthorized" response. This result should confirm that the SAS token has been accepted.
427+
No registration exists for the specified **installationId** at this point. The verification should result in a "404 Not Found" response rather than a "401 Unauthorized" response. This result should confirm that the SAS token has been accepted.
428428

429429
### Implement the installation service class
430430

@@ -534,7 +534,7 @@ class NotificationRegistrationService {
534534

535535
The requisite details are provided as part of initialization. Tags and templates are optionally passed into the **register** function to form part of the **Device Installation** JSON payload.
536536

537-
The **register** function calls the other private functions to prepare the request. Once a response is received, the completion is called and indicates whether the registration was successful.
537+
The **register** function calls the other private functions to prepare the request. After a response is received, the completion is called and indicates whether the registration was successful.
538538

539539
The request endpoint is constructed by the **getBaseAddress** function. The construction uses the notification hub parameters *namespace* and *name* that were provided during initialization.
540540

@@ -544,7 +544,7 @@ Finally, **encodeToJson** converts the respective model objects into JSON for us
544544

545545
### Invoke the Notification Hubs REST API
546546

547-
The last step is updating **AppDelegate** to use the **NotificationRegistrationService** to register with our **NotificationHub**.
547+
The last step is updating **AppDelegate** to use **NotificationRegistrationService** to register with our **NotificationHub**.
548548

549549
1. Open **AppDelegate.swift** and add a class-level variable to store a reference to the **NotificationRegistrationService**:
550550

@@ -585,11 +585,11 @@ The last step is updating **AppDelegate** to use the **NotificationRegistrationS
585585

586586
## Test the solution
587587

588-
Our app at this stage is registered with **NotificationHub** and can receive push notifications. In Xcode, stop the debugger and close the app if it's currently running. Next, check that the **Device Installation** details are as expected and that our app can now indeed receive push notifications.
588+
Our app at this stage is registered with **NotificationHub** and can receive push notifications. In Xcode, stop the debugger and close the app if it's currently running. Next, check that the **Device Installation** details are as expected and that our app can now receive push notifications.
589589

590590
### Verify the device installation
591591

592-
You can now make the same request as you did previously using **Postman** for [verifying the SAS token](#verify-the-sas-token). Assuming that the SAS token hasn't expired, the response should now include the installation details you provided, such as the templates and tags.
592+
You can now make the same request as you did previously by using **Postman** for [verifying the SAS token](#verify-the-sas-token). Assuming that the SAS token hasn't expired, the response should now include the installation details you provided, such as the templates and tags.
593593

594594
```json
595595
{
@@ -614,17 +614,17 @@ You can now make the same request as you did previously using **Postman** for [v
614614

615615
### Send a test notification (Azure portal)
616616

617-
The quickest way to test that you can now receive notifications is to navigate to the notification hub in the Azure portal:
617+
The quickest way to test that you can now receive notifications is to browse to the notification hub in the Azure portal:
618618

619-
1. In the Azure portal, navigate to the **Overview** tab on your notification hub.
619+
1. In the Azure portal, browse to the **Overview** tab on your notification hub.
620620

621-
1. Select **Test Send**, which is above the **Essentials** summary in the top-left of the portal window:
621+
1. Select **Test Send**, which is above the **Essentials** summary in the upper-left of the portal window:
622622

623623
![Notification Hubs Essentials Summary Test Send Button](./media/notification-hubs-ios-push-notifications-swift-apps-get-started/hub-essentials-test-send.png)
624624

625625
1. Choose **Custom Template** from the **Platforms** list.
626626

627-
1. Enter "12345" for the **Send to Tag Expression**. You had previously specified this tag in our installation.
627+
1. Enter **12345** for the **Send to Tag Expression**. You had previously specified this tag in our installation.
628628

629629
1. Optionally edit the **message** in the JSON payload:
630630

@@ -634,7 +634,7 @@ The quickest way to test that you can now receive notifications is to navigate t
634634

635635
![Notification Hubs Test Send Results](./media/notification-hubs-ios-push-notifications-swift-apps-get-started/hub-test-send-result.png)
636636

637-
Assuming that the app isn't running in the foreground, you should also see a notification in the **Notification Center** on your device. Tapping on the notification should open the app and show the alert.
637+
Assuming that the app isn't running in the foreground, you should also see a notification in the **Notification Center** on your device. Tapping the notification should open the app and show the alert.
638638

639639
![Notification Received Example](./media/notification-hubs-ios-push-notifications-swift-apps-get-started/test-send-notification-received.png)
640640

@@ -655,7 +655,7 @@ You can send notifications via the [REST API](/rest/api/notificationhubs/) by us
655655
| Key | Value |
656656
| ------------------------------ | ------------------------------ |
657657
| Content-Type | application/json;charset=utf-8 |
658-
| Authorization | *sasToken* |
658+
| Authorization | <sasToken> |
659659
| ServiceBusNotification-Format | template |
660660
| Tags | "12345" |
661661

@@ -667,7 +667,7 @@ You can send notifications via the [REST API](/rest/api/notificationhubs/) by us
667667
}
668668
```
669669

670-
1. Select the **Code** button, which is under the **Save** button on the top-right of the window. The request should look similar to the following example:
670+
1. Select the **Code** button, which is under the **Save** button on the upper-right of the window. The request should look similar to the following example:
671671

672672
```html
673673
POST /<hubName>/messages/?api-version=2015-01 HTTP/1.1
@@ -684,7 +684,7 @@ You can send notifications via the [REST API](/rest/api/notificationhubs/) by us
684684
}
685685
```
686686

687-
1. Select the **Send** button
687+
1. Select the **Send** button.
688688

689689
You should get a success status code and receive the notification on the client device.
690690

@@ -693,9 +693,9 @@ You now have a basic iOS Swift app connected to a notification hub via the [REST
693693

694694
- [Azure Notification Hubs overview](notification-hubs-push-notification-overview.md)
695695
- [Notification Hubs REST APIs](/rest/api/notificationhubs/)
696-
- [Notification Hubs SDK for backend operations](https://www.nuget.org/packages/Microsoft.Azure.NotificationHubs/)
696+
- [Notification Hubs SDK for back-end operations](https://www.nuget.org/packages/Microsoft.Azure.NotificationHubs/)
697697
- [Notification Hubs SDK on GitHub](https://github.com/Azure/azure-notificationhubs)
698-
- [Register with application backend](notification-hubs-ios-aspnet-register-user-from-backend-to-push-notification.md)
698+
- [Register with application back-end](notification-hubs-ios-aspnet-register-user-from-backend-to-push-notification.md)
699699
- [Registration management](notification-hubs-push-notification-registration-management.md)
700700
- [Working with tags](notification-hubs-tags-segment-push-message.md)
701701
- [Working with custom templates](notification-hubs-templates-cross-platform-push-messages.md)

0 commit comments

Comments
 (0)