相信大家多多少少已经对 Windows Azure 云平台有所耳闻,现在的互联网已经进入云+端的时代,我们手中的 PC 平板 手机 对网络的依赖程度日益深入,尤其是一些社交类型的应用更是需要一些信息的推送,之前我给大家介绍过关于windows phone 的推送服务,今天主要给大家介绍一下 基于微软云平台的手机推送服务。

首先使用Mobile service除了要安装我们的VS2012 + WP8 SDK以外 还要安装 Mobile Services SDK 

首先我们要登录 Management Portal Windows Azure的管理页面(当然你已经有一个 Windows Azure的订阅)。

Windows Phone & Windows 8 Push Notification from Windows Azure_windows phone

可以看到左侧的 Mobile service 或者点击左下角的添加按钮 选择创建一个新的Mobile service.

Windows Phone & Windows 8 Push Notification from Windows Azure_windows phone_02

随后会弹出创建 Mobile Service 的向导, 输入你的URL指向,以及数据库连接,最后一个选项是选择你的数据中心的位置。

Windows Phone & Windows 8 Push Notification from Windows Azure_windows phone_03

当然这里如果你选择的是使用一个新的数据库 会要求输入数据库名称和 登录名称和密码. 点击完成按钮

Windows Phone & Windows 8 Push Notification from Windows Azure_WP8_04

随后你可以在Mobile Service的选项下看到你刚创建的服务.

Windows Phone & Windows 8 Push Notification from Windows Azure_Windows Azure_05

 

Windows Phone & Windows 8 Push Notification from Windows Azure_Windows Azure_06

随后你可以选择下载一个代码示例项目或者将你已有的一个项目添加到Mobile Service中,我这里直接选择下载Windows Azure的 Demo Code.

运行你的项目发现已经可以和Mobile Service进行数据交互了, 是不是很简单?

Windows Phone & Windows 8 Push Notification from Windows Azure_WP8_07

 

Windows Phone & Windows 8 Push Notification from Windows Azure_WP8_08

在我们的服务中可以直接浏览到数据表中的数据.

Windows Phone & Windows 8 Push Notification from Windows Azure_Windows Azure_09

 

当然这里也有 Win8 版本的demo code下载。

Windows Phone & Windows 8 Push Notification from Windows Azure_WP8_10

对于推送Windows Phone是这样的 客户端和之前没什么太多区别还是要注册手机推送通道.

在Manifest文件中标记推送

Windows Phone & Windows 8 Push Notification from Windows Azure_WP8_11

在手机App文件中添加以下代码

1. 引入命名空间


  1. using Microsoft.Phone.Notification; 

