public Dictionary <string, string> RegisterFCM() { HttpContent requestContent = Request.Content; string content = requestContent.ReadAsStringAsync().Result; Dictionary <string, string> tokenDictionary = JsonConvert.DeserializeObject <Dictionary <string, string> >(content); var person = GetPerson(); if (tokenDictionary.ContainsKey("Token") && !string.IsNullOrWhiteSpace(tokenDictionary["Token"]) && person != null) { var userAgent = Request.Headers.UserAgent.ToString(); var deviceId = Regex.Match(userAgent, "(?<=-).+(?=\\))").Value.Trim(); if (deviceId.Length > 20) { deviceId = deviceId.Substring(0, 20); } RockContext rockContext = new RockContext(); PersonalDevice personalDevice = AvalancheUtilities.GetPersonalDevice(deviceId, person.PrimaryAliasId, rockContext); if (personalDevice != null && personalDevice.DeviceRegistrationId != tokenDictionary["Token"]) { personalDevice.DeviceRegistrationId = tokenDictionary["Token"]; personalDevice.NotificationsEnabled = true; rockContext.SaveChanges(); } } return(new Dictionary <string, string> { { "Status", "Ok" } }); }
public void Execute() { if (PageId.HasValue || !string.IsNullOrWhiteSpace(ComponentName)) { using (var rockContext = new RockContext()) { int channelMediumTypeValueId = DefinedValueCache.Get(AvalancheUtilities.AppMediumValue.AsGuid()).Id; var interactionChannelService = new InteractionChannelService(rockContext); var interactionService = new InteractionService(rockContext); var interactionChannel = interactionChannelService.Queryable() .Where(a => a.ChannelTypeMediumValueId == channelMediumTypeValueId && a.ChannelEntityId == this.SiteId) .FirstOrDefault(); if (interactionChannel == null) { interactionChannel = new InteractionChannel(); interactionChannel.Name = SiteCache.Get(SiteId ?? 1).Name; interactionChannel.ChannelTypeMediumValueId = channelMediumTypeValueId; interactionChannel.ChannelEntityId = this.SiteId; interactionChannel.ComponentEntityTypeId = EntityTypeCache.Get <Rock.Model.Page>().Id; interactionChannelService.Add(interactionChannel); rockContext.SaveChanges(); } InteractionComponent interactionComponent = null; var interactionComponentService = new InteractionComponentService(rockContext); if (PageId.HasValue) { interactionComponent = interactionComponentService.GetComponentByEntityId(interactionChannel.Id, PageId.Value, PageTitle); } else { interactionComponent = interactionComponentService.GetComponentByComponentName(interactionChannel.Id, ComponentName); } rockContext.SaveChanges(); // Add the interaction if (interactionComponent != null) { var deviceId = Regex.Match(UserAgent, "(?<=-).+(?=\\))").Value.Trim(); if (deviceId.Length > 20) { deviceId = deviceId.Substring(0, 20); } var deviceApplication = Regex.Match(UserAgent, "^[\\S]{0,}").Value.Trim() + " " + deviceId; var clientOs = Regex.Match(UserAgent, "(?<=;).+(?=-)").Value.Trim(); var clientType = Regex.Match(UserAgent, "(?<=\\().+(?=;)").Value.Trim(); var deviceType = interactionService.GetInteractionDeviceType(deviceApplication, clientOs, clientType, UserAgent); var interactionSessionService = new InteractionSessionService(rockContext); var interactionSession = interactionSessionService.Queryable().Where(s => s.IpAddress == IPAddress && s.DeviceTypeId == deviceType.Id).FirstOrDefault(); if (interactionSession == null) { interactionSession = new InteractionSession() { DeviceTypeId = deviceType.Id, IpAddress = TrimString(IPAddress, 25) }; interactionSessionService.Add(interactionSession); rockContext.SaveChanges(); } Operation = TrimString(Operation, 25); InteractionSummary = TrimString(InteractionSummary, 500); clientType = TrimString(clientType, 25); deviceApplication = TrimString(deviceApplication, 100); clientOs = TrimString(clientOs, 100); var interaction = new InteractionService(rockContext).AddInteraction(interactionComponent.Id, null, Operation, InteractionData, PersonAliasId, DateViewed, deviceApplication, clientOs, clientType, UserAgent, IPAddress, interactionSession.Guid); interaction.InteractionSummary = InteractionSummary; PersonalDevice personalDevice = AvalancheUtilities.GetPersonalDevice(deviceId, PersonAliasId, rockContext); if (personalDevice != null) { interaction.PersonalDeviceId = personalDevice.Id; } rockContext.SaveChanges(); } } } }