private static Action <CancellationToken> CreateTicketEventNotifications(
            IEnumerable <TicketEventNotification> notifications)
        {
            return(ct =>
            {
                // ReSharper disable once EmptyGeneralCatchClause
                try
                {
                    var notificationIds = notifications.Select(n => n.EventId).ToArray();
                    var domainContext = DependencyResolver.Current.GetService <TdDomainContext>();
                    var multiProject = domainContext.Projects.Count() > 1;
                    //fetch these back and make sure all dependent entities we need are loaded
                    var notes = domainContext.TicketEventNotifications
                                .Include(t => t.TicketEvent)
                                .Include(t => t.TicketEvent.Ticket)
                                .Include(t => t.TicketEvent.Ticket.Project)
                                .Include(t => t.TicketSubscriber)
                                .Where(t => notificationIds.Contains(t.EventId))
                                .ToArray();

                    if (notes.Any())
                    {
                        using (var noteContext = new TdPushNotificationContext())
                        {
                            var subscriberExclude =
                                noteContext.TicketDeskPushNotificationSettings.AntiNoiseSettings
                                .ExcludeSubscriberEvents;

                            var noteEvents = notes.ToNotificationEventInfoCollection(subscriberExclude,
                                                                                     multiProject);

                            noteContext.AddNotifications(noteEvents);

                            noteContext.SaveChanges();
                        }
                    }
                }
                catch
                {
                    //TODO: Log this somewhere
                }
            });
        }
        private static Action <CancellationToken> CreateNewTicketBroadcastNotification(IEnumerable <Ticket> tickets)
        {
            return(ct =>
            {
                try
                {
                    var notificationIds = tickets.Select(t =>
                                                         t.TicketEvents.First(te => te.ForActivity == TicketActivity.Create ||
                                                                              te.ForActivity == TicketActivity.CreateOnBehalfOf).EventId)
                                          .ToArray();

                    var domainContext = DependencyResolver.Current.GetService <TdDomainContext>();
                    var multiProject = domainContext.Projects.Count() > 1;
                    var notes = domainContext.TicketEventNotifications
                                .Include(t => t.TicketEvent)
                                .Include(t => t.TicketEvent.Ticket)
                                .Include(t => t.TicketEvent.Ticket.Project)
                                .Where(t => notificationIds.Contains(t.EventId))
                                .ToArray();

                    if (notes.Any())
                    {
                        using (var noteContext = new TdPushNotificationContext())
                        {
                            var newNoteEvents = notes.ToNewTicketPushNotificationInfoCollection(multiProject);
                            noteContext.AddNotifications(newNoteEvents);

                            noteContext.SaveChanges();
                        }
                    }
                }
                catch
                {
                    //TODO: Log this somewhere
                }
            });
        }
        /// <summary>
        /// Configures the push notifications.
        /// </summary>
        public static void ConfigurePushNotifications()
        {
            var demoMode = ConfigurationManager.AppSettings["ticketdesk:DemoModeEnabled"] ?? "false";

            if (!DatabaseConfig.IsDatabaseReady || demoMode.Equals("true", StringComparison.InvariantCultureIgnoreCase))
            {
                //disable if database hasn't been created, of if running in demo mode
                return;
            }

            //configuration supplied by the IoC configuration
            var context = DependencyResolver.Current.GetService <TdPushNotificationContext>();

            if (DatabaseConfig.IsFirstRunDemoRefreshEnabled())
            {
                DemoPushNotificationDataManager.SetupDemoPushNotificationData(context);
            }

            if (context.TicketDeskPushNotificationSettings.IsEnabled)
            {
                //TODO: poor man's detection of appropriate scheduler
                var siteName       = Environment.GetEnvironmentVariable("WEBSITE_SITE_NAME");
                var isAzureWebSite = !string.IsNullOrEmpty(siteName);
                if (!isAzureWebSite)
                {
                    InProcessPushNotificationScheduler.Start(context.TicketDeskPushNotificationSettings.DeliveryIntervalMinutes);
                }
                context.Dispose();//ensure that no one accidentally holds a reference to this in closure

                //register for static notifications created event handler
                TdDomainContext.NotificationsCreated += (sender, notifications) =>
                {
                    HostingEnvironment.QueueBackgroundWorkItem(ct =>
                    {
                        // ReSharper disable once EmptyGeneralCatchClause
                        try
                        {
                            var notificationIds = notifications.Select(n => n.EventId).ToArray();
                            var domainContext   = DependencyResolver.Current.GetService <TdDomainContext>();
                            var multiProject    = domainContext.Projects.Count() > 1;
                            //fetch these back and make sure all dependent entities we need are loaded
                            var notes = domainContext.TicketEventNotifications
                                        .Include(t => t.TicketEvent)
                                        .Include(t => t.TicketEvent.Ticket)
                                        .Include(t => t.TicketEvent.Ticket.Project)
                                        .Include(t => t.TicketSubscriber)
                                        .Where(t => notificationIds.Contains(t.EventId))
                                        .ToArray();

                            if (notes.Any())
                            {
                                using (var noteContext = new TdPushNotificationContext())
                                {
                                    var subscriberExclude =
                                        noteContext.TicketDeskPushNotificationSettings.AntiNoiseSettings
                                        .ExcludeSubscriberEvents;

                                    var noteEvents = notes.ToNotificationEventInfoCollection(subscriberExclude,
                                                                                             multiProject);

                                    noteContext.AddNotifications(noteEvents);

                                    noteContext.SaveChanges();
                                }
                            }
                        }
                        catch
                        {
                            //TODO: Log this somewhere
                        }
                    });
                };
            }
        }