Skip to content

Commit cbc2fb3

Browse files
committed
Cancellation tokens added
1 parent 484deb7 commit cbc2fb3

File tree

5 files changed

+25
-28
lines changed

5 files changed

+25
-28
lines changed

CorePush/Apple/ApnSender.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using System.Net.Http;
88
using System.Security.Cryptography;
99
using System.Text;
10+
using System.Threading;
1011
using System.Threading.Tasks;
1112

1213
namespace CorePush.Apple
@@ -53,7 +54,8 @@ public async Task<ApnsResponse> SendAsync(
5354
string apnsId = null,
5455
int apnsExpiration = 0,
5556
int apnsPriority = 10,
56-
bool isBackground = false)
57+
bool isBackground = false,
58+
CancellationToken cancellationToken = default)
5759
{
5860
var path = $"/3/device/{deviceToken}";
5961
var json = JsonHelper.Serialize(notification);
@@ -77,7 +79,7 @@ public async Task<ApnsResponse> SendAsync(
7779
request.Headers.Add(apnidHeader, apnsId);
7880
}
7981

80-
using (var response = await http.SendAsync(request))
82+
using (var response = await http.SendAsync(request, cancellationToken))
8183
{
8284
var succeed = response.IsSuccessStatusCode;
8385
var content = await response.Content.ReadAsStringAsync();

CorePush/CorePush.csproj

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,16 @@
66
<Authors>andrei-m-code</Authors>
77
<Company />
88
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
9-
<AssemblyVersion>3.0.2.0</AssemblyVersion>
10-
<FileVersion>3.0.2.0</FileVersion>
11-
<Version>3.0.2</Version>
9+
<AssemblyVersion>3.0.3.0</AssemblyVersion>
10+
<FileVersion>3.0.3.0</FileVersion>
11+
<Version>3.0.3</Version>
1212
<PackageProjectUrl>https://github.com/andrei-m-code/CorePush</PackageProjectUrl>
1313
<!-- <PackageLicenseFile>LICENSE.md</PackageLicenseFile> -->
1414
<PackageLicenseExpression>MIT</PackageLicenseExpression>
1515
<PackageReleaseNotes>
16+
v3.0.3
17+
- Cancellation tokens added to the interface with CancellationToken.None by default
18+
1619
v3.0.2
1720
- Reverted Portable.BouncyCastle so that the lib can work in shared envs (like Azure App Service)
1821
- Apple certificate cleanup added

CorePush/Google/FcmSender.cs

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
using CorePush.Interfaces;
22
using CorePush.Utils;
3+
using Newtonsoft.Json.Linq;
34
using System.Net.Http;
45
using System.Text;
56
using System.Threading.Tasks;
6-
using Newtonsoft.Json.Linq;
7+
using System.Threading;
78

89
namespace CorePush.Google
910
{
@@ -31,13 +32,13 @@ public FcmSender(FcmSettings settings, HttpClient http)
3132
/// <param name="deviceId">Device token (will add `to` to the payload)</param>
3233
/// <param name="payload">Notification payload that will be serialized using Newtonsoft.Json package</param>
3334
/// <cref="HttpRequestException">Throws exception when not successful</exception>
34-
public Task<FcmResponse> SendAsync(string deviceId, object payload)
35+
public Task<FcmResponse> SendAsync(string deviceId, object payload, CancellationToken cancellationToken = default)
3536
{
3637
var jsonObject = JObject.FromObject(payload);
3738
jsonObject.Remove("to");
3839
jsonObject.Add("to", JToken.FromObject(deviceId));
3940

40-
return SendRawAsync(jsonObject.ToString());
41+
return SendAsync(jsonObject.ToString(), cancellationToken);
4142
}
4243

4344
/// <summary>
@@ -48,21 +49,10 @@ public Task<FcmResponse> SendAsync(string deviceId, object payload)
4849
/// </summary>
4950
/// <param name="payload">Notification payload that will be serialized using Newtonsoft.Json package</param>
5051
/// <exception cref="HttpRequestException">Throws exception when not successful</exception>
51-
public Task<FcmResponse> SendAsync(object payload)
52+
public async Task<FcmResponse> SendAsync(object payload, CancellationToken cancellationToken = default)
5253
{
53-
return SendRawAsync(JsonHelper.Serialize(payload));
54-
}
54+
var serialized = JsonHelper.Serialize(payload);
5555

56-
/// <summary>
57-
/// Send firebase notification.
58-
/// Please check out payload formats:
59-
/// https://firebase.google.com/docs/cloud-messaging/concept-options#notifications
60-
/// The SendAsync method will add/replace "to" value with deviceId
61-
/// </summary>
62-
/// <param name="payload">Notification payload json</param>
63-
/// <exception cref="HttpRequestException">Throws exception when not successful</exception>
64-
private async Task<FcmResponse> SendRawAsync(string payload)
65-
{
6656
using (var httpRequest = new HttpRequestMessage(HttpMethod.Post, fcmUrl))
6757
{
6858
httpRequest.Headers.Add("Authorization", $"key={settings.ServerKey}");
@@ -72,9 +62,9 @@ private async Task<FcmResponse> SendRawAsync(string payload)
7262
httpRequest.Headers.Add("Sender", $"id={settings.SenderId}");
7363
}
7464

75-
httpRequest.Content = new StringContent(payload, Encoding.UTF8, "application/json");
65+
httpRequest.Content = new StringContent(serialized, Encoding.UTF8, "application/json");
7666

77-
using (var response = await http.SendAsync(httpRequest))
67+
using (var response = await http.SendAsync(httpRequest, cancellationToken))
7868
{
7969
response.EnsureSuccessStatusCode();
8070
var responseString = await response.Content.ReadAsStringAsync();

CorePush/Interfaces/IApnSender.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System.Threading;
22
using System.Threading.Tasks;
33
using CorePush.Apple;
44

@@ -12,6 +12,7 @@ Task<ApnsResponse> SendAsync(
1212
string apnsId = null,
1313
int apnsExpiration = 0,
1414
int apnsPriority = 10,
15-
bool isBackground = false);
15+
bool isBackground = false,
16+
CancellationToken cancellationToken = default);
1617
}
1718
}

CorePush/Interfaces/IFcmSender.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
using System.Threading.Tasks;
21
using CorePush.Google;
2+
using System.Threading;
3+
using System.Threading.Tasks;
34

45
namespace CorePush.Interfaces
56
{
67
public interface IFcmSender
78
{
8-
Task<FcmResponse> SendAsync(string deviceId, object payload);
9-
Task<FcmResponse> SendAsync(object payload);
9+
Task<FcmResponse> SendAsync(string deviceId, object payload, CancellationToken cancellationToken = default);
10+
Task<FcmResponse> SendAsync(object payload, CancellationToken cancellationToken = default);
1011
}
1112
}

0 commit comments

Comments
 (0)