77using CorePush . Interfaces ;
88using CorePush . Serialization ;
99
10- namespace CorePush . Google
10+ namespace CorePush . Firebase
1111{
1212 /// <summary>
1313 /// Firebase message sender
1414 /// </summary>
15- public class FcmSender : IFcmSender
15+ public class FirebaseSender : IFirebaseSender
1616 {
17- // TODO: Migrate to the new API: https://firebase.google.com/docs/cloud-messaging/send-message
18- private const string fcmUrl = "https://fcm.googleapis.com/fcm/send" ;
19-
20- private readonly FcmSettings settings ;
17+ private readonly FirebaseSettings settings ;
2118 private readonly HttpClient http ;
22- private readonly Serialization . IJsonSerializer serializer ;
19+ private readonly IJsonSerializer serializer ;
2320
24- public FcmSender ( FcmSettings settings , HttpClient http ) : this ( settings , http , new DefaultJsonSerializer ( ) )
21+ public FirebaseSender ( FirebaseSettings settings , HttpClient http ) : this ( settings , http , new DefaultJsonSerializer ( ) )
2522 {
23+
2624 }
2725
28- public FcmSender ( FcmSettings settings , HttpClient http , IJsonSerializer serializer )
26+ public FirebaseSender ( FirebaseSettings settings , HttpClient http , IJsonSerializer serializer )
2927 {
30- this . settings = settings ?? throw new ArgumentNullException ( nameof ( settings ) ) ;
3128 this . http = http ?? throw new ArgumentNullException ( nameof ( http ) ) ;
3229 this . serializer = serializer ?? throw new ArgumentNullException ( nameof ( serializer ) ) ;
30+ this . settings = settings ?? throw new ArgumentNullException ( nameof ( settings ) ) ;
3331
32+ if ( string . IsNullOrWhiteSpace ( settings . GoogleProjectId ) ||
33+ string . IsNullOrWhiteSpace ( settings . FcmBearerToken ) )
34+ {
35+ throw new ArgumentException ( "Some settings are not defined" , nameof ( settings ) ) ;
36+ }
37+
3438 if ( http . BaseAddress == null )
3539 {
36- http . BaseAddress = new Uri ( fcmUrl ) ;
40+ var url = $ "https://fcm.googleapis.com/v1/projects/{ settings . GoogleProjectId } /messages:send";
41+ http . BaseAddress = new Uri ( url ) ;
3742 }
3843 }
3944
4045 /// <summary>
41- /// Send firebase notification.
46+ /// Send firebase notification. Token must be present in order to send direct push notification.
4247 /// Please check out payload formats:
4348 /// https://firebase.google.com/docs/cloud-messaging/concept-options#notifications
44- /// The SendAsync method will add/replace "to" value with deviceId
49+ /// https://firebase.google.com/docs/cloud-messaging/send-message
4550 /// </summary>
4651 /// <param name="payload">Notification payload that will be serialized using Newtonsoft.Json package</param>
4752 /// <param name="cancellationToken">Cancellation token</param>
4853 /// <exception cref="HttpRequestException">Throws exception when not successful</exception>
49- public async Task < FcmResponse > SendAsync ( object payload , CancellationToken cancellationToken = default )
54+ public async Task < FirebaseResponse > SendAsync ( object payload , CancellationToken cancellationToken = default )
5055 {
5156 var json = serializer . Serialize ( payload ) ;
5257
5358 using var message = new HttpRequestMessage ( ) ;
5459
5560 message . Method = HttpMethod . Post ;
56- message . Headers . Add ( "Authorization" , $ "key = { settings . ServerKey } ") ;
57-
58- if ( ! string . IsNullOrEmpty ( settings . SenderId ) )
59- {
60- message . Headers . Add ( "Sender" , $ "id = { settings . SenderId } ") ;
61- }
62-
61+ message . Headers . Add ( "Authorization" , $ "Bearer { settings . FcmBearerToken } ") ;
6362 message . Content = new StringContent ( json , Encoding . UTF8 , "application/json" ) ;
6463
6564 using var response = await http . SendAsync ( message , cancellationToken ) ;
@@ -70,7 +69,7 @@ public async Task<FcmResponse> SendAsync(object payload, CancellationToken cance
7069 throw new HttpRequestException ( "Firebase notification error: " + responseString ) ;
7170 }
7271
73- return serializer . Deserialize < FcmResponse > ( responseString ) ;
72+ return serializer . Deserialize < FirebaseResponse > ( responseString ) ;
7473 }
7574 }
7675}
0 commit comments