@@ -25,7 +25,7 @@ public class ApnSender : IApnSender
25
25
{ ApnServerType . Production , "https://api.push.apple.com:443" }
26
26
} ;
27
27
28
- private const string apnidHeader = "apns-id" ;
28
+ private const string apnIdHeader = "apns-id" ;
29
29
private const int tokenExpiresMinutes = 50 ;
30
30
31
31
private readonly ApnSettings settings ;
@@ -40,14 +40,16 @@ public ApnSender(ApnSettings settings, HttpClient http)
40
40
{
41
41
this . settings = settings ?? throw new ArgumentNullException ( nameof ( settings ) ) ;
42
42
this . http = http ?? throw new ArgumentNullException ( nameof ( http ) ) ;
43
+
44
+ http . BaseAddress = http . BaseAddress ?? new Uri ( servers [ settings . ServerType ] ) ;
43
45
}
44
46
45
47
/// <summary>
46
48
/// Serialize and send notification to APN. Please see how your message should be formatted here:
47
49
/// https://developer.apple.com/library/archive/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/CreatingtheNotificationPayload.html#//apple_ref/doc/uid/TP40008194-CH10-SW1
48
50
/// Payload will be serialized using Newtonsoft.Json package.
49
51
/// !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.
51
53
/// </summary>
52
54
/// <exception cref="HttpRequestException">Throws exception when not successful</exception>
53
55
public async Task < ApnsResponse > SendAsync (
@@ -62,36 +64,36 @@ public async Task<ApnsResponse> SendAsync(
62
64
var path = $ "/3/device/{ deviceToken } ";
63
65
var json = JsonHelper . Serialize ( notification ) ;
64
66
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 ) )
85
68
{
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
+ }
89
84
90
- return new ApnsResponse
85
+ using ( var response = await http . SendAsync ( message , cancellationToken ) )
91
86
{
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
+ }
95
97
}
96
98
}
97
99
0 commit comments