C# 通过AWS SNS向FCM发送JSON数据有效负载
我正在尝试从AWS SNS向FCM发送一条包含C# 通过AWS SNS向FCM发送JSON数据有效负载,c#,amazon-web-services,firebase,firebase-cloud-messaging,amazon-sns,C#,Amazon Web Services,Firebase,Firebase Cloud Messaging,Amazon Sns,我正在尝试从AWS SNS向FCM发送一条包含数据有效负载的json消息。对于另一个线程,我从SNS发送的JSON消息应采用以下格式: { "GCM": "{ \"data\": { \"message\": \"test message\" } }" } { "GCM": "{ \"data\": { \"message\": \"test message\" } }" } 在我的Android应用程序中,我扩展了FirebaseMessagingService并覆盖了OnMessageR
数据有效负载的json消息。对于另一个线程,我从SNS发送的JSON消息应采用以下格式:
{
"GCM": "{ \"data\": { \"message\": \"test message\" } }"
}
{
"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发送数据有效载荷时,正确的JSON消息是什么
我也尝试过以下替代方案,但没有效果:
{
"GCM": "{ \"data\": { \"text\": \"test message\" } }"
}
{
"GCM": "{ \"data\": { \"body\": \"test message\" } }"
}
提前感谢您的帮助 根据此相关信息,SNS生成的消息将采用以下形式:
{
"GCM": "{ \"data\": { \"message\": \"test message\" } }"
}
{
"GCM": "{ \"data\": { \"message\": \"test message\" } }"
}
由于数据
如果没有实现接收有效负载的服务,有效负载将被忽略,因此我们应该发送通知
有效负载。为此,只需将JSON消息更改为:
{
"GCM": "{ \"notification\": { \"text\": \"test message\" } }"
}
有关更多信息,您可以从给定的链接中检查。I changedstring messageBody=message.GetNotification().Body
tomessageBody=message.Data[“message”].ToString()
并成功地检索到消息正文的内容 消息是否偶然为空?或者它是否正确地包含其他数据?不,消息不为空。它包含其他数据。