Service Bus Notification Hubs - Code Walkthrough - Windows 8 Edition
[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:
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:
All of you who didn't have a heart attack at 41:21, please raise your hand. (If you're still alive.. )