Skip to content

Commit 8591939

Browse files
authored
Merge pull request #49903 from conceptdev/crdun-notificationhubs-iospush1
[notificationhub] ios fixes (xamarin & native)
2 parents 693d4c0 + 10953a8 commit 8591939

7 files changed

+1054
-1007
lines changed

articles/notification-hubs/notification-hubs-aspnet-backend-ios-apple-apns-notification.md

Lines changed: 369 additions & 355 deletions
Large diffs are not rendered by default.

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"]

0 commit comments

Comments
 (0)