我正在尝试开发一个Xamarin Android应用程序,使用GCM服务来发送推送通知 .
我按照Xamarin教程page上的教程,讲授如何注册GCM,请求设备令牌以及如何创建消息发送器 .
一切正常,除了我想发送推送消息到特定设备 token x ,而不是所有这些 .
Message Sender如下所示:
private static void messageSender(string title, string message, string summary)
{
var jGcmData = new JObject();
var jData = new JObject();
jData.Add("message", message);
jData.Add("title", title);
jData.Add("summary", summary);
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 =>
{
Console.WriteLine(response);
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);
}
}
有帮助吗?
1 回答