Skip to content

Commit 9c9b612

Browse files
committed
#64 Add cancellation taken to Async methods and remove duplicate methods.
1 parent 265a3cc commit 9c9b612

File tree

1 file changed

+11
-46
lines changed

1 file changed

+11
-46
lines changed

WebPush/WebPushClient.cs

+11-46
Original file line numberDiff line numberDiff line change
@@ -265,32 +265,9 @@ public HttpRequestMessage GenerateRequestDetails(PushSubscription subscription,
265265
public void SendNotification(PushSubscription subscription, string payload = null,
266266
Dictionary<string, object> options = null)
267267
{
268-
SendNotification(subscription, payload, options, CancellationToken.None);
269-
}
270-
271-
272-
/// <summary>
273-
/// To send a push notification call this method with a subscription, optional payload and any options
274-
/// Will exception if unsuccessful
275-
/// </summary>
276-
/// <param name="subscription">The PushSubscription you wish to send the notification to.</param>
277-
/// <param name="payload">The payload you wish to send to the user</param>
278-
/// <param name="options">
279-
/// Options for the GCM API key and vapid keys can be passed in if they are unique for each
280-
/// notification.
281-
/// </param>
282-
/// <param name="cancellationToken">The cancellation token to cancel operation.</param>
283-
public void SendNotification(PushSubscription subscription, string payload,
284-
Dictionary<string, object> options, CancellationToken cancellationToken)
285-
{
286-
var request = GenerateRequestDetails(subscription, payload, options);
287-
var sendAsyncTask = HttpClient.SendAsync(request, cancellationToken);
288-
sendAsyncTask.Wait();
289-
290-
var response = sendAsyncTask.Result;
291-
292-
HandleResponse(response, subscription);
268+
SendNotification(subscription, payload, options);
293269
}
270+
294271

295272
/// <summary>
296273
/// To send a push notification call this method with a subscription, optional payload and any options
@@ -317,23 +294,9 @@ public void SendNotification(PushSubscription subscription, string payload, stri
317294
var options = new Dictionary<string, object> { ["gcmAPIKey"] = gcmApiKey };
318295
SendNotification(subscription, payload, options);
319296
}
297+
320298

321299
/// <summary>
322-
/// To send a push notification asynchronous call this method with a subscription, optional payload and any options
323-
/// Will exception if unsuccessful
324-
/// </summary>
325-
/// <param name="subscription">The PushSubscription you wish to send the notification to.</param>
326-
/// <param name="payload">The payload you wish to send to the user</param>
327-
/// <param name="options">
328-
/// Options for the GCM API key and vapid keys can be passed in if they are unique for each
329-
/// notification.
330-
/// </param>
331-
public Task SendNotificationAsync(PushSubscription subscription, string payload = null,
332-
Dictionary<string, object> options = null)
333-
{
334-
return SendNotificationAsync(subscription, payload, options, CancellationToken.None);
335-
}
336-
337300
/// To send a push notification asynchronous call this method with a subscription, optional payload and any options
338301
/// Will exception if unsuccessful
339302
/// </summary>
@@ -344,8 +307,8 @@ public Task SendNotificationAsync(PushSubscription subscription, string payload
344307
/// notification.
345308
/// </param>
346309
/// <param name="cancellationToken">The cancellation token to cancel operation.</param>
347-
public async Task SendNotificationAsync(PushSubscription subscription, string payload,
348-
Dictionary<string, object> options, CancellationToken cancellationToken)
310+
public async Task SendNotificationAsync(PushSubscription subscription, string payload = null,
311+
Dictionary<string, object> options = null, CancellationToken cancellationToken=default)
349312
{
350313
var request = GenerateRequestDetails(subscription, payload, options);
351314
var response = await HttpClient.SendAsync(request, cancellationToken);
@@ -360,11 +323,12 @@ public async Task SendNotificationAsync(PushSubscription subscription, string pa
360323
/// <param name="subscription">The PushSubscription you wish to send the notification to.</param>
361324
/// <param name="payload">The payload you wish to send to the user</param>
362325
/// <param name="vapidDetails">The vapid details for the notification.</param>
326+
/// <param name="cancellationToken"></param>
363327
public async Task SendNotificationAsync(PushSubscription subscription, string payload,
364-
VapidDetails vapidDetails)
328+
VapidDetails vapidDetails, CancellationToken cancellationToken=default)
365329
{
366330
var options = new Dictionary<string, object> { ["vapidDetails"] = vapidDetails };
367-
await SendNotificationAsync(subscription, payload, options);
331+
await SendNotificationAsync(subscription, payload, options, cancellationToken);
368332
}
369333

370334
/// <summary>
@@ -374,10 +338,11 @@ public async Task SendNotificationAsync(PushSubscription subscription, string pa
374338
/// <param name="subscription">The PushSubscription you wish to send the notification to.</param>
375339
/// <param name="payload">The payload you wish to send to the user</param>
376340
/// <param name="gcmApiKey">The GCM API key</param>
377-
public async Task SendNotificationAsync(PushSubscription subscription, string payload, string gcmApiKey)
341+
/// <param name="cancellationToken"></param>
342+
public async Task SendNotificationAsync(PushSubscription subscription, string payload, string gcmApiKey, CancellationToken cancellationToken=default)
378343
{
379344
var options = new Dictionary<string, object> { ["gcmAPIKey"] = gcmApiKey };
380-
await SendNotificationAsync(subscription, payload, options);
345+
await SendNotificationAsync(subscription, payload, options, cancellationToken);
381346
}
382347

383348
/// <summary>

0 commit comments

Comments
 (0)