Skip to content

Commit 1291a33

Browse files
committed
more codeblock fixes
1 parent 8f24580 commit 1291a33

File tree

2 files changed

+121
-114
lines changed

2 files changed

+121
-114
lines changed

articles/notification-hubs/notification-hubs-ios-apple-push-notification-apns-get-started.md

Lines changed: 60 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ ms.date: 04/14/2018
1919
ms.author: dimazaid
2020
---
2121
# Tutorial: Push notifications to iOS apps using Azure Notification Hubs
22+
2223
[!INCLUDE [notification-hubs-selector-get-started](../../includes/notification-hubs-selector-get-started.md)]
2324

2425
In this tutorial, you use Azure Notification Hubs to push notifications to an iOS application. You 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).
@@ -70,116 +71,120 @@ In this section, you create a notification hub and configure authentication with
7071
You have now configured your notification hub with APNS, and you have the connection strings to register your app and send push notifications.
7172

7273
## Connect your iOS app to Notification Hubs
74+
7375
1. In Xcode, create a new iOS project and select the **Single View Application** template.
74-
76+
7577
![Xcode - Single View Application][8]
76-
78+
7779
2. When setting the options for your new project, make sure to use the same **Product Name** and **Organization Identifier** that you used when you set the bundle identifier in the Apple Developer portal.
78-
80+
7981
![Xcode - project options][11]
80-
82+
8183
3. Under Project Navigator, click your project name, click the **General** tab, and find **Signing**. Make sure you select the appropriate Team for your Apple Developer account. XCode should automatically pull down the Provisioning Profile you created previously based on your bundle identifier.
82-
84+
8385
If you don't see the new provisioning profile that you created in Xcode, try refreshing the profiles for your signing identity. Click **Xcode** on the menu bar, click **Preferences**, click the **Account** tab, click the **View Details** button, click your signing identity, and then click the refresh button in the bottom-right corner.
8486

8587
![Xcode - provisioning profile][9]
8688

8789
4. Select the **Capabilities** tab and make sure to enable Push Notifications
8890

8991
![Xcode - push capabilities][12]
90-
92+
9193
5. Download the [Windows Azure Messaging Framework] and unzip the file. In Xcode, right-click your project and click the **Add Files to** option to add the **WindowsAzureMessaging.framework** folder to your Xcode project. Select **Options** and make sure **Copy items if needed** is selected, and then click **Add**.
9294

9395
![Unzip Azure SDK][10]
9496

9597
6. Add a new header file to your project named **HubInfo.h**. This file holds the constants for your notification hub. Add the following definitions and replace the string literal placeholders with your *hub name* and the *DefaultListenSharedAccessSignature* noted earlier.
9698

97-
```obj-c
98-
#ifndef HubInfo_h
99-
#define HubInfo_h
100-
101-
#define HUBNAME @"<Enter the name of your hub>"
102-
#define HUBLISTENACCESS @"<Enter your DefaultListenSharedAccess connection string"
103-
104-
#endif /* HubInfo_h */
99+
```objc
100+
#ifndef HubInfo_h
101+
#define HubInfo_h
102+
103+
#define HUBNAME @"<Enter the name of your hub>"
104+
#define HUBLISTENACCESS @"<Enter your DefaultListenSharedAccess connection string"
105+
106+
#endif /* HubInfo_h */
105107
```
106-
108+
107109
7. Open your **AppDelegate.h** file add the following import directives:
108110

109-
```obj-c
110-
#import <WindowsAzureMessaging/WindowsAzureMessaging.h>
111-
#import <UserNotifications/UserNotifications.h>
112-
#import "HubInfo.h"
111+
```objc
112+
#import <WindowsAzureMessaging/WindowsAzureMessaging.h>
113+
#import <UserNotifications/UserNotifications.h>
114+
#import "HubInfo.h"
113115
```
114116
8. In your **AppDelegate.m file**, add the following code in the **didFinishLaunchingWithOptions** method based on your version of iOS. This code registers your device handle with APNs:
115117

