`
thecloud
  • 浏览: 872395 次
文章分类
社区版块
存档分类
最新评论

微软云计算技术Windows Azure专题(二):如何利用Mobile向Windows商店应用推送消息

 
阅读更多

本文介绍了如何使用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。

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

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

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

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推送


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

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

2.修改脚本(script),我们这里仅仅对表的插入操作进行修改。

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推送。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics