Sergii Baidachnyi

Blog about technologies

Azure Mobile Services: Did we forget something?

leave a comment »

This is my last post about Azure Mobile Services. Last time we finished implementation of Windows client and I used all needed features for my application. You can read all posts about the topic at the following links:

Azure Mobile Services: Notification Hub vs “native development”

Azure Mobile Services: Creating a simple Notification Hub

Azure Mobile Services: Sending notification from server-side

Azure Mobile Services: Creating a Universal Application

Azure Mobile Services: Creating of Universal Application (part 2)

Azure Mobile Services: Visual Studio

I described a very simple application but what about other features of Azure Mobile Services. Frankly speaking, I used just half of all features there. Therefore, I decided to describe all other features in order to have a completed view of the topic.


This feature is related to Push Notification Hub but Azure Mobile Services encapsulate it as well. The idea is very simple – use a special tags in order to target right groups of users. For example, if you create a news site you can create a tag for each group of news and user might select just the groups that are interesting to him. I don’t like politics so much but somebody else might not like sports news etc.

Of course in order to implement this approach you need have a way to select tags during user registration process and you should have a method to broadcast messages only for selected tags. There is a good news – Mobile Service API supports tags. For example, in order to make registration using some tags you can use the following code:

await mobileService.GetPush().RegisterNativeAsync(channel.Uri,

new List<string>() {"sport", "movies", "Canada" });

In this example, three tags will be associated with user registration.

In order to send message to all users with tags movies, you can use the following code (C#):

await Services.Push.SendAsync(message, "movies");

Pay special attention that methods, which we use to send messages to users, usually allow to pass tag expression. Tag expression could be used for passing expressions based on tags, using logical operators like &&, || and !: "(tagA && !tagB)"


Templates are one more way to create personalized messages for users. For example, in order to send message from C# to Windows users, you may use the following code:

WindowsPushMessage message = new WindowsPushMessage();

message.XmlPayload = @"<?xml version=""1.0"" encoding=""utf-8""?>" +

@"<toast><visual><binding template=""ToastText02"">" +

@"<text id=""1""></text>" +

@"<text id=""2"">" + item.Text + @"</text>" +


And if you form a message in this way, all your users will see the same message. However, in some cases you might want to pass parameters to your message based on user preferences. For example, if your application supports several languages you might want to send localized messages; if your application provides some data like mileage or temperature, your might want to use measurement system, which is understandable for users, etc.

Templates allow us to use parameters in XML or JSon messages (based on platform), which we usually form on server side. In case of templates, your server side should not form message from scratch. Instead, client application should register a message template including parameters and the name of the template. Server side will send just parameter to selected templates.

For example, if you create Windows application, your client can use the following code in order to register a template:

string xmlTemplate = @"<?xml version=""1.0"" encoding=""utf-8""?>" +

@"<toast><visual><binding template=""ToastText02"">" +

@"<text id=""1""></text>" +

@"<text id=""2"">$(ru_message)</text>" +


await mobileService.GetPush().RegisterTemplateAsync(

channel.Uri, xmlTemplate, "langTemplate");

In this case, client application is registering a template with langTemplate name and with parameter ru_message. The second device might use en_message parameter instead of ru_message etc. It’s needed in order to get message in native language for device’s user. So, all devices will have the same template but with different parameters there.

In the next step, server side should form a message, which will contain the template name and all parameters. It allows Notification Hub to create unique messages for all devices based on registered templates and parameters. In order to prepare server side message you can use already prepared classes. For example, if you use C#, you can use the following code:

TemplatePushMessage mess = new TemplatePushMessage();

mess.Add("ru_message", "Сообщение");

mess.Add("en_message", "Message");

await Services.Push.SendAsync(mess);

Notification Hub will look at every registered template and send a message if template contains one of these parameters.


Some applications, like Facebook for Windows Phone, send many notifications during predefined time period. For example, usually at 11 pm. I get messages about upcoming birthdays of my friends. Of course, it’s a good idea to have something on Azure side, which might send notifications on schedule basis and Azure Mobile Services support this feature.

Using Azure Mobile Services, you can schedule any code there. You may set up a single timeframe or create a complex schedule there and you can use Azure Management panel, if you are going to create JavaScript code or Visual Studio – if you are going to create C# code.

In case of JavaScript it’s easy to create something using Sheduler tab. Pay attention that there is a way to create a job, which will run on demand:

JavaScript code may have access to all objects, which we used before, including push object.

In case of C# you need to work with Visual Studio like we did at the previous post and you should create a class, which derives from SheduledJob class. ExecuteAsync method there will allow you to create any code inside.

Custom API

One more interesting thing in Azure Mobile Services is the ability to create a custom API. You can create a set of JavaScript server-side functions using Management panel:

In case of Visual Studio, you need to create a class, which derives from ApiController.

Because you code will be called using standard HTTP protocol, you may use one of the following methods there like GET, POST, PUT, PATCH, DELETE.

Rest API

In the first article about Azure Mobile Services I told that the service will work for any type of platforms and languages because there is REST API support. Of course, I like Windows 8 and Windows Phone applications, so I used ready to use libraries, which encapsulate all work with REST and JSON but you still able to use REST API. Frankly speaking, Microsoft provides libraries for many platforms and languages (like Java, JavaScript, Objective C etc.) but in some cases it’s better to use REST directly. You can find REST API documentation here.


Written by Sergiy Baydachnyy

01/08/2015 at 9:20 PM

Posted in Microsoft Azure

Tagged with

Leave a Reply

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

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

Facebook photo

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

Connecting to %s

%d bloggers like this: