public async Task <bool> NotifyUser(string userId, string title, string body, string target, string image, NotificationOptions notificationType)
        {
            _logger.LogDebug($"Sending email messages to {userId} - {target}");
            using var scope = _provider.CreateScope();

            // this is called from a rabbitmq service so we're on a different scope
            // DI has to be manually constucted
            var subscriptionStore = scope.ServiceProvider.GetRequiredService <IPushSubscriptionStore>();

            await _sendEmail(userId, title, body, target, image, notificationType);

            var pushMessage = new PushMessage(body)
            {
                Topic   = title,
                Urgency = PushMessageUrgency.Normal
            };

            try {
                //TODO: Adding PushSubscriptionContext as Singleton is a recipe for disaster
                //TODO: but this gets f****d if I don't
                _logger.LogDebug($"Sending GCM messages to {userId}");
                _logger.LogDebug($"Using store {subscriptionStore.GetType()}");
                await subscriptionStore.ForEachSubscriptionAsync(userId,
                                                                 (WP.PushSubscription subscription) => {
                    _logger.LogInformation($"Sending to {target}");
                    _notificationService.SendNotificationAsync(subscription, pushMessage, target);
                });

                return(true);
            } catch (Exception ex) {
                _logger.LogError(ex.Message);
            }
            return(false);
        }
Esempio n. 2
0
        public async Task <bool> InitiateSupportRequest(ChatViewModel message)
        {
            if (!string.IsNullOrEmpty(_chatSettings.CurrentChatUser))
            {
                var user = await _userManager.FindByEmailAsync(_chatSettings.CurrentChatUser);

                if (!string.IsNullOrEmpty(user?.Id))
                {
                    //send firebase message to notify via web worker
                    var pushMessage = new PushMessage(message.Message)
                    {
                        Topic   = "New support chat message",
                        Urgency = PushMessageUrgency.Normal
                    };
                    await _subscriptionStore.ForEachSubscriptionAsync(user.Id, (PushSubscription subscription) => {
                        _notificationService.SendNotificationAsync(subscription, pushMessage, string.Empty);
                    });

                    // await _hub.SendUserAsync(user.Id, "support-message", new object[] { message });
                    //send slack message
                    var slackResult = await _slackSupport.NotifyUser(message);

                    return(true);
                }
            }
            return(false);
        }
        private async Task DequeueMessagesAsync()
        {
            while (!_stopTokenSource.IsCancellationRequested)
            {
                PushMessageEx pushmessage = await _messagesQueue.DequeueAsync(_stopTokenSource.Token);

                if (!_stopTokenSource.IsCancellationRequested)
                {
                    using (IServiceScope serviceScope = _serviceProvider.CreateScope())
                    {
                        IPushSubscriptionStore subscriptionStore = serviceScope.ServiceProvider.GetRequiredService <IPushSubscriptionStore>();

                        //await subscriptionStore.ForEachSubscriptionAsync((PushSubscription subscription) =>
                        //{
                        //    // Fire-and-forget
                        //    _notificationService.SendNotificationAsync(subscription, message, _stopTokenSource.Token);
                        //}, _stopTokenSource.Token);

                        await subscriptionStore.ForEachSubscriptionAsyncEx((SWSubscription sub) =>
                        {
                            PushSubscription subscription = new PushSubscription();
                            subscription.Endpoint         = sub.Endpoint;
                            subscription.SetKey(PushEncryptionKeyName.Auth, sub.Auth);
                            subscription.SetKey(PushEncryptionKeyName.P256DH, sub.P256DH);
                            // subscription.Keys
                            // Fire-and-forget
                            _notificationService.SendNotificationAsync(subscription, pushmessage.pushmessage, _stopTokenSource.Token);
                        }, _stopTokenSource.Token);
                    }
                }
            }
        }
        private async Task DequeueMessagesAsync()
        {
            while (!_stopTokenSource.IsCancellationRequested)
            {
                try
                {
                    PushQueueItem queueItem = await _messagesQueue.DequeueAsync(_stopTokenSource.Token);

                    if (!_stopTokenSource.IsCancellationRequested)
                    {
                        var recipientProvider = GetRecipientProvider(queueItem.RecipientProviderName);
                        if (recipientProvider != null)
                        {
                            var subscriptions = await recipientProvider.GetRecipients(queueItem, _stopTokenSource.Token);

                            foreach (var subscription in subscriptions)
                            {
                                var pushMessage = FromModel(queueItem.Message);
                                await _notificationService.SendNotificationAsync(subscription, pushMessage, _stopTokenSource.Token);
                            }
                        }
                        else
                        {
                            _log.LogWarning($"failed to send notification because IPushNotificationRecipientProvider with name {queueItem.RecipientProviderName} was not found");
                        }
                    }
                }
                catch (Exception ex)
                {
                    _log.LogError($"{ex.Message}:{ex.StackTrace}");
                }
            }
        }
