private async Task <string> createWindowsRegistration(Registration clientRegistration, ISet <string> tags)
        {
            string newId = string.Empty;

            // Register either a standard toast notification using templates or a raw noative notification
            // depending on what is requested buy the client.
            switch (clientRegistration.Type)
            {
            case "toast":
                var template =
                    @"<toast>
                                            <visual>
                                                <binding template=""ToastText04"">
                                                    <text id=""1"">{'Northwind data was ' + $(operation) + '...'}</text>
                                                    <text id=""2"">{'Feed: ' + $(feed)}</text>
                                                    <text id=""3"">{'Entity: ' + $(entity)}</text>
                                                </binding>  
                                            </visual>
                                        </toast>";

                // Register for the templated toast notification, and return the ID.
                newId = clientRegistration.RegistrationId =
                    (await hubClient.CreateWindowsTemplateRegistrationAsync(
                         clientRegistration.ChannelUri, template, tags)).RegistrationId;
                break;

            case "auto-update":
                // Register for the raw notification used by auto-updates, and return the ID.
                newId = clientRegistration.RegistrationId = (await hubClient.CreateWindowsNativeRegistrationAsync(
                                                                 clientRegistration.ChannelUri, tags)).RegistrationId;
                break;

            default:
                throw new DataServiceException("Unexpected notification type.");
            }
            return(newId);
        }
        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);
        }