Exemplo n.º 1
0
        public async Task PushAsync(string payload, PushOptions options)
        {
            var webPushOptions = WebPushOptions.Defaults;

            if (null != options && options.TimeToLive.HasValue)
            {
                webPushOptions.TimeToLive = options.TimeToLive.Value;
            }
            HttpResponseMessage res;

            if (null != payload)
            {
                res = await webPushClient.SendNotificationAsync(pushSubscription, payload, webPushOptions);
            }
            else
            {
                res = await webPushClient.SendNotificationAsync(pushSubscription, webPushOptions);
            }
            if (!res.IsSuccessStatusCode)
            {
                throw new PushException($"Attempted delivery resulted in {res.StatusCode}.", res);
            }
        }
Exemplo n.º 2
0
        public async Task SendNotificationToUserAsync(NotificationDto notification)
        {
            PushSubscription sub = await GetPushSubsciption(notification.SubscriptionId);

            VapidDetails vapiData = GetVapiData();
            string       message  = GetMessage(notification);

            try
            {
                await _webPushClient.SendNotificationAsync(sub, message, vapiData);
            }
            catch (WebPushException exception)
            {
                if (exception.Message == "Subscription no longer valid")
                {
                    await DeactivateSubscription(notification.SubscriptionId);
                }
                throw;
            }
        }