所以我尝试运行 Xamarin.Android 的推送通知示例 http://azure.microsoft.com/en-us/documentation/articles/partner-xamarin-mobile-services-android-get-started-push/在遵循文档的说明后 - 我启动并运行了它。项目的插入工作绝对正常,但推送通知拒绝工作。
这是我在 Azure 上推送时遇到的错误:错误:400 - 提供的通知负载无效。
还有其他人尝试在他们的设备上运行此示例并尝试过推送通知吗?该错误对我的情况没有多大帮助。
该示例使用 PushSharp。
如果有任何帮助,我将不胜感激。非常感谢!
最佳答案
这就是我从后端服务器向 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;
}
然后在您的应用 Intent Service 中,您可以解析 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
}
关于Azure 日志显示 : "The supplied notification payload is invalid" for official Xamarin. Android 示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24880722/