Windows Phone 72011. 5. 27. 18:43

마지막이다.

 

이제 메세지를 보낼 UI를 만들어보자 관리자를 위한 프로그램이다

 

난 그냥 쉬운 WPF로 만들었다.

 

일단 버튼 하나 만들고

 

private void ToastButton_Click(object sender, RoutedEventArgs e)
      {
          string appId = "1";
          //string baseUri = string.Format("http://localhost:19976/Notifications/GetSubscribers?appId={0}", appId);
          string baseUri = string.Format("http://192.168.10.174:6060/Notifications/GetSubscribers?appId={0}", appId);
 
          GetSubscribers(baseUri);
          
      }

 

 

private void GetSubscribers(string baseUri)
     {
         WebClient webclient = new WebClient();
         webclient.DownloadDataCompleted += new DownloadDataCompletedEventHandler(webclient_DownloadDataCompleted);
         webclient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(webclient_DownloadStringCompleted);
         webclient.DownloadStringAsync(new Uri(baseUri));
     }

 

이렇게 WCF 를 호출해서 사용자 URI들을 받는다. (10명이 등록 되어 있으면 서버가 10개가 들어 있는 List를 보내줌)

 

void webclient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
       {                           
           MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(e.Result));
           DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(List<Uri>));
           deserializedListUri = serializer.ReadObject(ms) as List<Uri>;
 
           SendToast();
       }

 

자 다운이 다 되면 List로  Deserialize 한후

 

private void SendToast()
       {
 
           HttpWebRequest sendNotificationRequest = (HttpWebRequest)WebRequest.Create(deserializedListUri[0]);
 
           // We will create a HTTPWebRequest that posts the toast notification to the Microsoft Push Notification Service.
           // HTTP POST is the only allowed method to send the notification.
           sendNotificationRequest.Method = "POST";
 
           // The optional custom header X-MessageID uniquely identifies a notification message. 
           // If it is present, the // same value is returned in the notification response. It must be a string that contains a UUID.
           // sendNotificationRequest.Headers.Add("X-MessageID", "<UUID>");
 
           var title = "test Title";
           var subtitle = "test SubTitle";
           // Create the toast message.
 
 
           string toastMessage = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
           "<wp:Notification xmlns:wp=\"WPNotification\">" +
              "<wp:Toast>" +
                   "<wp:Text1>" + title + "</wp:Text1>" +
                   "<wp:Text2>" + subtitle + "</wp:Text2>" +
              "</wp:Toast> " +
           "</wp:Notification>";
 
           // Sets the notification payload to send.
           byte[] notificationMessage = Encoding.Default.GetBytes(toastMessage);
 
           // Sets the web request content length.
           sendNotificationRequest.ContentLength = notificationMessage.Length;
           sendNotificationRequest.ContentType = "text/xml";
           sendNotificationRequest.Headers.Add("X-WindowsPhone-Target", "toast");
           sendNotificationRequest.Headers.Add("X-NotificationClass", "2");
 
           using (Stream requestStream = sendNotificationRequest.GetRequestStream())
           {
               requestStream.Write(notificationMessage, 0, notificationMessage.Length);
           }
 
           // Send the notification and get the response.
           HttpWebResponse response = (HttpWebResponse)sendNotificationRequest.GetResponse();
           string notificationStatus = response.Headers["X-NotificationStatus"];
           string notificationChannelStatus = response.Headers["X-SubscriptionStatus"];
           string deviceConnectionStatus = response.Headers["X-DeviceConnectionStatus"];
 
           // Display the response from the Microsoft Push Notification Service.  
           // Normally, error handling code would be here.  In the real world, because data connections are not always available,
           // notifications may need to be throttled back if the device cannot be reached.
 
           resultTextBlock.Text = notificationStatus + " | " + deviceConnectionStatus + " | " + notificationChannelStatus;
       }
 
이렇게 보낸다 xml 형식으로 만들어서 보내면 되며
 
상태들도 header정보를 받아서 볼수 있다.

Posted by 동동(이재동)