Push Notification using Service Bus

Posted: June 28, 2013 in Live in a Cloudy World, Windows Azure
Tags: , , , ,

Like the push notification on Windows Phone 7, there is also another type of notifications that you can do using the Service Bus. There are several ways to do so, for nowadays, there are a new way that facilitate much more the development and the subscription of the devices to the Service Bus. This kind of Service Bus is called the Service Bus Notification Hub, like the Service Bus Relay Messaging and the Service Bus Brokered Messaging, there are some steps that you should do to create the required Service Bus Notification Hub. This feature is still in Preview.

Here are the steps:

You have to download special references for the development for the Push Notification hub. After creating your project on Visual Studio, go to tools -> Library Package Manager -> Package Manager Console and enter the following command:

Install-Package ServiceBus.Preview

Going to the portal:

First of all, go to you Windows Azure Portal and quick Create a notification hub as shown below

After creating the service bus, there are several few things that must be done to register in the service bus.

Here they are in brief I will explain each one of them later on.

  1. Download the required ServiceBus.Preview.dll from the NuGet, previously explained.
  2. Create a Windows Store application using Visual Studio
  3. Get the Package SID & Client Secret from after registering the application in the store and paste it in the Notification Hub Configuration
  4. Associate the application created on Visual Studio with the one created on the store.
  5. Get the Connection information from the Notification hub.
  6. Enable the Toast Notification on the Windows 8 application.
  7. Get the Microsoft.WindowsAzure.Messaging.Managed.dll from the following link.
  8. Insert the notification hub as an attribute.

I will skip the second step which is so easy creating Windows Store application.

The third step go to https://appdev.microsoft.com create an account if you don’t have. After that submit your app, reserve your application name, then click on the third tile named Services.

Under the Services go to Authenticating your service

Copy the following package SID and the Client secret:

Now for the 4th step, in your created Windows Store application on Visual Studio right click on the project created and go to store and then click on associate App with the store. It will require that you sign in with the windows live account which you create the development account with. After that you will have to associate your application with the registered one on the store.

For the 5th step, let’s get back to the Azure account, in the Notification hub, we can get the connection information from the connection information under the service bus in the notification hub. Like the following picture:

The 6th step, is in your Windows 8 application to enable the toast push notification which is a very easy and small step. Just go to your Package.appmanifest and change the toast capable to “Yes”

The rest is for the development in the Windows Store, first thing we certainly have to do is to insert the libraries. So in the App.xaml enter the following libraries:

using
Microsoft.WindowsAzure.Messaging;

using
System.Threading.Tasks;

using
Windows.Data.Xml.Dom;

using
Windows.UI.Notifications;

using
Windows.Networking.PushNotifications;

the next step is to create an instance of NotificationHub object:

NotificationHub
notificationhub;

And in the constructor of the app.xaml, initialize the instance of the object. Just don’t forget to change the DefaultListenSharedAccessSignature by its true value from the connection information retrieved from the Azure account in a previous step.

notificationhub
=
new
NotificationHub(“myhub”, “DefaultListenSharedAccessSignature”);

Initialize the notification by registering the channel by its Uri.

async
Task
initializenotification()

{


var
channel
=
await
PushNotificationChannelManager.CreatePushNotificationChannelForApplicationAsync();


await
notificationhub.RegisterAsync(new
Registration(channel.Uri));

}

After that call the initialization function on the launch of the application or OnActivated.

await
initializenotification();

The previous part was for receiving the notification for sending the notification the following code will solve the issue. Don’t forget to enter the necessary libraries.

You can enter the following code in the desired function:


var
hubClient
=
NotificationHubClient.CreateClientFromConnectionString(“connectionstring”, “myhub”);


var
toast
=
“<toast> <visual> <binding template=\”ToastText01\”> <text id=\”1\”>Hello! </text> </binding> </visual> </toast>”;


hubClient.SendWindowsNativeNotification(toast);

After that you will be able to develop the application as required, in the following link, you can find all the related development issues for Windows Store apps in this link, this one for the Android and the last one is for the iOS.

Now for further development I have done a simple Windows 8 application that sends and receive push notifications using Service Bus Notification Hub, you can download the source code from here.

The Notification hub for now supports only Microsoft Platform, Apple iOS and Android. Here is some video reference on Channel 9 that I hope it can help you during your development.

Here is one of my sources which explains everything in details about the Push Notification Using Service Bus.

About these ads

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s