Skip to content

Instantly share code, notes, and snippets.

@rbravo
Last active July 24, 2021 06:42
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save rbravo/092dda13daf769b031c70d363aaf738d to your computer and use it in GitHub Desktop.
Save rbravo/092dda13daf769b031c70d363aaf738d to your computer and use it in GitHub Desktop.
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

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