public void SendNotification()
        {
            var name = Context.User.Identity.Name;

            if (string.IsNullOrEmpty(name))
            {
                return;
            }

            //
            // Azure table storage repositories
            //
            var connRepository = new ConnectionsRepository();
            var notifyRepo     = new NotificationsRepository();

            if (!notifyRepo.Init())
            {
                return;
            }
            if (!connRepository.Init())
            {
                return;
            }

            var connections   = connRepository.GetConnectionsByUserId(name).ToList();
            var notifications = notifyRepo.GetNotificationsByUserId(name).ToList();

            notifications.Sort();

            if (connections.Count > 0 && notifications.Count > 0)
            {
                // Send notification(s) to each user connection.
                foreach (var connection in connections)
                {
                    foreach (var notification in notifications)
                    {
                        string json = System.Web.Helpers.Json.Encode(new
                        {
                            title          = notification.Title,
                            message        = notification.Message,
                            url            = string.IsNullOrEmpty(notification.Url) ? string.Empty : notification.Url,
                            linkText       = string.IsNullOrEmpty(notification.LinkText) ? string.Empty : notification.LinkText,
                            type           = (int)notification.NotifyTypeInt,
                            notificationId = notification.RowKey
                        });

                        // Calls method on client side to show notification
                        Clients.Client(connection.RowKey).addNotification(json);
                    }
                }
            }
        }
        public void GetNotificationsCount()
        {
            var name = Context.User.Identity.Name;

            if (string.IsNullOrEmpty(name))
            {
                return;
            }
            var notifyRepo = new NotificationsRepository();

            if (!notifyRepo.Init())
            {
                return;
            }
            var notifications = notifyRepo.GetNotificationsByUserId(name);

            Clients.Caller.updateNotificationsCount(notifications.Count());
        }
Example #3
0
        public static void NotifyOfMembersSpecialDates(string userEmail)
        {
            using (var unitOfWork = new UnitOfWork())
            {
                var calService       = new CalendarService(unitOfWork);
                var specialOccasions = calService.GetEventsForDateRange(DateTime.Now, DateTime.Now.AddDays(2), userEmail);

                if (specialOccasions == null || specialOccasions.Count() == 0)
                {
                    return;
                }

                // Repository for sent notifications
                var notifyRepo        = new NotificationsRepository(sentNotifications: true);
                var sentNotifications = notifyRepo.GetNotificationsByUserId(userEmail).ToList();

                string message = string.Empty;
                foreach (var specialOccasion in specialOccasions)
                {
                    message = $"{specialOccasion.Description} is on {specialOccasion.DateString}";

                    // To prevent user getting the same special occasion notifications multiple times
                    if (!HasNotificationBeenSent(sentNotifications, specialOccasion.Title, message))
                    {
                        SendUserNotification(userEmail,
                                             specialOccasion.Title,
                                             message,
                                             specialOccasion.Url,
                                             specialOccasion.Title, // link text
                                             type: NotificationType.Info);

                        AddSentNotification(userEmail, specialOccasion.Title, message);
                    }
                }

                // Get rid of old sent notifications after a set time span
                RemoveOldSentNotifications(userEmail, sentNotifications, notifyRepo);
            }
        }