2020using System . Threading . Tasks ;
2121using Google . Apis . Auth . OAuth2 ;
2222using Google . Apis . Http ;
23+ using Google . Apis . Json ;
2324using Google . Apis . Requests ;
2425using Google . Apis . Services ;
2526using Google . Apis . Util ;
@@ -61,6 +62,14 @@ public FirebaseMessagingClient(
6162 this . fcmClientService = new FCMClientService ( this . httpClient ) ;
6263 }
6364
65+ internal static string ClientVersion
66+ {
67+ get
68+ {
69+ return $ "fire-admin-dotnet/{ FirebaseApp . GetSdkVersion ( ) } ";
70+ }
71+ }
72+
6473 /// <summary>
6574 /// Sends a message to the FCM service for delivery. The message gets validated both by
6675 /// the Admin SDK, and the remote FCM service. A successful return value indicates
@@ -92,8 +101,7 @@ public async Task<string> SendAsync(
92101 } ;
93102 try
94103 {
95- var response = await this . httpClient . PostJsonAsync (
96- this . sendUrl , request , cancellationToken ) . ConfigureAwait ( false ) ;
104+ var response = await this . SendRequestAsync ( request , cancellationToken ) . ConfigureAwait ( false ) ;
97105 var json = await response . Content . ReadAsStringAsync ( ) . ConfigureAwait ( false ) ;
98106 if ( ! response . IsSuccessStatusCode )
99107 {
@@ -157,12 +165,32 @@ public void Dispose()
157165 this . httpClient . Dispose ( ) ;
158166 }
159167
168+ private static void AddCommonHeaders ( HttpRequestMessage request )
169+ {
170+ request . Headers . Add ( "X-Firebase-Client" , ClientVersion ) ;
171+ }
172+
160173 private static FirebaseException CreateExceptionFor ( RequestError requestError )
161174 {
162175 return new FirebaseException ( requestError . ToString ( ) ) ;
163176 }
164177
165- private async Task < BatchResponse > SendBatchRequestAsync ( IEnumerable < Message > messages , bool dryRun , CancellationToken cancellationToken )
178+ private async Task < HttpResponseMessage > SendRequestAsync ( object body , CancellationToken cancellationToken )
179+ {
180+ var request = new HttpRequestMessage ( )
181+ {
182+ Method = HttpMethod . Post ,
183+ RequestUri = new Uri ( this . sendUrl ) ,
184+ Content = NewtonsoftJsonSerializer . Instance . CreateJsonHttpContent ( body ) ,
185+ } ;
186+ AddCommonHeaders ( request ) ;
187+ return await this . httpClient . SendAsync ( request , cancellationToken ) . ConfigureAwait ( false ) ;
188+ }
189+
190+ private async Task < BatchResponse > SendBatchRequestAsync (
191+ IEnumerable < Message > messages ,
192+ bool dryRun ,
193+ CancellationToken cancellationToken )
166194 {
167195 var responses = new List < SendResponse > ( ) ;
168196
@@ -181,21 +209,30 @@ private async Task<BatchResponse> SendBatchRequestAsync(IEnumerable<Message> mes
181209 }
182210 else
183211 {
184- responses . Add ( SendResponse . FromException ( new FirebaseException ( $ "Unexpected batch response. Response status code was { message . StatusCode } .") ) ) ;
212+ responses . Add ( SendResponse . FromException ( new FirebaseException (
213+ $ "Unexpected batch response. Response status code was { message . StatusCode } .") ) ) ;
185214 }
186215 } ) ;
187216
188- await batch . ExecuteAsync ( cancellationToken ) ;
217+ await batch . ExecuteAsync ( cancellationToken ) . ConfigureAwait ( false ) ;
189218 return new BatchResponse ( responses ) ;
190219 }
191220
192- private BatchRequest CreateBatchRequest ( IEnumerable < Message > messages , bool dryRun , BatchRequest . OnResponse < SingleMessageResponse > callback )
221+ private BatchRequest CreateBatchRequest (
222+ IEnumerable < Message > messages ,
223+ bool dryRun ,
224+ BatchRequest . OnResponse < SingleMessageResponse > callback )
193225 {
194226 var batch = new BatchRequest ( this . fcmClientService , FcmBatchUrl ) ;
195227
196228 foreach ( var message in messages )
197229 {
198- batch . Queue ( new FCMClientServiceRequest ( this . fcmClientService , this . restPath , message , dryRun ) , callback ) ;
230+ var body = new SendRequest ( )
231+ {
232+ Message = message ,
233+ ValidateOnly = dryRun ,
234+ } ;
235+ batch . Queue ( new FCMClientServiceRequest ( this . fcmClientService , this . restPath , body ) , callback ) ;
199236 }
200237
201238 return batch ;
@@ -258,16 +295,17 @@ public ConfigurableHttpClient CreateHttpClient(CreateHttpClientArgs args)
258295 private sealed class FCMClientServiceRequest : ClientServiceRequest < string >
259296 {
260297 private readonly string restPath ;
261- private readonly Message message ;
262- private readonly bool dryRun ;
298+ private readonly SendRequest body ;
263299
264- public FCMClientServiceRequest ( IClientService clientService , string restPath , Message message , bool dryRun )
300+ public FCMClientServiceRequest ( FCMClientService clientService , string restPath , SendRequest body )
265301 : base ( clientService )
266302 {
267303 this . restPath = restPath ;
268- this . message = message ;
269- this . dryRun = dryRun ;
270-
304+ this . body = body ;
305+ this . ModifyRequest = ( request ) =>
306+ {
307+ AddCommonHeaders ( request ) ;
308+ } ;
271309 this . InitParameters ( ) ;
272310 }
273311
@@ -279,13 +317,7 @@ public FCMClientServiceRequest(IClientService clientService, string restPath, Me
279317
280318 protected override object GetBody ( )
281319 {
282- var sendRequest = new SendRequest ( )
283- {
284- Message = this . message ,
285- ValidateOnly = this . dryRun ,
286- } ;
287-
288- return sendRequest ;
320+ return this . body ;
289321 }
290322 }
291323 }
0 commit comments