116-
```obj-c
117-
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeSound |
118-
UIUserNotificationTypeAlert | UIUserNotificationTypeBadge categories:nil];
119-
120-
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
121-
[[UIApplication sharedApplication] registerForRemoteNotifications];
118+
```objc
119+
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeSound |
120+
UIUserNotificationTypeAlert | UIUserNotificationTypeBadge categories:nil];
121+
122+
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
123+
[[UIApplication sharedApplication] registerForRemoteNotifications];
122124
```
123-
125+
124126
9. In the same file, add the following methods:
125127

126-
```obj-c
127-
- (void) application:(UIApplication *) application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *) deviceToken {
128-
SBNotificationHub* hub = [[SBNotificationHub alloc] initWithConnectionString:HUBLISTENACCESS
129-
notificationHubPath:HUBNAME];
130-
131-
[hub registerNativeWithDeviceToken:deviceToken tags:nil completion:^(NSError* error) {
132-
if (error != nil) {
133-
NSLog(@"Error registering for notifications: %@", error);
134-
}
135-
else {
136-
[self MessageBox:@"Registration Status" message:@"Registered"];
137-
}
138-
}];
139-
}
140-
141-
-(void)MessageBox:(NSString *) title message:(NSString *)messageText
142-
{
143-
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:messageText delegate:self
144-
cancelButtonTitle:@"OK" otherButtonTitles: nil];
145-
[alert show];
128+
```objc
129+
- (void) application:(UIApplication *) application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *) deviceToken {
130+
SBNotificationHub* hub = [[SBNotificationHub alloc] initWithConnectionString:HUBLISTENACCESS
131+
notificationHubPath:HUBNAME];
132+
133+
[hub registerNativeWithDeviceToken:deviceToken tags:nil completion:^(NSError* error) {
134+
if (error != nil) {
135+
NSLog(@"Error registering for notifications: %@", error);
136+
}
137+
else {
138+
[self MessageBox:@"Registration Status" message:@"Registered"];
139+
}
140+
}];
146141
}
142+
143+
-(void)MessageBox:(NSString *) title message:(NSString *)messageText
144+
{
145+
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:messageText delegate:self
146+
cancelButtonTitle:@"OK" otherButtonTitles: nil];
147+
[alert show];
148+
}
147149
```
148150

149151
This code connects to the notification hub using the connection information you specified in HubInfo.h. It then gives the device token to the notification hub so that the notification hub can send notifications.
150152

151153
10. In the same file, add the following method to display a **UIAlert** if the notification is received while the app is active:
152154