2. 添加以下代码

  1. public static HttpNotificationChannel CurrentChannel { getprivate set; } 
  2.  
  3.  
  4. private void AcquirePushChannel() 
  5.     CurrentChannel = HttpNotificationChannel.Find("MyPushChannel"); 
  6.  
  7.  
  8. if (CurrentChannel == null
  9.     CurrentChannel = new HttpNotificationChannel("MyPushChannel"); 
  10.     CurrentChannel.Open(); 
  11.     CurrentChannel.BindToShellTile(); 
  12.  
  13.  

3. 在Application_Launching事件方法中添加方法调用

  1. AcquirePushChannel(); 

 4.在TodoItem类中添加一个字段

  1. [DataMember(Name = "channel")] 
  2.  public string Channel { getset; } 

 5. 最后在MainPage页面中更改ButtonSave_Click事件响应代码

  1. private void ButtonSave_Click(object sender, RoutedEventArgs e) 
  2.     var todoItem = new TodoItem { Text = TodoInput.Text,  
  3.         Channel = App.CurrentChannel.ChannelUri.ToString() }; 
  4.     InsertTodoItem(todoItem); 

在 Windows Azure 云端我们要编辑下插入数据时的脚本代码

选择Data(数据) – Script (脚本) – Insert(插入)

Windows Phone & Windows 8 Push Notification from Windows Azure_WP8_12

Windows Phone & Windows 8 Push Notification from Windows Azure_WP8_13

更新代码如下:

  1. function insert(item, user, request) { 
  2.     request.execute({ 
  3.         success: function () { 
  4.             // Write to the response and then send the notification in the background 
  5.             request.respond(); 
  6.             push.mpns.sendFlipTile(item.channel, { 
  7.                 title: item.text 
  8.             }, { 
  9.                 success: function (pushResponse) { 
  10.                     console.log("Sent push:", pushResponse); 
  11.                 } 
  12.             }); 
  13.         } 
  14.     }); 

此时我们部署我们的项目到模拟器或者手机并且把我们应用的Tile ping到桌面上.

插入一条数据后,检查我们的Tile图标已经推送了一条消息过来了。

Windows Phone & Windows 8 Push Notification from Windows Azure_windows phone_14

Windows Phone & Windows 8 Push Notification from Windows Azure_windows phone_15

以上其实是WindowsAzure网站上的一个快速指导 我给大家搬过来加以总结, 不过我想相信大家不仅仅是使用Tile的推送这里Mobile 还支持土司消息的推送。

Windows Phone & Windows 8 Push Notification from Windows Azure_windows phone_16

Mobile Service 不仅仅支持 Windows Phone 同样支持 windows 8 的消息推送 ,下面我介绍下如何配置Windows 8 的Mobile service消息推送。

这里我就用上面 Windows Azure刚刚建立的TodoList表不在单独建立数据库。同样可以从Windows Azure网站上下载 Windows 8的 DEMO 示例代码稍加修改就可以支持我们的Windows 8 消息推送了。

Windows 8 的注册要比Windows Phone负责一点,要在Windows 应用商店先注册并且拿到你的 应用推送的 CLIENT SECRET 和 PACKAGE SID 操作如下:

首先你要先登录 Submit an app page 注册你的Win8应用并且在给你的应用预留一个应用名称.

Windows Phone & Windows 8 Push Notification from Windows Azure_windows phone_17

Windows Phone & Windows 8 Push Notification from Windows Azure_WP8_18

随后在VS中关联应用商店中的应用

Windows Phone & Windows 8 Push Notification from Windows Azure_WP8_19

Windows Phone & Windows 8 Push Notification from Windows Azure_WP8_20

接着在 Windows dev Center 中选择 Advanced features

Windows Phone & Windows 8 Push Notification from Windows Azure_Windows Azure_21

选择 Authenticating your service 并且记录下  Client secret and Package security identifier (SID).

Windows Phone & Windows 8 Push Notification from Windows Azure_Windows Azure_22

Windows Phone & Windows 8 Push Notification from Windows Azure_WP8_23

将记录的ID上传到Windows Azure中的 push(推送标签栏中)。

Windows Phone & Windows 8 Push Notification from Windows Azure_WP8_24

Windows Phone & Windows 8 Push Notification from Windows Azure_WP8_25

当然我们的Windwos 8 应用也要声明支持推送服务

Windows Phone & Windows 8 Push Notification from Windows Azure_WP8_26

1. 使用命名空间

  1. using Windows.Networking.PushNotifications; 

2. App文件中添加代码

  1. public static PushNotificationChannel CurrentChannel { getprivate set; } 
  2.  
  3.  
  4. private async void AcquirePushChannel() 
  5.         CurrentChannel =   
  6.             await PushNotificationChannelManager.CreatePushNotificationChannelForApplicationAsync(); 

3. OnLaunched 事件中添加

  1. AcquirePushChannel(); 

4. TodoItem类中添加属性

  1. [DataMember(Name = "channel")] 
  2.  public string Channel { getset; } 


5. 在MainPage中的ButtonSave_Click事件中添加代码

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

 另外我这里更新Windows Azure插入数据脚本。我这里是插入数据时推送所有设备信息(Win8 & windows Phone 土司消息)

  1. function insert(item1, user, request) { 
  2.  
  3.     request.execute();  
  4.      
  5.     var permissionsTable = tables.getTable('todoitem');  
  6.      
  7. permissionsTable.where(function(){  
  8.     return this.channel !== null  
  9. }).read({ success: function(channels) {  
  10.      console.log("read data:", channels.length );  
  11.       
  12.      var pushString= 'Hello';  
  13.       
  14.        channels.forEach(function(item) {  
  15.            if(item.iswin8data === true){  
  16.      push.wns.sendToastText04(item.channel, {  
  17.                 text1: item1.text  
  18.             }, {  
  19.                 success: function(pushResponse) {  
  20.                     console.log("Sent push:", pushResponse);  
  21.                 }  
  22.             });  
  23.            }else{  
  24.                    push.mpns.sendToast(item.channel, {  
  25.         text1: item1.text,  
  26.         test2: new Date().toISOString().replace(/T/, ' ').replace(/\..+/, ''),  
  27.         param: '/Page2.xaml?NavigatedFrom=Toast Notification'  
  28.     }, {  
  29.        success: function (pushResponse) {  
  30.                     console.log("Sent toast push sucess:",pushResponse+" "+item.channel);  
  31.                 },  
  32.                    error: function (failedResponse){  
  33.                     console.log("Sent toast push fail:", failedResponse);  
  34.                 }  
  35.     });  
  36.            }  
  37.                  
  38.              });  
  39.               
  40.       
  41. }}); 
  42.  

Windows Phone & Windows 8 Push Notification from Windows Azure_Windows Azure_27

欢迎大家在这里和我沟通交流或者在新浪微博上 @王博_Nick