Azure 日志显示 : "The supplied notification payload is invalid" for official Xamarin. Android 示例

标签 azure xamarin.android azure-mobile-services pushsharp

所以我尝试运行 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/

相关文章:

Azure Key Vault Secrets 非托管和托管有什么区别?

Azure 成本管理 - 如何将 ResourceGuid 与 ResourceId 匹配

c# - 将变量从 ViewModel 传递到另一个 View (MVVMCross)

android - Xamarin/Android 应用程序 : Broadcast receiver not working

android - 在 Xamarin 中更改 ImageView 上的图像

android - 将 base64 图像从 Android 发布到 Azure 移动后端

ios - 如何将 iOS 数据发送到 Azure 移动服务数据库?

azure - 如何使用arm模板在服务总线队列级别创建自定义共享访问策略

azure - 有没有办法在使用当前 API 创建 Blob 时设置元数据,而不使用 Microsoft.Azure.Storage.Blob?

c# - 在 .NET 后端移动服务的 InsertAsync 期间引发 HttpResponseException