2012-01-31 116 views
0

我對Windows Phone 7設備的推送通知服務有疑問: 我現在可以發送推送通知,使用Web應用程序到電話,更改數據的瓷磚。但問題是:當我啓動應用程序時,我需要在調試器輸出中顯示URI,然後將其複製粘貼到Web應用程序中,然後將該MPNS與適用於更新的MPNS聯繫一次一個電話。但我想創建一個可以自動進行多個調用的Web服務,檢索應用程序的URI(在關閉並打開應用程序後發生更改,我認爲),並向其發送推送通知。但我還沒有找到一個MSDN主題來處理這個問題。他們只是使用讚揚,說:「以後需要用URI來替換。」所以我的問題是:如何使用手機將這樣的消息發送給web服務,對其進行響應,然後再次連接到手機,處理此類請求? 還有:我是否需要經過身份驗證的Web服務,還是有調試版本?發送從Windows Phone設備到webservice的推送通知

這是我迄今:

/// <summary> 
    /// Setup a connection with a webservice, in order to update a shell, either a toast- or a tile shell. 
    /// </summary> 
    /// <param name="shellType">The type of shell you wish to update</param> 
    public void SetupShellChannel (ShellBindType shellType) 
    { 
     //holds the push notification that is created. Since we can only have one notification channel open at any one time, 
     //we will need to check for existance. That is why, the channelName shouldn't be changed 
     HttpNotificationChannel _httpChannel = HttpNotificationChannel.Find(_channelName); 

     //if the _httpChannel was not found (read: does not exist) 
     if (_httpChannel == null) 
     { 
      _httpChannel = new HttpNotificationChannel(_channelName ); 
      _httpChannel.Open(); 

      //because there is more than one shelltype we can open, we will use a switch to call the method we seek 
      BindToShell(_httpChannel, shellType); 
     } 
      //Only one push notification service is allowed per application, so we cannot send a tile notification, as well as 
      //a toast message notification. When we attempt this, we get a InvalidOperationException 
     else 
     { 
      //in this case, the _httpChannel did already exist, but the problem is, we cannot just add the eventHandlers, 
      //because there is the danger that it didn't exist, and we would get a null pointer exception. 
      //_httpChannel.ChannelUriUpdated += new EventHandler<NotificationChannelUriEventArgs>(httpChannel_ChannelUriUpdated); 
      //_httpChannel.ErrorOccurred += new EventHandler<NotificationChannelErrorEventArgs>(httpChannel_ErrorOccurred); 

      //For testing purposes, we now display the URI to the user, and as output. Normally, we would pass this URI back to the webserver 
      System.Diagnostics.Debug.WriteLine(_httpChannel.ChannelUri.ToString()); 
     } 

     //if (_httpChannel.ChannelUri) 

     //When the URI is updated, we want this to be sent to the server as well, so we know that the adress has changed, 
     //and don't just send data somewhere into the void. Also, when encountering an error, we want to show the user when 
     //an error has occured. 
     _httpChannel.ChannelUriUpdated += new EventHandler<NotificationChannelUriEventArgs>(HttpChannel_ChannelUriUpdated); 
     _httpChannel.ErrorOccurred += new EventHandler<NotificationChannelErrorEventArgs>(HttpChannel_ErrorOccurred); 
    } 

    //here, also we would return the URI to the server, but for debugging purposes, we display them to the user. 
    void HttpChannel_ChannelUriUpdated(object sender, NotificationChannelUriEventArgs e) 
    { 
     Deployment.Current.Dispatcher.BeginInvoke(() => 
     { 
      System.Diagnostics.Debug.WriteLine(e.ChannelUri.ToString()); 
      MessageBox.Show(String.Format("the URI is {0}", e.ChannelUri.ToString())); 
     }); 
    } 

    private void BindToShell(HttpNotificationChannel channel, ShellBindType shellType) 
    { 
     switch (shellType) 
     { 
      case ShellBindType.BindToShellTile: 
       channel.BindToShellTile(); 
       break; 
      case ShellBindType.BindToShellToast: 
       channel.BindToShellToast(); 
       break; 
     }   
    } 

    void HttpChannel_ErrorOccurred(object sender, NotificationChannelErrorEventArgs e) 
    { 
     //getting an error would be caugth here, and then displayed to the user. 
     Deployment.Current.Dispatcher.BeginInvoke(() => 
      { 
       MessageBox.Show(String.Format("A push notification {0} error occured. {1}{(2)}{3}", 
        e.ErrorType, e.Message, e.ErrorCode, e.ErrorAdditionalData)); 
      }); 
    } 
