public async Task<HttpResponseMessage> Register(Registration registration) { // You'd also want to verify that the user in the registration is allowed to register for that tag // Get notification hubs registrations for that "username" tag. // Usually, you should get only 1, if we get more than one (for example, during development you make mistakes), delete all others // The "100" parameter just represents how many registrations to return var registrationsForUser = await hub.GetRegistrationsByTagAsync(registration.Username, 100); bool firstRegistration = true; bool existingRegistration = false; foreach (var reg in registrationsForUser) { if (firstRegistration) { // Update the registration with the incoming channel var winReg = reg as WindowsRegistrationDescription; winReg.ChannelUri = new Uri(registration.Channel); winReg.Tags = new HashSet<string> { registration.Username }; await hub.UpdateRegistrationAsync(winReg); firstRegistration = false; existingRegistration = true; } else { // Delete other registrations for that user await hub.DeleteRegistrationAsync(reg); } } // Register with the notification hub and listen to the "username" tag, if this is a new registration if (!existingRegistration) { switch (registration.Platform) { case "WindowsStore": await hub.CreateWindowsNativeRegistrationAsync(registration.Channel, new List<string> { registration.Username }); break; default: // If the platform isn't supported, return Bad Request return Request.CreateErrorResponse(HttpStatusCode.BadRequest, registration.Platform + " platform is not supported."); } } return Request.CreateResponse(HttpStatusCode.OK); }
private async void registerButton_Click(object sender, RoutedEventArgs e) { // Disable button registerButton.IsEnabled = false; yourUsernameTextBox.IsEnabled = false; // Obtain a channel var channel = await PushNotificationChannelManager.CreatePushNotificationChannelForApplicationAsync(); channel.PushNotificationReceived += channel_PushNotificationReceived; var registration = new Registration { Username = yourUsernameTextBox.Text, Channel = channel.Uri, Platform = "WindowsStore" }; // Call our server to register this channel using (var httpClient = new HttpClient()) { var serializedBody = JsonConvert.SerializeObject(registration); await httpClient.PostAsync(baseUrl + "/api/chat/register", new StringContent(serializedBody, Encoding.UTF8, "application/json")); } // Show the messages panel registerButton.Content = "Ok!"; messagesPanel.Visibility = Visibility.Visible; }