153-
```obj-c
154-
- (void)application:(UIApplication *)application didReceiveRemoteNotification: (NSDictionary *)userInfo {
155-
NSLog(@"%@", userInfo);
156-
[self MessageBox:@"Notification" message:[[userInfo objectForKey:@"aps"] valueForKey:@"alert"]];
157-
}
155+
```objc
156+
- (void)application:(UIApplication *)application didReceiveRemoteNotification: (NSDictionary *)userInfo {
157+
NSLog(@"%@", userInfo);
158+
[self MessageBox:@"Notification" message:[[userInfo objectForKey:@"aps"] valueForKey:@"alert"]];
159+
}
158160
```
159161

160162
11. To verify there are no failures, build and run the app on your device.
161163

162164
## Send test push notifications
165+
163166
You can test receiving notifications in your app with the *Test Send* option in the [Azure portal]. It sends a test push notification to your device.
164167

165168
![Azure portal - Test Send][30]
166169

167170
[!INCLUDE [notification-hubs-sending-notifications-from-the-portal](../../includes/notification-hubs-sending-notifications-from-the-portal.md)]
168171

169-
170172
## Verify that your app receives push notifications
173+
171174
To test push notifications on iOS, you must deploy the app to a physical iOS device. You cannot send Apple push notifications by using the iOS Simulator.
172175

173176
1. Run the app and verify that registration succeeds, and then press **OK**.
174-
177+
175178
![iOS App Push Notification Registration Test][33]
176-
2. Next you send a test push notification from the [Azure portal], as described in the previous section.
179+
180+
2. Next you send a test push notification from the [Azure portal], as described in the previous section.
177181

178182
3. The push notification is sent to all devices that are registered to receive the notifications from the particular Notification Hub.
179-
183+
180184
![iOS App Push Notification Receive Test][35]
181185

182186
## Next steps
187+
183188
In this simple example, you broadcasted push notifications to all your registered iOS devices. To learn how to push notifications to specific iOS devices, advance to the following tutorial:
184189

185190
> [!div class="nextstepaction"]

articles/notification-hubs/notification-hubs-ios-xplat-localized-apns-push-notification.md

Lines changed: 61 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,13 @@ In [Use Notification Hubs to send breaking news], you built an app that used **t
4646
4747
At a high level, templates are a way to specify how a specific device should receive a notification. The template specifies the exact payload format by referring to properties that are part of the message sent by your app back-end. In your case, you send a locale-agnostic message containing all supported languages:
4848

49-
{
50-
"News_English": "...",
51-
"News_French": "...",
52-
"News_Mandarin": "..."
53-
}
49+
```json
50+
{
51+
"News_English": "...",
52+
"News_French": "...",
53+
"News_Mandarin": "..."
54+
}
55+
```
5456

5557
Then you ensure that devices register with a template that refers to the correct property. For instance, an iOS app that wants to register for French news registers using the following syntax:
5658

@@ -67,7 +69,7 @@ For more information on templates, see [Templates](notification-hubs-templates-c
6769
## Prerequisites
6870

6971
- Complete the [Push notifications to specific iOS devices](notification-hubs-ios-xplat-segmented-apns-push-notification.md) tutorial and have the code available, because this tutorial builds directly upon that code.
70-
- Visual Studio 2012 or later is optional.
72+
- Visual Studio 2017 is optional.
7173

7274
## Update the app user interface
7375

@@ -149,7 +151,7 @@ Then make sure to add an IBOutlet in your ViewController.h as shown in the follo
149151
2. Now that you modified the Notifications class, you have to make sure that the ViewController makes use of the new UISegmentControl. Add the following line in the *viewDidLoad* method to make sure to show the locale that is currently selected:
150152

151153
```objc
152-
self.Locale.selectedSegmentIndex = [notifications retrieveLocale];
154+
self.Locale.selectedSegmentIndex = [notifications retrieveLocale];
153155
```
154156

155157
Then, in your *subscribe* method, change your call to the *storeCategoriesAndSubscribe* to the following code:
@@ -187,70 +189,70 @@ Then make sure to add an IBOutlet in your ViewController.h as shown in the follo
187189

188190
If you don't have access to Visual Studio, or want to just test sending the localized template notifications directly from the app on the device. You can add the localized template parameters to the `SendNotificationRESTAPI` method you defined in the previous tutorial.
189191

190-
```objc
191-
- (void)SendNotificationRESTAPI:(NSString*)categoryTag
192-
{
193-
NSURLSession* session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration
194-
defaultSessionConfiguration] delegate:nil delegateQueue:nil];
192+
```objc
193+
- (void)SendNotificationRESTAPI:(NSString*)categoryTag
194+
{
195+
NSURLSession* session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration
196+
defaultSessionConfiguration] delegate:nil delegateQueue:nil];
195197

196-
NSString *json;
198+
NSString *json;
197199

198-
// Construct the messages REST endpoint
199-
NSURL* url = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@/messages/%@", HubEndpoint,
200-
HUBNAME, API_VERSION]];
200+
// Construct the messages REST endpoint
201+
NSURL* url = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@/messages/%@", HubEndpoint,
202+
HUBNAME, API_VERSION]];
201203

202-
// Generated the token to be used in the authorization header.
203-
NSString* authorizationToken = [self generateSasToken:[url absoluteString]];
204+
// Generated the token to be used in the authorization header.
205+
NSString* authorizationToken = [self generateSasToken:[url absoluteString]];
204206

205-
//Create the request to add the template notification message to the hub
206-
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
207-
[request setHTTPMethod:@"POST"];
207+
//Create the request to add the template notification message to the hub
208+
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
209+
[request setHTTPMethod:@"POST"];
208210

209-
// Add the category as a tag
210-
[request setValue:categoryTag forHTTPHeaderField:@"ServiceBusNotification-Tags"];
211+
// Add the category as a tag
212+
[request setValue:categoryTag forHTTPHeaderField:@"ServiceBusNotification-Tags"];
211213

212-
// Template notification
213-
json = [NSString stringWithFormat:@"{\"messageParam\":\"Breaking %@ News : %@\","
214-
\"News_English\":\"Breaking %@ News in English : %@\","
215-
\"News_French\":\"Breaking %@ News in French : %@\","
216-
\"News_Mandarin\":\"Breaking %@ News in Mandarin : %@\","
217-
categoryTag, self.notificationMessage.text,
218-
categoryTag, self.notificationMessage.text, // insert English localized news here
219-
categoryTag, self.notificationMessage.text, // insert French localized news here
220-
categoryTag, self.notificationMessage.text]; // insert Mandarin localized news here
214+
// Template notification
215+
json = [NSString stringWithFormat:@"{\"messageParam\":\"Breaking %@ News : %@\","
216+
\"News_English\":\"Breaking %@ News in English : %@\","
217+
\"News_French\":\"Breaking %@ News in French : %@\","
218+
\"News_Mandarin\":\"Breaking %@ News in Mandarin : %@\","
219+
categoryTag, self.notificationMessage.text,
220+
categoryTag, self.notificationMessage.text, // insert English localized news here
221+
categoryTag, self.notificationMessage.text, // insert French localized news here
222+
categoryTag, self.notificationMessage.text]; // insert Mandarin localized news here
221223

222-
// Signify template notification format
223-
[request setValue:@"template" forHTTPHeaderField:@"ServiceBusNotification-Format"];
224+
// Signify template notification format
225+
[request setValue:@"template" forHTTPHeaderField:@"ServiceBusNotification-Format"];
224226

225-
// JSON Content-Type
226-
[request setValue:@"application/json;charset=utf-8" forHTTPHeaderField:@"Content-Type"];
227+
// JSON Content-Type
228+
[request setValue:@"application/json;charset=utf-8" forHTTPHeaderField:@"Content-Type"];
227229

228-
//Authenticate the notification message POST request with the SaS token
229-
[request setValue:authorizationToken forHTTPHeaderField:@"Authorization"];
230+
//Authenticate the notification message POST request with the SaS token
231+
[request setValue:authorizationToken forHTTPHeaderField:@"Authorization"];
230232

231-
//Add the notification message body
232-
[request setHTTPBody:[json dataUsingEncoding:NSUTF8StringEncoding]];
233+
//Add the notification message body
234+
[request setHTTPBody:[json dataUsingEncoding:NSUTF8StringEncoding]];
233235

234-
// Send the REST request
235-
NSURLSessionDataTask* dataTask = [session dataTaskWithRequest:request
236-
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error)
236+
// Send the REST request
237+
NSURLSessionDataTask* dataTask = [session dataTaskWithRequest:request
238+
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error)
239+
{
240+
NSHTTPURLResponse* httpResponse = (NSHTTPURLResponse*) response;
241+
if (error || httpResponse.statusCode != 200)
237242
{
238-
NSHTTPURLResponse* httpResponse = (NSHTTPURLResponse*) response;
239-
if (error || httpResponse.statusCode != 200)
240-
{
241-
NSLog(@"\nError status: %d\nError: %@", httpResponse.statusCode, error);
242-
}
243-
if (data != NULL)
244-
{
245-
//xmlParser = [[NSXMLParser alloc] initWithData:data];
246-
//[xmlParser setDelegate:self];
247-
//[xmlParser parse];
248-
}
249-
}];
250-
251-
[dataTask resume];
252-
}
253-
```
243+
NSLog(@"\nError status: %d\nError: %@", httpResponse.statusCode, error);
244+
}
245+
if (data != NULL)
246+
{
247+
//xmlParser = [[NSXMLParser alloc] initWithData:data];
248+
//[xmlParser setDelegate:self];
249+
//[xmlParser parse];
250+
}
251+
}];
252+
253+
[dataTask resume];
254+
}
255+
```
254256

255257
## Next steps
256258

0 commit comments

Comments
 (0)