afontalv afontalv -4 years ago 299
C# Question

Send JSON data payload to FCM by AWS SNS

I am trying to send a json message that has

payload from AWS SNS to FCM. Per another thread, the JSON message that I send from SNS should be in the form:

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

Within my Android App, I have extended the
and overrode the
method to handle incoming push notifications.

Here's what my code looks like:

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);

When I send a custom notification through SNS using the JSON message I have written above, the message is received successfully. However, when I attempt to process the JSON, it fails once it reaches
. The error I receive tells me the body was not included in the JSON message.

My question is, what is the correct JSON message when sending
payloads from AWS SNS to FCM.

I have tried the following alternatives as well, to no avail:

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

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

Thank you in advance for any help.

Answer Source

I changed string messageBody = message.GetNotification().Body; to messageBody = message.Data["message"].ToString(); and successfully managed to retrieved the contents of the message body.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download