sendmsg ok sous GNU/Linux
This commit is contained in:
54
sendmsg/Program.cs
Normal file
54
sendmsg/Program.cs
Normal file
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
30
sendmsg/project.json
Normal file
30
sendmsg/project.json
Normal file
@ -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"
|
||||
]
|
||||
}
|
Reference in New Issue
Block a user