---
title: "微软云计算技术Windows Azure专题（二）：如何利用Mobile向Windows商店应用推送消息"
author: "thecloud"
date: "2013-10-05T03:46:00Z"
tags: []
slug: "windows-azure-mobile-windows"
draft: false
lastmod: "2013-10-05T10:08:45Z"
id: 1950608
url: "https://thecloud.iteye.com/blog/1950608"
---

本文介绍了如何使用Windows Azure的Mobile Service发送推送信息Windows商店应用程序。

建立一个Mobile Service的同时Azure会自动创建一个数据库。推送消息就是在对数据库这些表的插入、删除操作的同时，通过脚本语言发送

先来明确一下大体上要做哪些步骤:

1.注册Windows商店应用的推送通知，配置Mobile Service。

2.通过代码在应用中添加推送通知。

3.在Windows Azure控制中心更新脚本语言来发送推送。

4.添加数据，并接收WNS推送。

**第一步：注册Windows商店应用的推送通知，配置Mobile Service**

1.在Windows Azure上新建一个Mobile Service。

![](http://www.windowsazure.com/media/itpro/services/notification-hub-create-from-portal.png)

![](http://www.windowsazure.com/media/devcenter/mobile/mobile-services-selection.png)

2.新建一个商店应用并关联到应用商店，这里我们可以用MobileService门户上给的样例程序。按照步骤来获取。

![](http://img.blog.csdn.net/20131005034040953?watermark%2F2%2Ftext%2FaHR0cDovL2Jsb2cuY3Nkbi5uZXQvYXJzZW5hbDExMDkzODk0ODA=%2Ffont%2F5a6L5L2T%2Ffontsize%2F400%2Ffill%2FI0JBQkFCMA%3D%3D%2Fdissolve%2F70%2Fgravity%2FSouthEast)

3.关联Windows商店应用到Mobile Service。方法和上一讲的关联一样，这里不多说了。

![](http://www.windowsazure.com/media/devcenter/mobile/mobile-push-tab.png)

**第二步：通过代码在应用中添加推送通知**

1.在App.cs中添加下列引用

using Windows.Networking.PushNotifications;

2.接着添加如下代码

```
public static PushNotificationChannel CurrentChannel { get; private set; }


private async void AcquirePushChannel()
{
        CurrentChannel =  
            await PushNotificationChannelManager.CreatePushNotificationChannelForApplicationAsync();
}
```

3.在OnLanched（）函数中添加

AcquirePushChannel();

4.在MainPage.xaml.cs中有一个TodoItem的类，在类中添加

```
[JsonProperty(PropertyName = "channel")]
public string Channel { get; set; }
```

5.修改MainPage.xaml.cs中的ButtonSave\_Click函数

```
private void ButtonSave_Click(object sender, RoutedEventArgs e)
    {
        var todoItem = new TodoItem { Text = TextInput.Text, Channel = App.CurrentChannel.Uri };
        InsertTodoItem(todoItem);
    }
```

6.不要忘了让你的程序支持Toast推送

![](http://www.windowsazure.com/media/itpro/services/notification-hub-win8-app-toast.png)

**第三步：在Windows Azure控制中心更新脚本语言来发送推送。**

1.点击服务上方的数据，然后点击TodoItem，其实这个TodoItem就是数据库中的表。

![](http://www.windowsazure.com/media/devcenter/mobile/mobile-portal-data-tables.png)

2.修改脚本（script），我们这里仅仅对表的插入操作进行修改。

![](http://www.windowsazure.com/media/devcenter/mobile/mobile-insert-script-push2.png)

3.将上边的脚本改为

```
function insert(item, user, request) {
    request.execute({
        success: function() {
            // Write to the response and then send the notification in the background
            request.respond();
            push.wns.sendToastText04(item.channel, {
                text1: item.text
            }, {
                success: function(pushResponse) {
                    console.log("Sent push:", pushResponse);
                }
            });
        }
    });
}
```

**第四步：添加数据，并接收WNS推送。**

![](http://www.windowsazure.com/media/devcenter/mobile/mobile-quickstart-push1.png)

![](http://www.windowsazure.com/media/devcenter/mobile/mobile-quickstart-push2.png)