Contents: Part 1. Introduction. Part 2. Storing data in Mobile Services Part 3. Authentication. Part 4. Push-notifications.





To send a push-notification to your application using the Windows Azure Mobile Services, you must use the Windows Push service Notification (WNS). The result of this part of the manual is that the mobile service will send a push-notification whenever a new record is inserted. 

Join the Windows Store

To send a push-notification in the Windows Store application from a mobile service, you must register your app in the store, and then adjust accordingly mobile service to integrate with WNS.

Go to the registration page application -  Submit an app , sign in with your account and click on Microsoft  App name (Figure 1).

clip_image002

Fig. 1. Adding an application to the Windows Store

Enter the name of your application in the  App  name , click  Reserve  app  name  and  Save (Figure 2).

clip_image004

Fig. 2. Adding an application to the Windows Store

Open soda earlier draft, and right-clicking on the project name, click  Store => Associate  App  with  the  Store ... . (Figure 3).

clip_image006

Fig. 3. Interface Visual Studio 2012

Click  Sign in  and sign in with your account Microsoft. Select the previously documented application and click  Next => Associate  (Figure 4).

clip_image008

Fig. 4. Adding an application to the Windows Store

Go to the  My Applications  Page and select your application from the list (Figure 5).

clip_image010

Fig. 5. Application Settings

In the  API Settings  , note the values  ​​Client secret  and  Package Security identifier (SID)  (Figure 6).

clip_image012

Fig. 6. Application Settings

Go to the Management Portal. Select the tab  Mobile  Services  your application (Figure 7).

clip_image014

Fig. 7. Windows Azure Management Portal

On the  Push  enter the previously recorded values  ​​Client  Secret  and  Package SID  and click  Save (Figure 8).

clip_image016

Fig. 8. Settings push-notifications

The integration of mobile services and applications with WNS was successful.

Adding push-notification application

Add the file App.xaml.cs:

using Windows.Networking.PushNotifications;

Add the file App.xaml.cs:

public static PushNotificationChannel CurrentChannel {get; private set; }

private async void AcquirePushChannel ()

{

CurrentChannel =

await PushNotificationChannelManager.CreatePushNotificationChannelForApplicationAsync ();

}

This code is obtained and stored channel push-notifications.

Insert at the beginning of the handler  OnLaunched  call a new method that will ensure that every time an application is initialized property  CurrentChannel  :

AcquirePushChannel ();

Add in class MainPage.xaml.cs  TodoItem  new property:

[DataMember (Name = "channel")]

public string Channel {get; set; }

Replace method  ButtonSave_Click :

private void ButtonSave_Click (object sender, RoutedEventArgs e)

{

var todoItem = new TodoItem {Text = TextInput.Text, Channel = App.CurrentChannel.Uri};

InsertTodoItem (todoItem);

}

If you use your own project, and not generated by the Management Portal, click Package.appxmanifest file and make sure the tab  Application UI  value  Toast  Capable  defined as  Yes (Figure 9).

clip_image018

Fig. 9. Inclusions notifications

Thus, the application can use the pop-up notifications.

Update Script Management Portal

The portal uravleniya tab  Data  and select the table  TodoItem  (Figure 10).

clip_image020

Fig. 10. The Windows Azure Management Portal

On the  Script  click  Insert (Figure 11).

clip_image022

Fig. 11. The Windows Azure Management Portal

Replace the function of inserting the following code and click  Save :

function insert (item, user, request) {

request.execute ({

success: function () {

/ / Write back and send

/ / Notification in background

request.respond ();

push.wns.sendToastText04 (item.channel, {

text1: item.text

} {

success: function (pushResponse) {

console.log ("Sent push:", pushResponse);

}

});

}

});

}

Thus recorded a new script that handles the insert operation and sends push-notifications (inserted text) into the channel, provided requests to insert.

Launch an application

Run the application, enter text in the  Insert  TodoItem  and click  Save (Figure 12).

clip_image024

Fig. 12. Application Interface

Note that after inserting the essence application receives push-notification from WNS (Figure 13).

clip_image026

Fig. 13. Application Interface

I will do now so that you can send notifications to users push-Store App Windows.

Create a new table

Management Portal tab  Mobile  Services  and select your application (see Figure 14).

clip_image027

Fig. 14. The Windows Azure Management Portal

On the  Data  click  Create  (Figure 15).

clip_image029

Fig. 15. The Windows Azure Management Portal

In the dialog box, type in the  Table  name  meaning  Channel leave the other values ​​of the standard (Figure 16). Press the button.

clip_image031

Fig. 16. Creating a new table

The new table will be stored URI channels that will be used when sending push-notification separate from the entity data.

Updating the application

Remove from the property MainPage.xaml.cs  Channel  classroom  TodoItem  . The class should look like this:

public class TodoItem

{

public int Id {get; set; }

[DataMember (Name = "text")]

public string Text {get; set; }

[DataMember (Name = "complete")]

public bool Complete {get; set; }

}

Replace method  ButtonSave_Click :

private void ButtonSave_Click (object sender, RoutedEventArgs e)

{

var todoItem = new TodoItem {Text = TextInput.Text};

InsertTodoItem (todoItem);

}

Add code that creates a new class of  Channel :

public class Channel

{

public int Id {get; set; }

[DataMember (Name = "uri")]

public string Uri {get; set; }

}

Replace file App.xaml.cs method  AcquirePushChannel :

private async void AcquirePushChannel ()

{

CurrentChannel =

await PushNotificationChannelManager.CreatePushNotificationChannelForApplicationAsync ();

IMobileServiceTable <Channel> channelTable = App.MobileService.GetTable <Channel> ();

var channel = new Channel {Uri = CurrentChannel.Uri};

await channelTable.InsertAsync (channel);

}

In this code you are inserting the current channel table Channel.

Update server scripts

Management Portal tab  Data  table, select  Channel  (Figure 18).

clip_image033

Fig. 18. The Windows Azure Management Portal

On the  Script  click  Insert (Figure 19).

clip_image035

Fig. 19. The Windows Azure Management Portal

Replace the insert function (Insert) and click  Save :

function insert (item, user, request) {

var channelTable = tables.getTable ('Channel');

channelTable

. Where ({uri: item.uri})

. Read ({success: insertChannelIfNotFound});

function insertChannelIfNotFound (existingChannels) {

if (existingChannels.length> 0) {

request.respond (200, existingChannels [0]);

} Else {

request.execute ();

}

}

}

The script validates the table  Channel  for the presence of channels with the same URI and, if there is none, the channel insertion occurs.

Click  Script  and select  Insert (Figure 20).

clip_image036

Fig. 20. The Windows Azure Management Portal

Replace the insert function and click  Save :

function insert (item, user, request) {

request.execute ({

success: function () {

request.respond ();

sendNotifications ();

}

});

function sendNotifications () {

var channelTable = tables.getTable ('Channel');

channelTable.read ({

success: function (channels) {

channels.forEach (function (channel) {

push.wns.sendToastText04 (channel.uri, {

text1: item.text

} {

success: function (pushResponse) {

console.log ("Sent push:", pushResponse);

}

});

});

}

});

}

}

Insert script will send a push-notification with the text inserted in the essence of all the channels stored in the table  Channel .

Launch an application

Run the application and enter text in the  Insert A TodoItem , then click  Save (Figure 21).

clip_image037

Fig. 21. Application Interface

Run on multiple machines your application and repeat the procedure, note that all will come push-notification from WNS (Figure 22).

clip_image038

Fig. 22. Application Interface

In this walkthrough, we examined the translation application development Store  Windows , using  Windows  Azure  Mobile  Services  as cloud backend to various problems.