【问题标题】:Send JSON data payload to FCM by AWS SNS通过 AWS SNS 将 JSON 数据负载发送到 FCM
【发布时间】:2017-03-20 12:40:51
【问题描述】:

我正在尝试将具有 data 有效负载的 json 消息从 AWS SNS 发送到 FCM。根据另一个线程,我从 SNS 发送的 JSON 消息应采用以下形式:

{
"GCM": "{ \"data\": { \"message\": \"test message\" } }"
}

在我的 Android 应用程序中,我扩展了 FirebaseMessagingService 并覆盖了 OnMessageReceived 方法来处理传入的推送通知。

我的代码如下所示:

    public override void OnMessageReceived(RemoteMessage message)
    {
        string messageBody = message.GetNotification().Body; //Fails here
        int custom1;
        string custom2 = string.Empty;

        try { custom1 = Convert.ToInt32(message.Data["custom1"]); }
        catch (KeyNotFoundException e) { custom1 = -1; }

        try { custom2 = message.Data["custom2"].ToString(); }
        catch (KeyNotFoundException e) { custom2 = "err"; }

        PublishNotification(messageBody, custom1, custom2);
    }

当我使用上面写的 JSON 消息通过 SNS 发送自定义通知时,消息被成功接收。但是,当我尝试处理 JSON 时,一旦到达message.GetNotification().Body,它就会失败。我收到的错误告诉我正文未包含在 JSON 消息中。

我的问题是,从 AWS SNS 向 FCM 发送 data 有效负载时,正确的 JSON 消息是什么。

我也尝试了以下替代方法,但无济于事:

{
"GCM": "{ \"data\": { \"text\": \"test message\" } }"
}

{
"GCM": "{ \"data\": { \"body\": \"test message\" } }"
}

提前感谢您的帮助。

【问题讨论】:

  • message 是否偶然为空?或者它是否正确包含其他数据?
  • 不,消息不为空。它包含其他数据。

标签: c# amazon-web-services firebase firebase-cloud-messaging amazon-sns


【解决方案1】:

基于此相关SO thread,SNS 生成的消息将具有以下形式:

{
"GCM": "{ \"data\": { \"message\": \"test message\" } }"
}

由于data 负载将被忽略,如果没有实现接收它们的服务,我们应该发送一个notification 负载。为此,只需将 JSON 消息更改为:

{
"GCM": "{ \"notification\": { \"text\": \"test message\" } }"
}

有关详细信息,您可以从给定链接中查看answer

【讨论】:

    【解决方案2】:

    我将string messageBody = message.GetNotification().Body; 更改为messageBody = message.Data["message"].ToString(); 并成功检索到消息正文的内容。

    【讨论】:

      猜你喜欢
      • 2020-09-04
      • 2015-10-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-22
      • 2013-09-21
      • 2015-09-28
      • 2020-09-21
      相关资源
      最近更新 更多