【发布时间】:2016-10-01 17:45:50
【问题描述】:
我需要在通过 xamarin 制作的 android 应用程序中单击 A 按钮时发送 GCM 通知。
Button btnCLick = Findviewbyid<button>(resource.id.btnclikc);
btnCLick.Click += btnCLick_CLICK;
void btnCLick_Click (object sender, System.EventArgs e)
{
// Here i need to send my notification. I am not able to get it.
}
我使用 MessageSender.exe 发送通知,但无法将其发送到我的应用程序中。
using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;
namespace MessageSender
{
class Program
{
public const string API_KEY = "API_KEY";
public const string MESSAGE = "MESSAGE";
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 =>
{
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);
}
}
}
}
我需要在 xamarin.Android 中将其添加到我的应用程序按钮中单击 我该怎么做??
【问题讨论】:
-
您是在尝试从您的 Android 应用发送消息吗?
-
是的,推送通知
-
您的客户端应用程序订阅了全局主题吗?您能否在问题中包含该代码?
-
是的。! developer.xamarin.com/guides/cross-platform/… 我在链接中使用了相同的代码来为我的应用程序订阅全局主题。
标签: c# azure xamarin google-cloud-messaging xamarin.android