Skip to content

Instantly share code, notes, and snippets.

@sefacan
Created October 29, 2020 17:01
Show Gist options
  • Save sefacan/bf4689bb52d655e0fed831b5d21b7a8e to your computer and use it in GitHub Desktop.
Save sefacan/bf4689bb52d655e0fed831b5d21b7a8e to your computer and use it in GitHub Desktop.
Firebase Notification
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