Skip to content

Commit 60a4d52

Browse files
committed
FCM and APN base url change for testing
1 parent b41ea74 commit 60a4d52

File tree

3 files changed

+40
-34
lines changed

3 files changed

+40
-34
lines changed

CorePush/Apple/ApnSender.cs

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public class ApnSender : IApnSender
2525
{ApnServerType.Production, "https://api.push.apple.com:443" }
2626
};
2727

28-
private const string apnidHeader = "apns-id";
28+
private const string apnIdHeader = "apns-id";
2929
private const int tokenExpiresMinutes = 50;
3030

3131
private readonly ApnSettings settings;
@@ -40,14 +40,16 @@ public ApnSender(ApnSettings settings, HttpClient http)
4040
{
4141
this.settings = settings ?? throw new ArgumentNullException(nameof(settings));
4242
this.http = http ?? throw new ArgumentNullException(nameof(http));
43+
44+
http.BaseAddress = http.BaseAddress ?? new Uri(servers[settings.ServerType]);
4345
}
4446

4547
/// <summary>
4648
/// Serialize and send notification to APN. Please see how your message should be formatted here:
4749
/// https://developer.apple.com/library/archive/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/CreatingtheNotificationPayload.html#//apple_ref/doc/uid/TP40008194-CH10-SW1
4850
/// Payload will be serialized using Newtonsoft.Json package.
4951
/// !IMPORTANT: If you send many messages at once, make sure to retry those calls. Apple typically doesn't like
50-
/// to receive too many requests and may ocasionally respond with HTTP 429. Just try/catch this call and retry as needed.
52+
/// to receive too many requests and may occasionally respond with HTTP 429. Just try/catch this call and retry as needed.
5153
/// </summary>
5254
/// <exception cref="HttpRequestException">Throws exception when not successful</exception>
5355
public async Task<ApnsResponse> SendAsync(
@@ -62,36 +64,36 @@ public async Task<ApnsResponse> SendAsync(
6264
var path = $"/3/device/{deviceToken}";
6365
var json = JsonHelper.Serialize(notification);
6466

65-
var request = new HttpRequestMessage(HttpMethod.Post, new Uri(servers[settings.ServerType] + path))
66-
{
67-
Version = new Version(2, 0),
68-
Content = new StringContent(json)
69-
};
70-
71-
request.Headers.Authorization = new AuthenticationHeaderValue("bearer", GetJwtToken());
72-
request.Headers.TryAddWithoutValidation(":method", "POST");
73-
request.Headers.TryAddWithoutValidation(":path", path);
74-
request.Headers.Add("apns-topic", settings.AppBundleIdentifier);
75-
request.Headers.Add("apns-expiration", apnsExpiration.ToString());
76-
request.Headers.Add("apns-priority", apnsPriority.ToString());
77-
request.Headers.Add("apns-push-type", isBackground ? "background" : "alert"); // required for iOS 13+
78-
79-
if (!string.IsNullOrWhiteSpace(apnsId))
80-
{
81-
request.Headers.Add(apnidHeader, apnsId);
82-
}
83-
84-
using (var response = await http.SendAsync(request, cancellationToken))
67+
using (var message = new HttpRequestMessage(HttpMethod.Post, path))
8568
{
86-
var succeed = response.IsSuccessStatusCode;
87-
var content = await response.Content.ReadAsStringAsync();
88-
var error = JsonHelper.Deserialize<ApnsError>(content);
69+
message.Version = new Version(2, 0);
70+
message.Content = new StringContent(json);
71+
72+
message.Headers.Authorization = new AuthenticationHeaderValue("bearer", GetJwtToken());
73+
message.Headers.TryAddWithoutValidation(":method", "POST");
74+
message.Headers.TryAddWithoutValidation(":path", path);
75+
message.Headers.Add("apns-topic", settings.AppBundleIdentifier);
76+
message.Headers.Add("apns-expiration", apnsExpiration.ToString());
77+
message.Headers.Add("apns-priority", apnsPriority.ToString());
78+
message.Headers.Add("apns-push-type", isBackground ? "background" : "alert"); // required for iOS 13+
79+
80+
if (!string.IsNullOrWhiteSpace(apnsId))
81+
{
82+
message.Headers.Add(apnIdHeader, apnsId);
83+
}
8984

90-
return new ApnsResponse
85+
using (var response = await http.SendAsync(message, cancellationToken))
9186
{
92-
IsSuccess = succeed,
93-
Error = error
94-
};
87+
var succeed = response.IsSuccessStatusCode;
88+
var content = await response.Content.ReadAsStringAsync();
89+
var error = JsonHelper.Deserialize<ApnsError>(content);
90+
91+
return new ApnsResponse
92+
{
93+
IsSuccess = succeed,
94+
Error = error
95+
};
96+
}
9597
}
9698
}
9799

CorePush/CorePush.csproj

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
<Summary>Server Side library for sending ✅Web, ✅Android and ✅iOS Push Notifications</Summary>
1111
<Authors>andrei-m-code</Authors>
1212

13-
<AssemblyVersion>3.0.13</AssemblyVersion>
14-
<FileVersion>3.0.13</FileVersion>
15-
<Version>3.0.13</Version>
13+
<AssemblyVersion>3.1.0</AssemblyVersion>
14+
<FileVersion>3.1.0</FileVersion>
15+
<Version>3.1.0</Version>
1616

1717
<PackageProjectUrl>https://github.com/andrei-m-code/CorePush</PackageProjectUrl>
1818
<RepositoryUrl>https://github.com/andrei-m-code/CorePush</RepositoryUrl>
@@ -25,6 +25,10 @@
2525
<PackageTags>push-notifications android-push-notifications ios-push-notifications web-push web-push-notifications apn fcm firebase</PackageTags>
2626

2727
<PackageReleaseNotes>
28+
v3.1.0
29+
- Memory optimizations
30+
- Ability to change FCM and APN base URL for testing purposes
31+
2832
v3.0.11 - v3.0.12
2933
- Package information update
3034

CorePush/Google/FcmSender.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ public class FcmSender : IFcmSender
2121

2222
public FcmSender(FcmSettings settings, HttpClient http)
2323
{
24-
this.settings = settings;
25-
this.http = http;
24+
this.settings = settings ?? throw new ArgumentNullException(nameof(settings));
25+
this.http = http ?? throw new ArgumentNullException(nameof(http));
2626

2727
http.BaseAddress = http.BaseAddress ?? new Uri(fcmUrl);
2828
}

0 commit comments

Comments
 (0)