Esempio n. 5
0
        public async Task Run([TimerTrigger("0 * * * * *")] TimerInfo myTimer, ILogger log)
        {
            var todoItems = await todoItemFacade.GetByNotificationTimeAsync(dateTimeProvider.Now);

            foreach (var todoItem in todoItems)
            {
                log.LogInformation($"Sending notification for item: {todoItem.Title}");
                await pushNotificationService.SendNotificationAsync(todoItem);
            }
        }
Esempio n. 6
0
        /// <summary>
        /// invio delle notifiche
        /// </summary>
        /// <returns></returns>
        private async Task DequeueMessagesAsync()
        {
            while (!_stopTokenSource.IsCancellationRequested)
            {
                //recupero del primo messaggio dalla coda
                PushMessageContract messageContract = await _messagesQueue.DequeueAsync(_stopTokenSource.Token);

                if (!_stopTokenSource.IsCancellationRequested)
                {
                    //invio notifica
                    await _notificationService.SendNotificationAsync(messageContract.Subscription, messageContract.NotificationMessage, _stopTokenSource.Token);
                }
            }
        }
Esempio n. 7
0
        public async Task <string> ServerPush(string message)
        {
            var response    = new StringBuilder();
            var pushMessage = new WP.PushMessage(message)
            {
                Topic   = "Debug",
                Urgency = WP.PushMessageUrgency.Normal
            };
            await _subscriptionStore.ForEachSubscriptionAsync(_applicationUser.Id, (subscription) => {
                _notificationService.SendNotificationAsync(subscription, pushMessage, "http://fergl.ie");
                response.Append($"Sent: {subscription.Endpoint}");
            });

            return(response.ToString());
        }
Esempio n. 8
0
        public async Task <IActionResult> SendNotification([FromBody] PushMessageViewModel message)
        {
            PushMessage pushMessage = new PushMessage(message.Notification)
            {
                Topic   = message.Topic,
                Urgency = message.Urgency
            };

            // TODO: This should be scheduled in background
            await _subscriptionStore.ForEachSubscriptionAsync((PushSubscription subscription) =>
            {
                // Fire-and-forget
                _notificationService.SendNotificationAsync(subscription, pushMessage);
            });

            return(NoContent());
        }
        public async Task <IActionResult> SendNotification([FromBody] PushMessageViewModel message)
        {
            _logger.LogInformation($"Sending targeted push for: {message.Target} - {message.Notification}");
            var pushMessage = new WP.PushMessage(message.Notification)
            {
                Topic   = message.Topic,
                Urgency = message.Urgency
            };

            // TODO: This should be scheduled in background
            await _subscriptionStore.ForEachSubscriptionAsync(message.Target, (WP.PushSubscription subscription) => {
                _logger.LogInformation($"Found subscription: {subscription}");
                _notificationService.SendNotificationAsync(subscription, pushMessage, message.Target);
            });

            return(NoContent());
        }
