Upload
teemu-tapanila
View
620
Download
2
Tags:
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
Windows Azure Mobile Services next stageTeemu TapanilaScreen Interaction
[email protected]@TapanilaT
Thank you, sponsors!
What is Mobile Services?
Data
Notifications
Auth
Server Scripts
Scheduler
Logging & Diag
Scale
Authentication
Authentication out of the box• Microsoft Account• Twitter login• Google login• Facebook login
Developers.facebook.com
Setup facebook
Setup Mobile Services
Create Windows 8 project
Set table access level
Windows 8 sample application
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();
}
See results on Windows 8 app
Authentication with groups
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();
}
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();
}
Server scripts
CRUD• Create• Read• Update• Delete
Read operation
function read(query, user, request) {request.execute();
}
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();});
}
Data
Major features of data handling• Dynamic schema• Windows Azure SQL Database
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; }}
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);
}
Data at portal
Data at SQL explorer
Notifications
Notifications• Windows Phone• Android• iPhone• Windows 8
Adding notifications to Windows 8
Visual Studio wizard
Reserve application name
Choose used mobile service
Notification is done!1. App start and send channel to mobile
service2. Mobile service saves that into database
and sends notification
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();
}
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();
}
Version control
Enable from portal
Git repository
Clone your git repository
Folder structure
NPM install
Sendgrid
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
});});
Scheduler
Creating scheduler job
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“
});}
}});
}
Logging
Log on portal
Log on Visual Studio
Summary
What is Mobile Services?
Data
Notifications
Auth
Server Scripts
Scheduler
Logging & Diag
Scale
Thank you!Teemu TapanilaScreen Interaction
[email protected]@TapanilaT
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