這就是我如何從後端服務器向Google Cloud Messaging發送推送通知的方式。
public async Task<bool> SendNotification(int id, int index, string from, string text, string tag)
{
try
{
var payload = new
{
data = new
{
message = new
{
// this part can be anything you want
id,
index,
from,
text,
when = DateTime.UtcNow.ToString("s") + "Z"
}
}
};
var json = JsonConvert.SerializeObject(payload);
await _hubClient.SendGcmNativeNotificationAsync(json, tag);
return true;
}
catch (ArgumentException ex)
{
// This is expected when an APNS registration doesn't exist.
return false;
}
然後在你的應用程序意向服務,您可以解析JSON「消息」:
protected override void OnMessage(Context context, Intent intent)
{
var message = intent.Extras.GetString("message");
// message is JSON payload
// { "id":"someid", "index":"1", "text":"some text","from"... }
var json = JObject.Parse(message);
var id = json["id"].ToString();
var index = json["index"].ToString();
var text = json["text"].ToString();
var from = json["from"].ToString();
var when = DateTime.Parse(json["when"].ToString());
// do whatever you want with your values here
}
嘿,後端是蔚藍 - 看我上面鏈接的樣本。我確實設法解決這個問題,並得到推送通知成功日誌,但是從不調用OnMessage方法。有任何想法嗎? –
您是否在啓動時註冊過設備? – Kiliman
是的,日誌顯示設備已註冊。服務器說推送已發送 - 但Onmessage()從不調用。 –