public async Task<HttpResponseMessage> Send(ChatMessage message) { // Do verification that both users can send messages to each other // For example, they are friends // Then prepare the toast notification payload string toastPayload = "<toast> " + " <visual>" + " <binding template=\"ToastText02\">" + " <text id=\"1\">{0}</text>" + " <text id=\"2\">{1}</text>" + " </binding>" + " </visual>" + "</toast>"; toastPayload = string.Format(toastPayload, message.FromUsername, message.MessageText); // Send a Windows 8 toast notification to the "tag" await hub.SendWindowsNativeNotificationAsync(toastPayload,message.ToUsername); // Send a raw XML notification to update the UI as well in realtime, if the app is opened // Serialize the chat message into XML XmlSerializer xmlSerializer = new XmlSerializer(typeof(ChatMessage)); StringWriter serializedStringWriter = new StringWriter(); xmlSerializer.Serialize(serializedStringWriter, message); // Send the notification var rawChatNotification = new WindowsNotification(serializedStringWriter.ToString(), message.ToUsername); rawChatNotification.Headers.Add("X-WNS-Type", "wns/raw"); rawChatNotification.ContentType = "application/octet-stream"; await hub.SendNotificationAsync(rawChatNotification); return Request.CreateResponse(HttpStatusCode.OK); }
private async void sendMessageButton_Click(object sender, RoutedEventArgs e) { sendMessageButton.IsEnabled = false; var message = new ChatMessage { FromUsername = yourUsernameTextBox.Text, ToUsername = toUsernameTextBox.Text, MessageText = messageTextBox.Text }; // Add the message to our UI before sending it out ChatMessages.Add(message); // Call our server send a message using (var httpClient = new HttpClient()) { var serializedBody = JsonConvert.SerializeObject(message); await httpClient.PostAsync(baseUrl + "/api/chat/send", new StringContent(serializedBody, Encoding.UTF8, "application/json")); } // Clean up UI sendMessageButton.IsEnabled = true; messageTextBox.Text = string.Empty; }