Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Expo.io Push Notifications Send Through C#
public class ExpoPushHelper
{
public static dynamic SendPushNotification(string ExpoToken)
{
dynamic body = new
{
to = ExpoToken,
title = "hello",
body = "world",
sound = "default",
data = new { some = "daaaata" }
};
string response = null;
using (WebClient client = new WebClient())
{
client.Headers.Add("accept", "application/json");
client.Headers.Add("accept-encoding", "gzip, deflate");
client.Headers.Add("Content-Type", "application/json");
response = client.UploadString("https://exp.host/--/api/v2/push/send", JsonExtensions.ToJson(body));
}
var json = JsonExtensions.FromJson<dynamic>(response);
return json;
}
}
@danparker276
Copy link

danparker276 commented Jul 25, 2018

I had an error which wasn't decoded with webclient like this because expo sent back as gzip.
I added this override of webclient and it worked to get the correct response
From here: https://stackoverflow.com/questions/2973208/automatically-decompress-gzip-response-via-webclient-downloaddata

` class MyWebClient : WebClient

{

    protected override WebRequest GetWebRequest(Uri address)

    {

        HttpWebRequest request = base.GetWebRequest(address) as HttpWebRequest;

        request.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;

        return request;

    }

}`

@danparker276
Copy link

danparker276 commented Aug 17, 2018

Also, another thing I found I had to do was add this for emojis
client.Encoding = System.Text.Encoding.UTF8;

after you create the client

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment