11using CorePush . Interfaces ;
22using CorePush . Utils ;
3+ using Newtonsoft . Json . Linq ;
34using System . Net . Http ;
45using System . Text ;
56using System . Threading . Tasks ;
6- using Newtonsoft . Json . Linq ;
7+ using System . Threading ;
78
89namespace 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 ( ) ;
0 commit comments