Beispiel #1
0
        /// <summary>
        /// Asynchronously sends a notification to the queue.
        /// </summary>
        /// <param name="account">The account to send the notification to.</param>
        /// <param name="notification">A notification to send.</param>
        /// <returns>The task.</returns>
        public Task SendAsync <TAccountKey>(NotificationAccount <TAccountKey> account, Notification notification)
        {
            Throw.IfArgumentNull(account, nameof(account));
            Throw.IfArgumentNull(notification, nameof(notification));

            INotificationChannel channel = this.channelFactory.GetChannel(notification);

            return(channel.SendAsync(account, notification));
        }
Beispiel #2
0
        /// <summary>
        /// Registers a new mobile device with the specified account.
        /// </summary>
        /// <param name="accountId">The id of the account.</param>
        /// <param name="mobileDevice">A mobile device to register.</param>
        public void RegisterMobileDevice(TKey accountId, MobileDevice mobileDevice)
        {
            Throw.IfArgumentNull(mobileDevice, nameof(mobileDevice));

            NotificationAccount <TKey> account = this.GetAccount(accountId);

            account.AddMobileDevice(mobileDevice);

            this.repository.UpdateAccount(account);
        }
Beispiel #3
0
        /// <summary>
        /// Gets the account with the specified id.
        /// </summary>
        /// <param name="accountId">The account identifier.</param>
        /// <returns>A notification account.</returns>
        public NotificationAccount <TKey> GetAccount(TKey accountId)
        {
            NotificationAccount <TKey> account = this.repository.GetAccount(accountId);

            if (account == null)
            {
                throw new NotificationAccountNotFoundException(accountId);
            }

            return(account);
        }
Beispiel #4
0
 /// <summary>
 /// Sends a notification to the specified account.
 /// </summary>
 /// <param name="account">An account to send the notification to.</param>
 /// <param name="notification">A notification to send.</param>
 /// <returns>The task for the operation.</returns>
 public async Task SendAsync <TAccountKey>(NotificationAccount <TAccountKey> account, Notification notification)
 {
     try
     {
         await this.DoSendAsync(account, (TNotification)notification).ConfigureAwait(false);
     }
     catch (ArgumentException ex)
     {
         this.OnError(account.AccountId.ToString(), notification, ex);
     }
 }
Beispiel #5
0
        /// <summary>
        /// Attempts to schedule a notification for delivery.
        /// </summary>
        /// <param name="accountId">The id of the account the notification will be sent to.</param>
        /// <param name="notification">A notification to schedule.</param>
        /// <param name="deliveryDateTime">The date and time the notification should be delivered.</param>
        /// <returns>True if the notification was scheduled or false if an account to send the notification to does not exist.</returns>
        public bool TrySchedule(TKey accountId, Notification notification, DateTime deliveryDateTime)
        {
            NotificationAccount <TKey> account = this.accountManager.TryGetAccount(accountId);

            if (account != null)
            {
                this.Schedule(account, notification, deliveryDateTime);
                return(true);
            }

            return(false);
        }
Beispiel #6
0
        private bool TryUpdateAccount(TKey accountId, Action <NotificationAccount <TKey> > modifyAccount)
        {
            NotificationAccount <TKey> account = this.repository.GetAccount(accountId);

            if (account != null)
            {
                modifyAccount(account);
                this.repository.UpdateAccount(account);
                return(true);
            }

            return(false);
        }
Beispiel #7
0
        private async Task SendScheduledNotifications(IEnumerable <ScheduledNotification <TKey> > scheduledNotificationsToSend)
        {
            foreach (ScheduledNotification <TKey> scheduledNotification in scheduledNotificationsToSend)
            {
                NotificationAccount <TKey> account = this.accountManager.TryGetAccount(scheduledNotification.AccountId);

                if (account != null)
                {
                    await this.notificationQueue.SendAsync(account, scheduledNotification.Notification).ConfigureAwait(false);
                }
                else
                {
                    this.eventPublisher.Publish(
                        new NotificationSendFailure(
                            scheduledNotification.Notification,
                            NotificationErrorCode.AccountNotFound,
                            scheduledNotification.AccountId.ToString(),
                            "An account could not be found for the scheduled notification."));
                }

                this.repository.DeleteScheduledNotification(scheduledNotification.Notification.Id);
            }
        }
Beispiel #8
0
        /// <summary>
        /// Schedules a notification for delivery.
        /// </summary>
        /// <param name="account">The account the notification will be sent to.</param>
        /// <param name="notification">A notification to schedule.</param>
        /// <param name="deliveryDateTime">The date and time the notification should be delivered.</param>
        public void Schedule(NotificationAccount <TKey> account, Notification notification, DateTime deliveryDateTime)
        {
            Throw.IfArgumentNull(account, nameof(account));
            Throw.IfArgumentNull(notification, nameof(notification));

            string timeZone =
                string.IsNullOrWhiteSpace(account.TimeZone)
                ? Constants.EasternTimeZone
                : account.TimeZone;

            ScheduledNotification <TKey> scheduledNotification =
                new ScheduledNotification <TKey>(
                    account.AccountId,
                    notification,
                    deliveryDateTime.ToDateTimeOffset(timeZone));

            this.repository.SaveScheduledNotification(scheduledNotification);

            this.eventPublisher.Publish(
                new NotificationScheduled(
                    scheduledNotification.AccountId.ToString(),
                    scheduledNotification.Notification,
                    scheduledNotification.DeliveryDateTime));
        }
Beispiel #9
0
 /// <summary>
 /// Updates an existing notificaitons account.
 /// </summary>
 /// <param name="account">An account to update.</param>
 public void UpdateAccount(NotificationAccount <TKey> account)
 {
     Throw.IfArgumentNull(account, nameof(account));
     this.repository.UpdateAccount(account);
 }
Beispiel #10
0
        /// <summary>
        /// Enables push notifications for an account.
        /// </summary>
        /// <param name="accountId">The id of the account who is using the device.</param>
        /// <param name="deviceId">The device to enable notifications for.</param>
        /// <param name="pushNotificationToken">A token associated with the device to receive notifications.</param>
        public void EnablePushNotifications(TKey accountId, string deviceId, string pushNotificationToken)
        {
            NotificationAccount <TKey> account = this.GetAccount(accountId);

            account.EnablePushNotifications(deviceId, pushNotificationToken);
        }
Beispiel #11
0
 /// <summary>
 /// Sends a notification to the specified account.
 /// </summary>
 /// <param name="account">An account to send the notification to.</param>
 /// <param name="notification">A notification to send.</param>
 /// <returns>The task for the operation.</returns>
 protected abstract Task DoSendAsync <TAccountKey>(NotificationAccount <TAccountKey> account, TNotification notification);