4
4
using System . Net . Http ;
5
5
using System . Net . Http . Headers ;
6
6
using System . Runtime . CompilerServices ;
7
+ using System . Threading ;
7
8
using System . Threading . Tasks ;
8
9
using WebPush . Util ;
9
10
@@ -26,14 +27,14 @@ public class WebPushClient : IDisposable
26
27
27
28
public WebPushClient ( )
28
29
{
29
-
30
+
30
31
}
31
32
32
33
public WebPushClient ( HttpClient httpClient )
33
34
{
34
35
_httpClient = httpClient ;
35
36
}
36
-
37
+
37
38
public WebPushClient ( HttpClientHandler httpClientHandler )
38
39
{
39
40
_httpClientHandler = httpClientHandler ;
@@ -142,7 +143,7 @@ public HttpRequestMessage GenerateRequestDetails(PushSubscription subscription,
142
143
143
144
if ( options != null )
144
145
{
145
- var validOptionsKeys = new List < string > { "headers" , "gcmAPIKey" , "vapidDetails" , "TTL" } ;
146
+ var validOptionsKeys = new List < string > { "headers" , "gcmAPIKey" , "vapidDetails" , "TTL" } ;
146
147
foreach ( var key in options . Keys )
147
148
{
148
149
if ( ! validOptionsKeys . Contains ( key ) )
@@ -181,7 +182,7 @@ public HttpRequestMessage GenerateRequestDetails(PushSubscription subscription,
181
182
}
182
183
183
184
//at this stage ttl cannot be null.
184
- timeToLive = ( int ) ttl ;
185
+ timeToLive = ( int ) ttl ;
185
186
}
186
187
}
187
188
@@ -258,9 +259,27 @@ public HttpRequestMessage GenerateRequestDetails(PushSubscription subscription,
258
259
/// </param>
259
260
public void SendNotification ( PushSubscription subscription , string payload = null ,
260
261
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 )
261
280
{
262
281
var request = GenerateRequestDetails ( subscription , payload , options ) ;
263
- var sendAsyncTask = HttpClient . SendAsync ( request ) ;
282
+ var sendAsyncTask = HttpClient . SendAsync ( request , cancellationToken ) ;
264
283
sendAsyncTask . Wait ( ) ;
265
284
266
285
var response = sendAsyncTask . Result ;
@@ -277,7 +296,7 @@ public void SendNotification(PushSubscription subscription, string payload = nul
277
296
/// <param name="vapidDetails">The vapid details for the notification.</param>
278
297
public void SendNotification ( PushSubscription subscription , string payload , VapidDetails vapidDetails )
279
298
{
280
- var options = new Dictionary < string , object > { [ "vapidDetails" ] = vapidDetails } ;
299
+ var options = new Dictionary < string , object > { [ "vapidDetails" ] = vapidDetails } ;
281
300
SendNotification ( subscription , payload , options ) ;
282
301
}
283
302
@@ -290,7 +309,7 @@ public void SendNotification(PushSubscription subscription, string payload, Vapi
290
309
/// <param name="gcmApiKey">The GCM API key</param>
291
310
public void SendNotification ( PushSubscription subscription , string payload , string gcmApiKey )
292
311
{
293
- var options = new Dictionary < string , object > { [ "gcmAPIKey" ] = gcmApiKey } ;
312
+ var options = new Dictionary < string , object > { [ "gcmAPIKey" ] = gcmApiKey } ;
294
313
SendNotification ( subscription , payload , options ) ;
295
314
}
296
315
@@ -304,11 +323,27 @@ public void SendNotification(PushSubscription subscription, string payload, stri
304
323
/// Options for the GCM API key and vapid keys can be passed in if they are unique for each
305
324
/// notification.
306
325
/// </param>
307
- public async Task SendNotificationAsync ( PushSubscription subscription , string payload = null ,
326
+ public Task SendNotificationAsync ( PushSubscription subscription , string payload = null ,
308
327
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 )
309
344
{
310
345
var request = GenerateRequestDetails ( subscription , payload , options ) ;
311
- var response = await HttpClient . SendAsync ( request ) ;
346
+ var response = await HttpClient . SendAsync ( request , cancellationToken ) ;
312
347
313
348
HandleResponse ( response , subscription ) ;
314
349
}
@@ -323,7 +358,7 @@ public async Task SendNotificationAsync(PushSubscription subscription, string pa
323
358
public async Task SendNotificationAsync ( PushSubscription subscription , string payload ,
324
359
VapidDetails vapidDetails )
325
360
{
326
- var options = new Dictionary < string , object > { [ "vapidDetails" ] = vapidDetails } ;
361
+ var options = new Dictionary < string , object > { [ "vapidDetails" ] = vapidDetails } ;
327
362
await SendNotificationAsync ( subscription , payload , options ) ;
328
363
}
329
364
@@ -336,7 +371,7 @@ public async Task SendNotificationAsync(PushSubscription subscription, string pa
336
371
/// <param name="gcmApiKey">The GCM API key</param>
337
372
public async Task SendNotificationAsync ( PushSubscription subscription , string payload , string gcmApiKey )
338
373
{
339
- var options = new Dictionary < string , object > { [ "gcmAPIKey" ] = gcmApiKey } ;
374
+ var options = new Dictionary < string , object > { [ "gcmAPIKey" ] = gcmApiKey } ;
340
375
await SendNotificationAsync ( subscription , payload , options ) ;
341
376
}
342
377
@@ -355,7 +390,7 @@ private static void HandleResponse(HttpResponseMessage response, PushSubscriptio
355
390
}
356
391
357
392
// Error
358
- var message = @"Received unexpected response code: " + ( int ) response . StatusCode ;
393
+ var message = @"Received unexpected response code: " + ( int ) response . StatusCode ;
359
394
switch ( response . StatusCode )
360
395
{
361
396
case HttpStatusCode . BadRequest :
@@ -366,7 +401,7 @@ private static void HandleResponse(HttpResponseMessage response, PushSubscriptio
366
401
message = "Payload too large" ;
367
402
break ;
368
403
369
- case ( HttpStatusCode ) 429 :
404
+ case ( HttpStatusCode ) 429 :
370
405
message = "Too many request." ;
371
406
break ;
372
407
@@ -378,7 +413,7 @@ private static void HandleResponse(HttpResponseMessage response, PushSubscriptio
378
413
379
414
throw new WebPushException ( message , response . StatusCode , response . Headers , subscription ) ;
380
415
}
381
-
416
+
382
417
public void Dispose ( )
383
418
{
384
419
if ( _httpClient != null && _isHttpClientInternallyCreated )
0 commit comments