Windows Phone Windows 8 Push Notification from Windows Azure(4)

1. 使用命名空间

 

using Windows.Networking.PushNotifications;

 

2. App文件中添加代码

 

public static PushNotificationChannel CurrentChannel { get; private set; } private async void AcquirePushChannel() { CurrentChannel = await PushNotificationChannelManager.CreatePushNotificationChannelForApplicationAsync(); }

 

3. OnLaunched 事件中添加

 

AcquirePushChannel();

 

4. TodoItem类中添加属性

 

[DataMember(Name = "channel")] public string Channel { get; set; }

 

5. 在MainPage中的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插入数据脚本。我这里是插入数据时推送所有设备信息(Win8 & windows Phone 土司消息)

 

function insert(item1, user, request) { request.execute(); var permissionsTable = tables.getTable('todoitem'); permissionsTable.where(function(){ return this.channel !== null }).read({ success: function(channels) { console.log("read data:", channels.length ); var pushString= 'Hello'; channels.forEach(function(item) { if(item.iswin8data === true){ push.wns.sendToastText04(item.channel, { text1: item1.text }, { success: function(pushResponse) { console.log("Sent push:", pushResponse); } }); }else{ push.mpns.sendToast(item.channel, { text1: item1.text, test2: new Date().toISOString().replace(/T/, ' ').replace(/\..+/, ''), param: '/Page2.xaml?NavigatedFrom=Toast Notification' }, { success: function (pushResponse) { console.log("Sent toast push sucess:",pushResponse+" "+item.channel); }, error: function (failedResponse){ console.log("Sent toast push fail:", failedResponse); } }); } }); }}); }

 

image

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/83df78485feeb67d6e486d573aa2133c.html