0

I am trying to send a json message that has data 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 FirebaseMessagingService and overrode the OnMessageReceived 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 message.GetNotification().Body. 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 data 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.

AL.
  • 36,815
  • 10
  • 142
  • 281
afontalv
  • 313
  • 1
  • 4
  • 12

2 Answers2

1

Based from this related SO thread, the message that is generated by SNS Will be of the form:

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

Since data payloads will be ignored if no service to receive them has been implemented, instead we should send a notification payload. To do this, simply change the JSON message to read:

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

For more information, you can check the answer from the given link.

Community
  • 1
  • 1
abielita
  • 13,147
  • 2
  • 17
  • 59
0

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

afontalv
  • 313
  • 1
  • 4
  • 12