1

因此,我尝试为 Xamarin.Android http://azure.microsoft.com/en-us/documentation/articles/partner-xamarin-mobile-services-android-get-started-push/运行此推送通知示例,然后按照说明进行操作来自文档 - 我启动并运行了它。项目的插入工作绝对正常,但推送通知拒绝工作。

这是我在 Azure 上推送的错误:错误:400 - 提供的通知负载无效。

还有其他人尝试在他们的设备上运行此示例并尝试推送通知吗?这个错误对我的情况没有多大帮助。

该示例使用 PushSharp。

我会很感激任何帮助。非常感谢!

4

1 回答 1

0

这就是我从后端服务器向 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

}
于 2014-07-22T16:43:02.323 回答