Esempio n. 10
0
        public async Task <ActionResult <ChatViewModel> > Initialise([FromBody] ChatViewModel message)
        {
            var chatUser = await _userManager.FindByEmailAsync(_chatSettings.CurrentChatUser);

            if (chatUser != null)
            {
                var chat = _mapper.Map <ChatMessage>(message);
                chat.FromUser = _applicationUser;
                chat.ToUser   = chatUser;
                if (chat.FromUser != null)
                {
                    _chatRepository.AddOrUpdate(chat);
                    await _unitOfWork.CompleteAsync();
                }
                else
                {
                    // it's an anonymous chat, we don't need to save item
                    chat.Id = System.Guid.NewGuid();
                }

                var filledMessage = _mapper.Map <ChatViewModel>(chat);
                filledMessage.FromUserName = _applicationUser == null ?
                                             message.FromUserName :
                                             _applicationUser.GetBestGuessName();

                //send push message to whoever is registered as admin
                await _subscriptionStore.ForEachSubscriptionAsync(chatUser.Id, (PushSubscription subscription) => {
                    _notificationService.SendNotificationAsync(
                        subscription,
                        new PushMessage("New SUPPORT Request")
                    {
                        Urgency = PushMessageUrgency.High,
                        Topic   = "NewSupport"
                    },
                        _appSettings.SiteUrl);
                });

                if (await _supportChatService.InitiateSupportRequest(filledMessage))
                {
                    await _hub.SendUserAsync(filledMessage.ToUserId, "support-message", new object[] { filledMessage });

                    return(Ok(filledMessage));
                }
            }
            return(StatusCode(503));
        }
        private async Task DequeueMessagesAsync()
        {
            while (!_stopTokenSource.IsCancellationRequested)
            {
                PushMessage message = await _messagesQueue.DequeueAsync(_stopTokenSource.Token);

                if (!_stopTokenSource.IsCancellationRequested)
                {
                    using (IPushSubscriptionStoreAccessor subscriptionStoreAccessor = _subscriptionStoreAccessorProvider.GetPushSubscriptionStoreAccessor())
                    {
                        await subscriptionStoreAccessor.PushSubscriptionStore.ForEachSubscriptionAsync((PushSubscription subscription) =>
                        {
                            // Fire-and-forget
                            _notificationService.SendNotificationAsync(subscription, message, _stopTokenSource.Token);
                        }, _stopTokenSource.Token);
                    }
                }
            }
        }
Esempio n. 12
0
        private async Task DequeueMessagesAsync()
        {
            while (!_stopTokenSource.IsCancellationRequested)
            {
                PushMessage message = await _messagesQueue.DequeueAsync(_stopTokenSource.Token);

                if (!_stopTokenSource.IsCancellationRequested)
                {
                    using (IServiceScope serviceScope = _serviceProvider.CreateScope())
                    {
                        IPushSubscriptionStore subscriptionStore = serviceScope.ServiceProvider.GetRequiredService <IPushSubscriptionStore>();

                        await subscriptionStore.ForEachSubscriptionAsync((PushSubscription subscription) =>
                        {
                            // Fire-and-forget
                            _notificationService.SendNotificationAsync(subscription, message, _stopTokenSource.Token);
                        }, _stopTokenSource.Token);
                    }
                }
            }
        }
Esempio n. 13
0
        private async Task DequeueMessagesAsync()
        {
            while (!_stopTokenSource.IsCancellationRequested)
            {
                PushMessage message = await _messagesQueue.DequeueAsync(_stopTokenSource.Token);

                if (!_stopTokenSource.IsCancellationRequested)
                {
                    using (IPushSubscriptionStoreAccessor subscriptionStoreAccessor = _subscriptionStoreAccessorProvider.GetPushSubscriptionStoreAccessor())
                    {
                        await subscriptionStoreAccessor.PushSubscriptionStore.ForEachSubscriptionAsync((PushSubscription subscription) =>
                        {
                            Console.WriteLine("Sending to subscription right about now: " + subscription.Endpoint);
                            Task.Delay(1000).Wait();
                            // Fire-and-forget
                            _notificationService.SendNotificationAsync(subscription, message, _stopTokenSource.Token);
                        }, _stopTokenSource.Token);
                    }
                }
            }
        }