Skip to content

Commit 6bf6385

Browse files
Merge pull request #64 from divan-9/cancellationToken
Add cancellation token to SendNotification* methods
2 parents ed82215 + 14692a0 commit 6bf6385

File tree

1 file changed

+49
-14
lines changed

1 file changed

+49
-14
lines changed

WebPush/WebPushClient.cs

+49-14
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Net.Http;
55
using System.Net.Http.Headers;
66
using System.Runtime.CompilerServices;
7+
using System.Threading;
78
using System.Threading.Tasks;
89
using WebPush.Util;
910

@@ -26,14 +27,14 @@ public class WebPushClient : IDisposable
2627

2728
public WebPushClient()
2829
{
29-
30+
3031
}
3132

3233
public WebPushClient(HttpClient httpClient)
3334
{
3435
_httpClient = httpClient;
3536
}
36-
37+
3738
public WebPushClient(HttpClientHandler httpClientHandler)
3839
{
3940
_httpClientHandler = httpClientHandler;
@@ -142,7 +143,7 @@ public HttpRequestMessage GenerateRequestDetails(PushSubscription subscription,
142143

143144
if (options != null)
144145
{
145-
var validOptionsKeys = new List<string> {"headers", "gcmAPIKey", "vapidDetails", "TTL"};
146+
var validOptionsKeys = new List<string> { "headers", "gcmAPIKey", "vapidDetails", "TTL" };
146147
foreach (var key in options.Keys)
147148
{
148149
if (!validOptionsKeys.Contains(key))
@@ -181,7 +182,7 @@ public HttpRequestMessage GenerateRequestDetails(PushSubscription subscription,
181182
}
182183

183184
//at this stage ttl cannot be null.
184-
timeToLive = (int) ttl;
185+
timeToLive = (int)ttl;
185186
}
186187
}
187188

@@ -258,9 +259,27 @@ public HttpRequestMessage GenerateRequestDetails(PushSubscription subscription,
258259
/// </param>
259260
public void SendNotification(PushSubscription subscription, string payload = null,
260261
Dictionary<string, object> options = null)
262+
{
263+
SendNotification(subscription, payload, options, CancellationToken.None);
264+
}
265+
266+
267+
/// <summary>
268+
/// To send a push notification call this method with a subscription, optional payload and any options
269+
/// Will exception if unsuccessful
270+
/// </summary>
271+
/// <param name="subscription">The PushSubscription you wish to send the notification to.</param>
272+
/// <param name="payload">The payload you wish to send to the user</param>
273+
/// <param name="options">
274+
/// Options for the GCM API key and vapid keys can be passed in if they are unique for each
275+
/// notification.
276+
/// </param>
277+
/// <param name="cancellationToken">The cancellation token to cancel operation.</param>
278+
public void SendNotification(PushSubscription subscription, string payload,
279+
Dictionary<string, object> options, CancellationToken cancellationToken)
261280
{
262281
var request = GenerateRequestDetails(subscription, payload, options);
263-
var sendAsyncTask = HttpClient.SendAsync(request);
282+
var sendAsyncTask = HttpClient.SendAsync(request, cancellationToken);
264283
sendAsyncTask.Wait();
265284

266285
var response = sendAsyncTask.Result;
@@ -277,7 +296,7 @@ public void SendNotification(PushSubscription subscription, string payload = nul
277296
/// <param name="vapidDetails">The vapid details for the notification.</param>
278297
public void SendNotification(PushSubscription subscription, string payload, VapidDetails vapidDetails)
279298
{
280-
var options = new Dictionary<string, object> {["vapidDetails"] = vapidDetails};
299+
var options = new Dictionary<string, object> { ["vapidDetails"] = vapidDetails };
281300
SendNotification(subscription, payload, options);
282301
}
283302

@@ -290,7 +309,7 @@ public void SendNotification(PushSubscription subscription, string payload, Vapi
290309
/// <param name="gcmApiKey">The GCM API key</param>
291310
public void SendNotification(PushSubscription subscription, string payload, string gcmApiKey)
292311
{
293-
var options = new Dictionary<string, object> {["gcmAPIKey"] = gcmApiKey};
312+
var options = new Dictionary<string, object> { ["gcmAPIKey"] = gcmApiKey };
294313
SendNotification(subscription, payload, options);
295314
}
296315

@@ -304,11 +323,27 @@ public void SendNotification(PushSubscription subscription, string payload, stri
304323
/// Options for the GCM API key and vapid keys can be passed in if they are unique for each
305324
/// notification.
306325
/// </param>
307-
public async Task SendNotificationAsync(PushSubscription subscription, string payload = null,
326+
public Task SendNotificationAsync(PushSubscription subscription, string payload = null,
308327
Dictionary<string, object> options = null)
328+
{
329+
return SendNotificationAsync(subscription, payload, options, CancellationToken.None);
330+
}
331+
332+
/// To send a push notification asynchronous call this method with a subscription, optional payload and any options
333+
/// Will exception if unsuccessful
334+
/// </summary>
335+
/// <param name="subscription">The PushSubscription you wish to send the notification to.</param>
336+
/// <param name="payload">The payload you wish to send to the user</param>
337+
/// <param name="options">
338+
/// Options for the GCM API key and vapid keys can be passed in if they are unique for each
339+
/// notification.
340+
/// </param>
341+
/// <param name="cancellationToken">The cancellation token to cancel operation.</param>
342+
public async Task SendNotificationAsync(PushSubscription subscription, string payload,
343+
Dictionary<string, object> options, CancellationToken cancellationToken)
309344
{
310345
var request = GenerateRequestDetails(subscription, payload, options);
311-
var response = await HttpClient.SendAsync(request);
346+
var response = await HttpClient.SendAsync(request, cancellationToken);
312347

313348
HandleResponse(response, subscription);
314349
}
@@ -323,7 +358,7 @@ public async Task SendNotificationAsync(PushSubscription subscription, string pa
323358
public async Task SendNotificationAsync(PushSubscription subscription, string payload,
324359
VapidDetails vapidDetails)
325360
{
326-
var options = new Dictionary<string, object> {["vapidDetails"] = vapidDetails};
361+
var options = new Dictionary<string, object> { ["vapidDetails"] = vapidDetails };
327362
await SendNotificationAsync(subscription, payload, options);
328363
}
329364

@@ -336,7 +371,7 @@ public async Task SendNotificationAsync(PushSubscription subscription, string pa
336371
/// <param name="gcmApiKey">The GCM API key</param>
337372
public async Task SendNotificationAsync(PushSubscription subscription, string payload, string gcmApiKey)
338373
{
339-
var options = new Dictionary<string, object> {["gcmAPIKey"] = gcmApiKey};
374+
var options = new Dictionary<string, object> { ["gcmAPIKey"] = gcmApiKey };
340375
await SendNotificationAsync(subscription, payload, options);
341376
}
342377

@@ -355,7 +390,7 @@ private static void HandleResponse(HttpResponseMessage response, PushSubscriptio
355390
}
356391

357392
// Error
358-
var message = @"Received unexpected response code: " + (int) response.StatusCode;
393+
var message = @"Received unexpected response code: " + (int)response.StatusCode;
359394
switch (response.StatusCode)
360395
{
361396
case HttpStatusCode.BadRequest:
@@ -366,7 +401,7 @@ private static void HandleResponse(HttpResponseMessage response, PushSubscriptio
366401
message = "Payload too large";
367402
break;
368403

369-
case (HttpStatusCode) 429:
404+
case (HttpStatusCode)429:
370405
message = "Too many request.";
371406
break;
372407

@@ -378,7 +413,7 @@ private static void HandleResponse(HttpResponseMessage response, PushSubscriptio
378413

379414
throw new WebPushException(message, response.StatusCode, response.Headers, subscription);
380415
}
381-
416+
382417
public void Dispose()
383418
{
384419
if (_httpClient != null && _isHttpClientInternallyCreated)

0 commit comments

Comments
 (0)