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


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

 

 

第一步:注册Windows商店应用的推送通知,配置Mobile Service

 

 

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);
    }


 

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

 

 

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);
                }
            });
        }
    });
}


 

 

 

相关内容