Service Bus Notification Hub Tutorial (iOS apps)
Note |
|---|
| Notification Hubs are available in the Windows Azure Service Bus as a preview feature in January 2013. It is expected to transition to General Availability (GA) in midyear 2013. |
Note |
|---|
| As a result of the improvements made to Notification Hubs, the SDK you downloaded before April 2013 will not work with the new Notification Hubs. Conversely, the current SDK will not work with Notification Hubs created before April 2013. Please start using the new Notification Hubs with the new SDK to take full advantage of the new features, such as optimistic concurrency control. Support for Notification Hubs created before April 2013 will end when Notification Hubs transitions to GA. |
Service Bus Notification Hubs are Service Bus entities that enable a user to send device push notifications through third-party application platforms, including:
-
The Windows Push Notification Services (WNS) for Windows 8.
-
The Apple Push Notification service (APNs).
Support for the Microsoft Push Notification Service (MPNS) for Windows Phone, and Google Cloud Messaging (GCM), targeting the Android platform, will be added soon.
This tutorial describes how to set up an iOS application and a simple .NET application that broadcasts a notification through the Apple Push Notification service.
The steps to follow are:
-
Generate the certificate signing request to activate APNs for your app.
-
Register your iOS app in the iOS Provisioning Portal to enable push notifications and obtain the credentials and provisioning profile.
-
Create a provisioning profile for the app.
-
Create and configure a Notification Hub.
-
Set up your iOS app to receive push notifications from a Notification Hub.
-
Create a back-end program to broadcast a push notification.
An in-depth How To: Service Bus Notification Hubs (iOS Apps) topic, and a Windows Azure Service Bus Notification Hubs overview topic are also available. For more information about how to use Notification Hubs from Windows Store apps, see How To: Service Bus Notification Hubs (Windows Store Apps),
Prerequisites
To perform the tasks explained in this tutorial, you must have the following:
-
An Apple developer account.
-
Service Bus .NET Preview SDK. The SDK is a NuGet package that contains Service Bus preview features, and you can download it here. The package is ServiceBus preview features, and it contains a new Service Bus library called Microsoft.ServiceBus.Preview.dll. In order to use Notification Hubs, use this library instead of the production version (Microsoft.ServiceBus.dll).
-
Service Bus iOS SDK framework. You can download this from here.
Generate the certificate signing request
-
First, generate the Certificate Signing Request (CSR) file, which Apple uses to generate a signed certificate.
-
From the Utilities folder, run the Keychain Access tool.
-
Click Keychain Access, expand Certificate Assistant, and then click Request a Certificate from a Certificate Authority.....

-
Click your User Email Address, type the Common Name and CA Email Address values, make sure that Saved to disk is selected, and then click Continue.

-
Type a name for the Certificate Signing Request (CSR) file in Save As, select the location in Where, and then click Save. This operation saves the CSR file in the selected location; the default location is in the Desktop. Remember the location chosen for this file.

-
Next, register your app with Apple, enable push notifications, and upload this exported CSR to create a push certificate.
Register your app and enable push notifications
-
To be able to send push notifications to an iOS app, register your application with Apple and also register for push notifications.
-
If you have not already registered your app, navigate to the iOS Provisioning Portal at the Apple Developer Center, log on with your Apple ID, click App IDs, and then click New App ID.

