-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
83a780d
commit 1528a8f
Showing
1 changed file
with
33 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
using System; | ||
using System.Net.Http; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Firebase.Xamarin.Auth; | ||
|
||
namespace Firebase.Xamarin.Notification | ||
{ | ||
public class PushNotification | ||
{ | ||
private const string NotificationUrl = "https://fcm.googleapis.com/fcm/send"; | ||
|
||
private readonly FirebaseConfig authConfig; | ||
|
||
public PushNotification(FirebaseConfig authConfig) | ||
{ | ||
this.authConfig = authConfig; | ||
} | ||
|
||
public async Task Send(string to, string title, string message) | ||
{ | ||
using (HttpClient client = new HttpClient()) | ||
{ | ||
var postContent = $"{{\"to\":\"{to}\",\"notification\":{{ \"title\":\"{title}\",\"text\":\"{message}\" }}}}"; | ||
|
||
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("key", $"={this.authConfig.ApiKeyForPushNotification}"); | ||
var response = await client.PostAsync(NotificationUrl, new StringContent(postContent, Encoding.UTF8, "application/json")).ConfigureAwait(false); | ||
|
||
response.EnsureSuccessStatusCode(); | ||
} | ||
} | ||
} | ||
} |