微软云技术Windows Azure专题(二):如何利用Mobile向Windows商店应用推送消息(1)
本文介绍了如何使用Windows Azure的Mobile Service发送推送信息Windows商店应用程序。
2.新建一个商店应用并关联到应用商店,这里我们可以用MobileService门户上给的样例程序。按照步骤来获取。
3.关联Windows商店应用到Mobile Service。方法和上一讲的关联一样,这里不多说了。
第二步:通过代码在应用中添加推送通知
1.在App.cs中添加下列引用
public static PushNotificationChannel CurrentChannel { get; private set; }private async void AcquirePushChannel(){ CurrentChannel = await PushNotificationChannelManager.CreatePushNotificationChannelForApplicationAsync();}
3.在OnLanched()函数中添加
[JsonProperty(PropertyName = "channel")]public string Channel { get; set; }
5.修改MainPage.xaml.cs中的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推送。