Search code examples
androidamazon-web-servicesgoogle-cloud-messagingamazon-snsaws-step-functions

How to publish to android via AWS Step Functions and AWS SNS


I am trying to push to Android phones via AWS Step Functions and AWS SNS.

I am able to see the notification in the debug console, but it does not appear. How do i have to format the message correctly? I tried several combinations, but none worked.

"Publish notification": {
      "Type": "Task",
      "Resource": "arn:aws:states:::sns:publish",
      "Parameters": {
        "Message": {
          "default": "TestTestTest",
          "GCM": {
            "data": {
              "message": "Sample message for Android endpoints"
            }
          },
          "Input": "Hello from Step Functions!"
        },
        "MessageStructure": "json",
        "TargetArn": "arn:aws:sns:eu-central-1:xxxxxxxxx:endpoint/GCM/android/xxxxxxxxxxxxx"
      },
      "Next": "next state"
    }

The notification appears correctly on the phone if a send directly via SNS:

"GCM": "{ \"data\": { \"message\": \"Sample message for Android endpoints\" } }"

I also tried the Code Sample from the Step Functions Editor:

"Message": {
  "Input": "Hello from Step Functions!"
}

Solution

  • When it comes to FCM Push Notifications, there are two types of message types that are supported i.e “data” or ”notification”.

    Looking at your message payload, I see you are using message of type "data". This means that:

    “data" messages are not automatically handled by the FCM SDKs and will not be delivered to the notification center automatically therefore the clients' need to handle this type of messages.

    Resolution:

    You will need use message payload of type notification since you want the push notification to appear on the end-user device. See sample state machine definition I authored:

    {
    "StartAt": "Publish to SNS",
    "States": {
      "Publish to SNS": {
        "Type": "Task",
        "Resource": "arn:aws:states:::sns:publish",
        "Parameters": {
          "TargetArn": "arn:aws:sns:us-east-1:xxxxxxx:endpoint/GCM/AWSNS-Android/xxxxx-xxxxxxx-xxxxxxx",
          "MessageStructure": "json",
          "Message": {"GCM": "{ \"notification\": { \"body\": \"sample test push\" } }"}
        },
        "End": true
        }
      }
    }
    

    Hope this helps!