Created
October 29, 2020 17:01
-
-
Save sefacan/bf4689bb52d655e0fed831b5d21b7a8e to your computer and use it in GitHub Desktop.
Firebase Notification
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public async Task<bool> SendNotificationAsync(string token, string title, string body) | |
{ | |
using (var client = new HttpClient()) | |
{ | |
var firebaseOptionsServerId = _firebaseOptions.ServerApiKey; | |
var firebaseOptionsSenderId = _firebaseOptions.SenderId; | |
client.BaseAddress = new Uri("https://fcm.googleapis.com"); | |
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); | |
client.DefaultRequestHeaders.TryAddWithoutValidation("Authorization", | |
$"key={firebaseOptionsServerId}"); | |
client.DefaultRequestHeaders.TryAddWithoutValidation("Sender", $"id={firebaseOptionsSenderId}"); | |
var data = new | |
{ | |
to = token, | |
notification = new | |
{ | |
body = body, | |
title = title, | |
}, | |
priority = "high" | |
}; | |
var json = JsonConvert.SerializeObject(data); | |
var httpContent = new StringContent(json, Encoding.UTF8, "application/json"); | |
var result = await _client.PostAsync("/fcm/send", httpContent); | |
return result.StatusCode.Equals(HttpStatusCode.OK); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment