Subscribe!

Service Bus Notification Hubs - Code Walkthrough - iOS Edition

Download this episode

Download Video

Description

[This is the iOS version of the previously posted Walkthrough. If you've already watched the Windows 8 Edition, skip to 10 minutes 25 seconds where the actual code walkthrough starts]

In this clip I'll walk you through the basic principles of the brand-new Windows Azure Service Bus Notification Hubs in a somewhat more formal and serious fashion than in my chat with Elio.

Recapping from the other post,Service Bus Notification Hubs are an intrinsic feature of Windows Azure Service Bus and are different from other push notification services in four key areas:

  • Complete client registration management. Your backend application (if you even have one) does not need to worry at all about device-ids or channels or other particulars of push notifications and doesn't need to cooperate in management. It doesn't even have to be a web app that's publicly accessible.  
  • Platform independence. Service Bus Notification Hubs allow cross-platform push notifications so that iOS Alerts and Windows Live Tiles can be targeted with a single event message. 
  • Broadcast and tag-based Multicast - Service Bus Notification Hubs are optimized around automatic notification broadcast to many thousand devices with low latency. One message in, thousands of notifications out.
  • Mass customization - Notification Hub notification templates allow for customization of notification delivery for each individual registration, allowing each instance of a client App to choose how it wants to receive events.

In this preview, Notification Hubs are able to push notifications to Windows Store apps and iOS apps from .NET back-ends. Support for Android and Windows Phone, along with additional back-end technologies (including Windows Azure Mobile Services) will be added soon.

After the basic intro, I'm showing how to create and provision a iOS application from scratch, how to hook it up to a new Notification Hub, and send it a notification Alert using the portals and Visual Studio Express 2012 Web. (The equivalent Windows 8 walkthrough is here)

For those of you with a "TL;DW" attention span (too long; didn't watch), here's the whole extent of the code added to the stock iOS code template to enable Service Bus Notifications and that includes re-registering existing registrations at App startup. Decoration excluded it's 6 functional lines including one required by iOS - admittedly, without error handling.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    NSString * connectionString =
    [SBConnectionString stringWithEndpoint:@"https://myhub-euw-ns.servicebus.windows.net/" listenAccessSecret:@"... secret..."];
    
    self.hub = [[SBNotificationHub alloc] initWithConnectionString: connectionString notificationHubPath:@"myhub"];
    
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:
     UIRemoteNotificationTypeAlert|UIRemoteNotificationTypeBadge|UIRemoteNotificationTypeSound];
     ...
    return YES;
}


- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    
    [self.hub refreshRegistrationsWithDeviceToken:deviceToken completion:
       ^(NSError *error) {
         if ( error == nil )
         {
             [self.hub defaultRegistrationExistsWithCompletion:^(BOOL exists, NSError *error2) {
                 if ( error2 == nil && !exists )
                 {
                     [self.hub createDefaultRegistrationWithTags:nil completion:
                      
                      ^(NSError *error3) {
                          if ( error3 != nil) {
                          }
                      }];
                 }
             }];
         }
     }];
}

The event-source code, written on the server-side for a Windows Azure Web Site in ASP.NET, is similarly terse:

var cs = ServiceBusConnectionStringBuilder.CreateUsingSharedAccessSecretWithFullAccess( "myhub-euw-ns", "...secret...");
var nh = NotificationHubClient.CreateClientFromConnectionString( cs, "myhub");

nh.SendAppleNativeNotification( AppleNotificationJsonBuilder.Create(TextBox1.Text).ToJsonString());

 3 lines. Three lines. No management of device ids. No public endpoint for the phone to talk to. Service Bus does all that. It really is worth playing with. 

And here are all the key links ....

 SDKs:

Embed

Format

Available formats for this video:

Actual format may change based on video formats available and browser capability.

    The Discussion

    Comments closed

    Comments have been closed since this content was published more than 30 days ago, but if you'd like to continue the conversation, please create a new thread in our Forums, or Contact Us and let us know.