public async Task SendNotificationAsync(PushSubscriptionUser subscription, WebPushNotificationMessages message, CancellationToken cancellationToken)
        {
            var vapidDetails = new VapidDetails(this._options.Subject, this._options.PublicKey, this._options.PrivateKey);
            PushSubscription pushSubscription = new WebPush.PushSubscription(subscription.Endpoint, subscription.P256DH, subscription.Auth);
            //var payload = System.Text.Json.JsonSerializer.Serialize(message);
            var settings = new JsonSerializerSettings();

            settings.ContractResolver = new DefaultContractResolver {
                NamingStrategy = new JsonLowerCaseNamingStrategy()
            };;
            var payload = JsonConvert.SerializeObject(message, settings);

            try
            {
                await _pushClient.SendNotificationAsync(pushSubscription, payload, vapidDetails);
            }
            catch (WebPushException ex)
            {
                {
                    using (IStoreRepositoryAccessor pushstoreAccessor = _pushStoreAccessorProvider.GetStoreRepositoryAccessor())
                    {
                        subscription.DateEndTime = DateTime.Now;
                        await pushstoreAccessor.StoreRepository.UpdateSubscriptionAsync(subscription);
                    }
                    _logger?.LogInformation("Subscription has expired or is no longer valid and has been removed.");
                }

                Console.Error.WriteLine("(WebPush) Error sending push notification: " + ex.Message);
            }
        }
示例#2
0
        public async Task <WebPushNotificationMessages> Enqueue(WebPushNotificationMessages message)
        {
            if (message == null)
            {
                throw new ArgumentNullException(nameof(message));
            }

            _messages.Enqueue(message);

            _messageEnqueuedSignal.Release();

            return(message);
        }
        private async Task DequeueMessagesAsync()
        {
            while (!_stopTokenSource.IsCancellationRequested)
            {
                WebPushNotificationMessages message = await _messagesQueue.DequeueAsync(_stopTokenSource.Token);

                if (!_stopTokenSource.IsCancellationRequested)
                {
                    using (IStoreRepositoryAccessor pushStoreAccessor = _pushStoreAccessorProvider.GetStoreRepositoryAccessor())
                    {
                        await pushStoreAccessor.StoreRepository.ForEachSubscriptionAsync((PushSubscriptionUser subscription) =>
                        {
                            // Fire-and-forget
                            _pushService.SendNotificationAsync(subscription, message, _stopTokenSource.Token);
                        },
                                                                                         _stopTokenSource.Token);
                    }
                }
            }
        }
 public Task SendNotificationAsync(PushSubscriptionUser subscription, WebPushNotificationMessages message)
 {
     return(SendNotificationAsync(subscription, message, CancellationToken.None));
 }