diff --git a/sendmsg/Program.cs b/sendmsg/Program.cs new file mode 100644 index 00000000..60291ee6 --- /dev/null +++ b/sendmsg/Program.cs @@ -0,0 +1,54 @@ +using System; +using System.Net.Http; +using System.Net.Http.Headers; +using System.Text; +using System.Threading.Tasks; +using Newtonsoft.Json.Linq; + +namespace MessageSender +{ + public class Program + { + public const string API_KEY = "AIzaSyDwPlu2ky9AoPwDvThoXxJwqiPq-Tc8p8k"; + public const string MESSAGE = "Hello, Xamarin!"; + + static void Main(string[] args) + { + var jGcmData = new JObject(); + var jData = new JObject(); + + jData.Add("message", MESSAGE); + jGcmData.Add("to", "/topics/global"); + jGcmData.Add("data", jData); + + var url = new Uri("https://gcm-http.googleapis.com/gcm/send"); + try + { + using (var client = new HttpClient()) + { + client.DefaultRequestHeaders.Accept.Add( + new MediaTypeWithQualityHeaderValue("application/json")); + + client.DefaultRequestHeaders.TryAddWithoutValidation( + "Authorization", "key=" + API_KEY); + + Task.WaitAll(client.PostAsync(url, + new StringContent(jGcmData.ToString(), Encoding.Default, "application/json")) + .ContinueWith(response => + { + Task.Run ( async () => { + var content = await response.Result.Content.ReadAsStringAsync(); + Console.WriteLine(content); + }); + Console.WriteLine(">Message sent: check the client device notification tray."); + })); + } + } + catch (Exception e) + { + Console.WriteLine("Unable to send GCM message:"); + Console.Error.WriteLine(e.StackTrace); + } + } + } +} diff --git a/sendmsg/project.json b/sendmsg/project.json new file mode 100644 index 00000000..9fadf190 --- /dev/null +++ b/sendmsg/project.json @@ -0,0 +1,30 @@ +{ + "version": "1.0.0-*", + "compilationOptions": { + "emitEntryPoint": true + }, + "tooling": { + "defaultNamespace": "sendmsg" + }, + "dependencies": { + "Microsoft.AspNet.IISPlatformHandler": "1.0.0-rc1-final", + "Microsoft.AspNet.Server.Kestrel": "1.0.0-rc1-final", + "System.Net.Http": "4.1.0", + "Newtonsoft.Json": "9.0.1" + }, + "commands": { + "web": "Microsoft.AspNet.Server.Kestrel" + }, + "frameworks": { + "dnx451": {}, + "dnxcore50": {} + }, + "exclude": [ + "wwwroot", + "node_modules" + ], + "publishExclude": [ + "**.user", + "**.vspscc" + ] +} \ No newline at end of file