通过 AWS SNS 向 FCM 发送 JSON 数据负载
Send JSON data payload to FCM by AWS SNS
我正在尝试从 AWS SNS 向 FCM 发送一条具有 data
负载的 json 消息。根据另一个线程,我从 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\" } }"
}
提前感谢您的帮助。
基于此相关 ,SNS 生成的消息将采用以下形式:
{
"GCM": "{ \"data\": { \"message\": \"test message\" } }"
}
因为如果没有实现接收它们的服务,data
有效负载将被忽略,所以我们应该发送 notification
有效负载。为此,只需将 JSON 消息更改为:
{
"GCM": "{ \"notification\": { \"text\": \"test message\" } }"
}
有关详细信息,您可以从给定的 link 查看 。
我将 string messageBody = message.GetNotification().Body;
更改为 messageBody = message.Data["message"].ToString();
并成功地检索了邮件正文的内容。
我正在尝试从 AWS SNS 向 FCM 发送一条具有 data
负载的 json 消息。根据另一个线程,我从 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\" } }"
}
提前感谢您的帮助。
基于此相关
{
"GCM": "{ \"data\": { \"message\": \"test message\" } }"
}
因为如果没有实现接收它们的服务,data
有效负载将被忽略,所以我们应该发送 notification
有效负载。为此,只需将 JSON 消息更改为:
{
"GCM": "{ \"notification\": { \"text\": \"test message\" } }"
}
有关详细信息,您可以从给定的 link 查看
我将 string messageBody = message.GetNotification().Body;
更改为 messageBody = message.Data["message"].ToString();
并成功地检索了邮件正文的内容。