+0

您能否解釋qn的第一部分 - 如何使用手機向web服務發送此類消息,作出響應並再次連接到手機,以處理此類請求。 – whihathac 2012-02-07 21:43:56

回答

1

好吧,我明白你的問題。我所做的一切就是一旦我從MPNS獲得URI,我就以此爲參數調用服務的Web方法 - 訂閱(int subscriberId,Uri channelUri);

因此,您需要確保在應用中生成subscriberId以識別用戶並將其存儲在獨立存儲中。這可以是一個GUID。

該責任現在在hte服務器上將用戶保存到Uri映射到持久存儲中。

此外,您還需要提供UnSubscribe方法供用戶選擇退出推送通知。這是推送通知的認證要求之一。

現在談談你的第二個問題 - 是的,你需要確保你的服務 - 你不想處理未知的請求。

我個人做什麼,將它分成2個服務 - 發佈服務和訂閱服務。發佈服務將發送hte通知,而訂閱將具有訂閱/取消訂閱方法。

+0

謝謝你答案= D 我已經找到了答案,並做了類似的idd。我還沒有實現兩個獨立的web服務,這似乎是一個非常可行的事情,因爲我可以管理接收通知的設備。 – GeekPeek 2012-02-09 08:23:49

+0

歡迎您:) – whihathac 2012-02-09 19:51:15

0

我想你想問你可以發送推送通知從Windows Phone本身或不使用任何其他服務器端的ASP/PHP像MSDN中的示例應用程序中所述。是。您可以從手機/設備本身發送通知。您必須更改MSDN中給出的Sample應用程序的Send函數。如果您有任何疑問,請回復。

static async Task<string> SendPushNotification(string textToSend) 
{ 
    //You can maintain a DB to query different channel URIs of devices 
    string subscriptionUri = "<Uri To Which You Want Send Notification>"; 
    HttpWebRequest sendNotificationRequest = (HttpWebRequest)WebRequest.Create(subscriptionUri); 
    sendNotificationRequest.Method = "POST"; 

    string toastMessage = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" + 
     "<wp:Notification xmlns:wp=\"WPNotification\">" + 
      "<wp:Toast>" + 
       "<wp:Text1>" + textToSend + "</wp:Text1>" + 
       "<wp:Param>/NotificationDetails.xaml?Message=" + textToSend + "</wp:Param>" + 
      "</wp:Toast> " + 
     "</wp:Notification>"; 
    byte[] notificationMessage = Encoding.UTF8.GetBytes(toastMessage); 

    sendNotificationRequest.ContentLength = notificationMessage.Length; 
    sendNotificationRequest.ContentType = "text/xml"; 
    sendNotificationRequest.Headers["X-WindowsPhone-Target"] = "toast"; 
    sendNotificationRequest.Headers["X-NotificationClass"] = "2"; 

    using (var requestStream = await Task.Factory.FromAsync<Stream>(sendNotificationRequest.BeginGetRequestStream, sendNotificationRequest.EndGetRequestStream, null)) 
    { 
     requestStream.Write(notificationMessage, 0, notificationMessage.Length); 
    } 

    string notificationStatus; 
    using (var response = (HttpWebResponse)(await Task<WebResponse>.Factory.FromAsync(sendNotificationRequest.BeginGetResponse, sendNotificationRequest.EndGetResponse, null))) 
    { 
     //StreamReader reader = new StreamReader(response.GetResponseStream()); 
     //result = reader.ReadToEnd(); 
     notificationStatus = response.Headers["X-NotificationStatus"]; 
     MessageBox.Show(notificationStatus); 
    } 
    return notificationStatus; 
} 
+0

這適用於Windows/Windows Phone 8.1嗎? – 2015-09-15 09:43:11

相關問題