Skip to content

Commit

Permalink
Added push notification class
Browse files Browse the repository at this point in the history
  • Loading branch information
joseotavioq committed Nov 18, 2016
1 parent 83a780d commit 1528a8f
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions Notification/PushNotification.cs
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();
}
}
}
}

0 comments on commit 1528a8f

Please sign in to comment.