-
In Description, type a name for your app, enter the value ServiceBus.Tutorial in Bundle Identifier, and then click Submit.
Note The bundle identifier must be the same one as the one you provided in the Xcode project for your app. -
Locate the app ID that you created, and then click Configure.
-
Check the Enable for Apple Push Notification service check box, and then click Configure for the Development Push SSL Certificate. The Apple Push Notification service SSL Certificate Assistant is displayed.
Note The same process applies for the production certificate. -
Click Browse, go to the location in which you saved the CSR file that you created in the first task, and then click Generate.
-
After the portal creates the certificate, click Continue, and on the next screen click Download. The signing certificate is downloaded and saved to your computer in your Downloads folder as aps_development.cer.
-
Double-click the downloaded push certificate aps_development.cer. The new certificate is downloaded and installed in the Keychain.
-
In Keychain Access, right-click the new certificate, click Export, name your file ServiceBusTutorial, select the .p12 format, and then click Save.
Important If the portal does not accept the .p12 extension, change the extension to .pfx.
Create a provisioning profile for the app
-
Back in the iOS Provisioning Portal, click Provisioning, then click New Profile.
-
Enter a Profile Name, click the Certificates and Devices to use for testing, select the App ID, and then click Submit. A new provisioning profile is created.
-
From the list of provisioning profiles, click Download for this new profile. The profile is downloaded to the local computer.
-
In Xcode, open the Organizer select the Devices view, click Provisioning Profiles in the Library section in the left pane, and then click the Import button at the bottom of the middle pane.
-
Locate the downloaded provisioning profile and click Open.
-
Under Targets, click your app’s main target (usually the app name), expand Code Signing Identity, and then under Debug, select the new profile. This procedure ensures that the Xcode project uses the new profile for code signing.
Create a Service Bus Notification Hub
Using the Windows Azure Management Portal, create a notification hub and configure it to send push notifications to a Windows Store application.
-
Log on to the Windows Azure Management Portal (http://manage.windowsazure.com/)
-
In the bottom left corner, click New.
-
Click App Services, and then Service Bus Notification Hub, then Quick Create.
-
Select a name for the notification hub, a region, and the namespace in which to create this notification hub (if there are no namespaces available, a new one with the specified name is provisioned).

-
Click the check mark.
-
Under the Service Bus tab in the left navigation pane, click the created namespace. The notification hub should appear in the list.

-
Click the notification hub, and then click the Configure tab at the top.

-
Upload the APNs certificate (ServiceBusTutorial.p12), with its password. Remember to select the corresponding environment for the certificate, i.e. Production or Development.
Create an iOS app and register for push notifications with Service Bus
Download and reference the Service Bus framework
-
Download the Service Bus iOS Preview SDK.
-
Drag the WindowsAzureServiceBus.framework folder from Finder into your project (select Copy items into destination group’s folder (if needed)).
Access a notification hub
-
Add the following import directive in the
AppDelegateclass of your app.
#import <WindowsAzureMessaging/WindowsAzureMessaging.h>
-
Write the following code in the
didFinishLaunchingWithOptionsmethod:
[[UIApplication sharedApplication] registerForRemoteNotificationTypes: UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound];
-
In the Windows Azure management portal, click Service Bus in the left navigation pane, and then click your namespace name on the right. Note the name of your namespace.
-
Click your notification hub, and then click View SAS Key on the bottom toolbar.

-
Copy the value of the key named DefaultListenSharedAccessSignature.
-
In AppDelegate.m, add the following method that creates a template registration for this device:
(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *) deviceToken { NSString* connectionString = [SBConnectionString stringWithEndpoint:[[NSURL alloc] initWithString:@"sb://todons.servicebus.windows.net"] listenAccessSecret:@"+@_#i1Q]rcia$zzt"]; SBNotificationHub* hub = [[SBNotificationHub alloc] initWithConnectionString: connectionString notificationHubPath:@"mynh"]; [hub refreshRegistrationsWithDeviceToken: deviceToken completion: ^(NSError* error) { if (error == nil) { [hub createNativeRegistrationWithTags:nil completion:^(NSError* error2) { if (error2 != nil) { NSLog(@"Error creating template registration: %@", error2); } }]; } else {NSLog(@"Error refreshing device token: %@", error);} }]; } -
In AppDelegate.m, add the following method to display a UIAlert if a notification is received when the app is running:
- (void)application:(UIApplication *)application didReceiveRemoteNotification: (NSDictionary *)userInfo { NSLog(@"%@", userInfo); UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Notification" message: [userInfo objectForKey:@"inAppMessage"] delegate:nil cancelButtonTitle: @"OK" otherButtonTitles:nil, nil]; [alert show]; }
In the section How To: Service Bus Notification Hubs (iOS Apps), you will find more information about the preceding code, and how to access the more advanced features of Notification Hubs from iOS apps.
Create a back-end app to send push notifications
-
In Visual Studio, create a new project, select Visual C#->Windows->Console Application.
-
In Solution Explorer, right-click References, and click Manage NuGet packages. Search for the package service bus preview features, and install it.
-
In Program.cs, add the following code at the top:
using Microsoft.ServiceBus; using Microsoft.ServiceBus.Messaging; using Microsoft.ServiceBus.Notifications;
-
In the Windows Azure Management Portal, from the notification hub dashboard, click View SAS Key on the bottom toolbar.

-
Copy the value of the key named DefaultFullSharedAccessSignature.
-
In Program.cs, paste the following code in the
Main()method:
string connectionString = ServiceBusConnectionStringBuilder.CreateUsingSharedAccessKey(new Uri("sb://<your namespace name>.servicebus.windows.net "), NotificationHubDescription.DefaultFullSasRuleName, "<key value copied from the portal>"); var hubClient = NotificationHubClient.CreateClientFromConnectionString(connectionString, "myHub"); var iosPayload = AppleNotificationJsonBuilder.Create("Hello!"); iosPayload.CustomFields.Add("inAppMessage", "Hello!"); hubClient.SendAppleNativeNotification(iosPayload.ToJsonString());
Run the app and send a notification
-
In Xcode, in the top-left corner, click iOS Device.
Important |
|---|
| Push notifications do not work in the simulator. |
-
Run the app on your device.
-
In Visual Studio, run the console program.
-
A notification should appear on the device.
Next steps
Notification Hubs provide more useful features. Refer to the following topics to learn more about Notification Hubs.