53
Windows Azure Mobile Services next stage Teemu Tapanila Screen Interaction [email protected] @TapanilaT

CloudBrew: Windows Azure Mobile Services - Next stage

Embed Size (px)

DESCRIPTION

Have a need for backend but don't have people to build it for you? Windows Azure Mobile Service is service to build backend service quickly with all of the often needed features. In this session we will start from basics and continue to next level from there and look how you can start to use the service for advanced scenarios!

Citation preview

Page 1: CloudBrew: Windows Azure Mobile Services - Next stage

Windows Azure Mobile Services next stageTeemu TapanilaScreen Interaction

[email protected]@TapanilaT

Page 2: CloudBrew: Windows Azure Mobile Services - Next stage

Thank you, sponsors!

Page 3: CloudBrew: Windows Azure Mobile Services - Next stage

What is Mobile Services?

Data

Notifications

Auth

Server Scripts

Scheduler

Logging & Diag

Scale

Page 4: CloudBrew: Windows Azure Mobile Services - Next stage

Authentication

Page 5: CloudBrew: Windows Azure Mobile Services - Next stage

Authentication out of the box• Microsoft Account• Twitter login• Google login• Facebook login

Page 6: CloudBrew: Windows Azure Mobile Services - Next stage

Developers.facebook.com

Page 7: CloudBrew: Windows Azure Mobile Services - Next stage

Setup facebook

Page 8: CloudBrew: Windows Azure Mobile Services - Next stage

Setup Mobile Services

Page 9: CloudBrew: Windows Azure Mobile Services - Next stage

Create Windows 8 project

Page 10: CloudBrew: Windows Azure Mobile Services - Next stage

Set table access level

Page 11: CloudBrew: Windows Azure Mobile Services - Next stage

Windows 8 sample application

Page 12: CloudBrew: Windows Azure Mobile Services - Next stage

Enable authenicationprivate MobileServiceUser user;

private async Task Authenticate()

{

user = await App.MobileService.LoginAsync(MobileServiceAuthenticationProvider.Facebook);

var message = string.Format("You are now logged in - {0}", user.UserId);

var dialog = new MessageDialog(message);

dialog.Commands.Add(new UICommand("OK"));

await dialog.ShowAsync();

}

protected override async void OnNavigatedTo(NavigationEventArgs e)

{

await Authenticate();

RefreshTodoItems();

}

Page 13: CloudBrew: Windows Azure Mobile Services - Next stage

See results on Windows 8 app

Page 14: CloudBrew: Windows Azure Mobile Services - Next stage

Authentication with groups

Page 15: CloudBrew: Windows Azure Mobile Services - Next stage

Enable authenicationprivate MobileServiceUser user;

private async Task Authenticate()

{

user = await App.MobileService.LoginAsync(MobileServiceAuthenticationProvider.Facebook);

var message = string.Format("You are now logged in - {0}", user.UserId);

var dialog = new MessageDialog(message);

dialog.Commands.Add(new UICommand("OK"));

await dialog.ShowAsync();

}

protected override async void OnNavigatedTo(NavigationEventArgs e)

{

await Authenticate();

RefreshTodoItems();

}

Page 16: CloudBrew: Windows Azure Mobile Services - Next stage

Authentication with groups on clientprivate FacebookSessionClient FacebookSessionClient = new FacebookSessionClient("1434104456805487");

private FacebookSession fbSession;

private MobileServiceUser user;

private async Task Authenticate()

{

fbSession = await FacebookSessionClient.LoginAsync("user_groups,user_likes,email");

var client = new FacebookClient(fbSession.AccessToken);

var token = JObject.FromObject(new { access_token = fbSession.AccessToken});

user = await App.MobileService.LoginAsync(MobileServiceAuthenticationProvider.Facebook, token);

}

protected override async void OnNavigatedTo(NavigationEventArgs e)

{

await Authenticate();

RefreshTodoItems();

}

Page 17: CloudBrew: Windows Azure Mobile Services - Next stage

Server scripts

Page 18: CloudBrew: Windows Azure Mobile Services - Next stage

CRUD• Create• Read• Update• Delete

Page 19: CloudBrew: Windows Azure Mobile Services - Next stage

Read operation

function read(query, user, request) {request.execute();

}

Page 20: CloudBrew: Windows Azure Mobile Services - Next stage

Read operation with filteringfunction read(query, user, request) {

var identities = user.getIdentities();var req = require('request');var fbAccessToken = identities.facebook.accessToken;var url = 'https://graph.facebook.com/me?fields=groups.fields(id)&access_token=' +

fbAccessToken;req(url, function (err, resp, body) {

var userData = JSON.parse(body);var groups = userData.groups.data;for(var i=0;i<groups.length;i++){

if (groups[i].id == "304013539633881"){

request.execute();return;

}}

query.take(2);request.execute();});

}

Page 21: CloudBrew: Windows Azure Mobile Services - Next stage

Data

Page 22: CloudBrew: Windows Azure Mobile Services - Next stage

Major features of data handling• Dynamic schema• Windows Azure SQL Database

Page 23: CloudBrew: Windows Azure Mobile Services - Next stage

Changing table model

public class TodoItem{

public string Id { get; set; }

[JsonProperty(PropertyName = "text")]public string Text { get; set; }

[JsonProperty(PropertyName = "complete")]public bool Complete { get; set; }

public string Platfrom { get; set; }}

Page 24: CloudBrew: Windows Azure Mobile Services - Next stage

Changing handling logicprivate async void UpdateCheckedTodoItem(TodoItem item){

if (item.Platfrom == null){

item.Platfrom = "Windows 8";}await todoTable.UpdateAsync(item);items.Remove(item);

}

private void ButtonSave_Click(object sender, RoutedEventArgs e){

var todoItem = new TodoItem { Text = TextInput.Text, Platfrom = "Windows 8" };InsertTodoItem(todoItem);

}

Page 25: CloudBrew: Windows Azure Mobile Services - Next stage

Data at portal

Page 26: CloudBrew: Windows Azure Mobile Services - Next stage

Data at SQL explorer

Page 27: CloudBrew: Windows Azure Mobile Services - Next stage

Notifications

Page 28: CloudBrew: Windows Azure Mobile Services - Next stage

Notifications• Windows Phone• Android• iPhone• Windows 8

Page 29: CloudBrew: Windows Azure Mobile Services - Next stage

Adding notifications to Windows 8

Page 30: CloudBrew: Windows Azure Mobile Services - Next stage

Visual Studio wizard

Page 31: CloudBrew: Windows Azure Mobile Services - Next stage

Reserve application name

Page 32: CloudBrew: Windows Azure Mobile Services - Next stage

Choose used mobile service

Page 33: CloudBrew: Windows Azure Mobile Services - Next stage

Notification is done!1. App start and send channel to mobile

service2. Mobile service saves that into database

and sends notification

Page 34: CloudBrew: Windows Azure Mobile Services - Next stage

Client implementation 2.0public async static void UploadChannel(MobileServiceUser user){

var channel = awaitPushNotificationChannelManager.CreatePushNotificationChannelForApplicationAsync();

var token = HardwareIdentification.GetPackageSpecificToken(null);string installationId = CryptographicBuffer.EncodeToBase64String(token.Id);var ch = new Jobject{

{"channelUri", channel.Uri},{"installationId", installationId},{"userId", user.UserId.Substring(9)}

};await App.MobileService.GetTable("channels").InsertAsync(ch);

}

private MobileServiceUser user;protected override async void OnNavigatedTo(NavigationEventArgs e){

await Authenticate();cloudbrewtapanila.cloudbrewtapanilaPush.UploadChannel(user);RefreshTodoItems();

}

Page 35: CloudBrew: Windows Azure Mobile Services - Next stage

TodoItem Insertfunction insert(item, user, request) {

var identities = user.getIdentities();var fbUserId = identities.facebook.userId;var ct = tables.getTable("channels");

ct.where({ userId: fbUserId }).read({success: function (results) {

if (results.length > 0) {sendNotifications(results[0].channelUri,item);

}}

});function sendNotifications(uri,todoItem) {

push.wns.sendToastText01(uri, { text1: "You have just inserted new todo " + todoItem.text },

{success: function (pushResponse) {

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

});}request.execute();

}

Page 36: CloudBrew: Windows Azure Mobile Services - Next stage

Version control

Page 37: CloudBrew: Windows Azure Mobile Services - Next stage

Enable from portal

Page 38: CloudBrew: Windows Azure Mobile Services - Next stage

Git repository

Page 39: CloudBrew: Windows Azure Mobile Services - Next stage

Clone your git repository

Page 40: CloudBrew: Windows Azure Mobile Services - Next stage

Folder structure

Page 41: CloudBrew: Windows Azure Mobile Services - Next stage

NPM install

Page 42: CloudBrew: Windows Azure Mobile Services - Next stage

Sendgrid

Page 43: CloudBrew: Windows Azure Mobile Services - Next stage

Sending email

var req = require('request');var fbAccessToken = identities.facebook.accessToken;var url = 'https://graph.facebook.com/me?fields=email&access_token=' + fbAccessToken;req(url, function (err, resp, body) {

var userData = JSON.parse(body);var userEmail = userData.email;var sendgrid = require('sendgrid')("TapanilaCloudBrew", "password");sendgrid.send({

to: userEmail,from: '[email protected]',subject: ‘New todoitem added',text: ‘You added new todoitem ‘ + todoItem.text

});});

Page 44: CloudBrew: Windows Azure Mobile Services - Next stage

Scheduler

Page 45: CloudBrew: Windows Azure Mobile Services - Next stage

Creating scheduler job

Page 46: CloudBrew: Windows Azure Mobile Services - Next stage

BrewCloudfunction BrewCloud() {

var td = tables.getTable("TodoItem");td.where({ complete: false }).read({success: function (results) {

if (results.length > 0) {var sendgrid = require('sendgrid')("TapanilaCloudBrew", "password");

sendgrid.send({to: "[email protected]",from: '[email protected]',subject: 'Status of brewing cloud',text: 'There is ' + results.length + " items left“

});}

}});

}

Page 47: CloudBrew: Windows Azure Mobile Services - Next stage

Logging

Page 48: CloudBrew: Windows Azure Mobile Services - Next stage

Log on portal

Page 49: CloudBrew: Windows Azure Mobile Services - Next stage

Log on Visual Studio

Page 50: CloudBrew: Windows Azure Mobile Services - Next stage

Summary

Page 51: CloudBrew: Windows Azure Mobile Services - Next stage

What is Mobile Services?

Data

Notifications

Auth

Server Scripts

Scheduler

Logging & Diag

Scale

Page 52: CloudBrew: Windows Azure Mobile Services - Next stage

Thank you!Teemu TapanilaScreen Interaction

[email protected]@TapanilaT

Page 53: CloudBrew: Windows Azure Mobile Services - Next stage

The Cloud for Modern Business

Grab your benefit

aka.ms/azuretry

Deploy fast in the cloud, scale elastically and minimize test cost

Activate your Windows Azure MSDN benefit at no additional charge

aka.ms/msdnsubscr