public async Task <RegistrationDescription> Post([FromBody] JObject registrationCall) { // Get the registration info that we need from the request. var platform = registrationCall["platform"].ToString(); var installationId = registrationCall["instId"].ToString(); var channelUri = registrationCall["channelUri"] != null ? registrationCall["channelUri"].ToString() : null; var deviceToken = registrationCall["deviceToken"] != null ? registrationCall["deviceToken"].ToString() : null; var registrationId = registrationCall["registrationId"] != null ? registrationCall["registrationId"].ToString() : null; var userName = HttpContext.Current.User.Identity.Name; // Get registrations for the current installation ID. var regsForInstId = await _hubClient.GetRegistrationsByTagAsync(installationId, 100); bool updated = false; bool firstRegistration = true; RegistrationDescription registration = null; // Check for existing registrations. foreach (var registrationDescription in regsForInstId) { if (firstRegistration) { // Update the tags. registrationDescription.Tags = new HashSet <string> { installationId, userName }; // We need to handle each platform separately. switch (platform) { case "windows": var winReg = registrationDescription as WindowsRegistrationDescription; if (winReg != null) { if (channelUri != null) { winReg.ChannelUri = new Uri(channelUri); } registration = await _hubClient.UpdateRegistrationAsync(winReg); } break; case "android": var gcmReg = registrationDescription as GcmRegistrationDescription; if (gcmReg != null) { gcmReg.GcmRegistrationId = registrationId; registration = await _hubClient.UpdateRegistrationAsync(gcmReg); } break; case "ios": var iosReg = registrationDescription as AppleRegistrationDescription; if (iosReg != null) { iosReg.DeviceToken = deviceToken; registration = await _hubClient.UpdateRegistrationAsync(iosReg); } break; } updated = true; firstRegistration = false; } else { // We shouldn't have any extra registrations; delete if we do. await _hubClient.DeleteRegistrationAsync(registrationDescription); } } // Create a new registration. if (!updated) { string template; switch (platform) { case "windows": template = @"<toast><visual><binding template=""ToastText01""><text id=""1"">$(message)</text></binding></visual></toast>"; await _hubClient.CreateWindowsTemplateRegistrationAsync(channelUri, template, new string[] { installationId, userName }); break; case "android": template = "{\"data\":{\"message\":\"$(message)\"}}"; registration = await _hubClient.CreateGcmTemplateRegistrationAsync(registrationId, template, new[] { installationId, userName }); break; case "ios": template = "{\"aps\":{\"alert\":\"$(message)\"}, \"inAppMessage\":\"$(message)\"}"; await _hubClient.CreateAppleTemplateRegistrationAsync(deviceToken, template, new string[] { installationId, userName }); break; } } // Send out a test notification. SendNotification(string.Format("Test notification for {0}", userName), userName); return(registration); }
// Private async method that performs the actual registration, // since we can't return Task<T> from a service operation. private async Task <string> RegisterForPushNotificationsAsync( Registration clientRegistration) { // If the client gave us a registration ID, use it to try and get the registration. RegistrationDescription currentRegistration = null; if (!string.IsNullOrEmpty(clientRegistration.RegistrationId)) { // Try to get the current registration by ID. currentRegistration = await hubClient.GetRegistrationAsync <RegistrationDescription>( clientRegistration.RegistrationId); } // Update a current registration. if (currentRegistration != null) { // Update the current set of tags. foreach (string tag in clientRegistration.Tags) { if (!currentRegistration.Tags.Contains(tag)) { currentRegistration.Tags.Add(tag); } } // We need to update each platform separately. switch (clientRegistration.Platform) { case "windows": case "win8": // In the Windows Store case we need to delete and recreate the // registration because there's no way to change from a toast template // to a native raw notification. await DeleteRegistrationsAsync(currentRegistration.RegistrationId); clientRegistration.RegistrationId = await createWindowsRegistration(clientRegistration, currentRegistration.Tags); break; case "wp": case "windowsphone": await DeleteRegistrationsAsync(currentRegistration.RegistrationId); clientRegistration.RegistrationId = await createWindowsRegistration(clientRegistration, currentRegistration.Tags); break; case "ios": var iosReg = currentRegistration as AppleRegistrationDescription; // Update tags and device token. iosReg.DeviceToken = clientRegistration.DeviceToken; clientRegistration.RegistrationId = (await hubClient.UpdateRegistrationAsync(iosReg)).RegistrationId; break; } } // Create a new registration. else { // Create an ISet<T> of the supplied tags. HashSet <string> tags = new HashSet <string>(clientRegistration.Tags); // We need to create each platform separately. switch (clientRegistration.Platform) { case "windows": case "win8": // Call the method that creates Windows Store registrations. clientRegistration.RegistrationId = await createWindowsRegistration(clientRegistration, tags); break; case "ios": var template = @"{""aps"":{""alert"":""$(message)""}, ""inAppMessage"":""$(message)""}"; clientRegistration.RegistrationId = (await hubClient.CreateAppleTemplateRegistrationAsync( clientRegistration.DeviceToken, template, tags)).RegistrationId; break; default: throw new DataServiceException("Unsupported client platform."); } } return(clientRegistration.RegistrationId); }