diff --git a/WebPush/WebPushClient.cs b/WebPush/WebPushClient.cs index 58828e1..d4d1174 100755 --- a/WebPush/WebPushClient.cs +++ b/WebPush/WebPushClient.cs @@ -4,6 +4,7 @@ using System.Net.Http; using System.Net.Http.Headers; using System.Runtime.CompilerServices; +using System.Threading; using System.Threading.Tasks; using WebPush.Util; @@ -26,14 +27,14 @@ public class WebPushClient : IDisposable public WebPushClient() { - + } public WebPushClient(HttpClient httpClient) { _httpClient = httpClient; } - + public WebPushClient(HttpClientHandler httpClientHandler) { _httpClientHandler = httpClientHandler; @@ -142,7 +143,7 @@ public HttpRequestMessage GenerateRequestDetails(PushSubscription subscription, if (options != null) { - var validOptionsKeys = new List {"headers", "gcmAPIKey", "vapidDetails", "TTL"}; + var validOptionsKeys = new List { "headers", "gcmAPIKey", "vapidDetails", "TTL" }; foreach (var key in options.Keys) { if (!validOptionsKeys.Contains(key)) @@ -181,7 +182,7 @@ public HttpRequestMessage GenerateRequestDetails(PushSubscription subscription, } //at this stage ttl cannot be null. - timeToLive = (int) ttl; + timeToLive = (int)ttl; } } @@ -258,9 +259,27 @@ public HttpRequestMessage GenerateRequestDetails(PushSubscription subscription, /// public void SendNotification(PushSubscription subscription, string payload = null, Dictionary options = null) + { + SendNotification(subscription, payload, options, CancellationToken.None); + } + + + /// + /// To send a push notification call this method with a subscription, optional payload and any options + /// Will exception if unsuccessful + /// + /// The PushSubscription you wish to send the notification to. + /// The payload you wish to send to the user + /// + /// Options for the GCM API key and vapid keys can be passed in if they are unique for each + /// notification. + /// + /// The cancellation token to cancel operation. + public void SendNotification(PushSubscription subscription, string payload, + Dictionary options, CancellationToken cancellationToken) { var request = GenerateRequestDetails(subscription, payload, options); - var sendAsyncTask = HttpClient.SendAsync(request); + var sendAsyncTask = HttpClient.SendAsync(request, cancellationToken); sendAsyncTask.Wait(); var response = sendAsyncTask.Result; @@ -277,7 +296,7 @@ public void SendNotification(PushSubscription subscription, string payload = nul /// The vapid details for the notification. public void SendNotification(PushSubscription subscription, string payload, VapidDetails vapidDetails) { - var options = new Dictionary {["vapidDetails"] = vapidDetails}; + var options = new Dictionary { ["vapidDetails"] = vapidDetails }; SendNotification(subscription, payload, options); } @@ -290,7 +309,7 @@ public void SendNotification(PushSubscription subscription, string payload, Vapi /// The GCM API key public void SendNotification(PushSubscription subscription, string payload, string gcmApiKey) { - var options = new Dictionary {["gcmAPIKey"] = gcmApiKey}; + var options = new Dictionary { ["gcmAPIKey"] = gcmApiKey }; SendNotification(subscription, payload, options); } @@ -304,11 +323,27 @@ public void SendNotification(PushSubscription subscription, string payload, stri /// Options for the GCM API key and vapid keys can be passed in if they are unique for each /// notification. /// - public async Task SendNotificationAsync(PushSubscription subscription, string payload = null, + public Task SendNotificationAsync(PushSubscription subscription, string payload = null, Dictionary options = null) + { + return SendNotificationAsync(subscription, payload, options, CancellationToken.None); + } + + /// To send a push notification asynchronous call this method with a subscription, optional payload and any options + /// Will exception if unsuccessful + /// + /// The PushSubscription you wish to send the notification to. + /// The payload you wish to send to the user + /// + /// Options for the GCM API key and vapid keys can be passed in if they are unique for each + /// notification. + /// + /// The cancellation token to cancel operation. + public async Task SendNotificationAsync(PushSubscription subscription, string payload, + Dictionary options, CancellationToken cancellationToken) { var request = GenerateRequestDetails(subscription, payload, options); - var response = await HttpClient.SendAsync(request); + var response = await HttpClient.SendAsync(request, cancellationToken); HandleResponse(response, subscription); } @@ -323,7 +358,7 @@ public async Task SendNotificationAsync(PushSubscription subscription, string pa public async Task SendNotificationAsync(PushSubscription subscription, string payload, VapidDetails vapidDetails) { - var options = new Dictionary {["vapidDetails"] = vapidDetails}; + var options = new Dictionary { ["vapidDetails"] = vapidDetails }; await SendNotificationAsync(subscription, payload, options); } @@ -336,7 +371,7 @@ public async Task SendNotificationAsync(PushSubscription subscription, string pa /// The GCM API key public async Task SendNotificationAsync(PushSubscription subscription, string payload, string gcmApiKey) { - var options = new Dictionary {["gcmAPIKey"] = gcmApiKey}; + var options = new Dictionary { ["gcmAPIKey"] = gcmApiKey }; await SendNotificationAsync(subscription, payload, options); } @@ -354,7 +389,7 @@ private static void HandleResponse(HttpResponseMessage response, PushSubscriptio } // Error - var message = @"Received unexpected response code: " + (int) response.StatusCode; + var message = @"Received unexpected response code: " + (int)response.StatusCode; switch (response.StatusCode) { case HttpStatusCode.BadRequest: @@ -365,7 +400,7 @@ private static void HandleResponse(HttpResponseMessage response, PushSubscriptio message = "Payload too large"; break; - case (HttpStatusCode) 429: + case (HttpStatusCode)429: message = "Too many request."; break; @@ -377,7 +412,7 @@ private static void HandleResponse(HttpResponseMessage response, PushSubscriptio throw new WebPushException(message, response.StatusCode, response.Headers, subscription); } - + public void Dispose() { if (_httpClient != null && _isHttpClientInternallyCreated)