The Amazon SNS documentation is still lacking here, with few pointers on how to format a message to use a custom payload. This FAQ explains how to do it, but doesn't provide an example.
The solution is to publish the notification with the MessageStructure
parameter set to json
and a Message
parameter that is json-encoded, with a key for each transport protocol. There always needs to be a default
key too, as a fallback.
This is an example for iOS notifications with a custom payload:
array(
'TargetArn' => $EndpointArn,
'MessageStructure' => 'json',
'Message' => json_encode(array(
'default' => $title,
'APNS' => json_encode(array(
'aps' => array(
'alert' => $title,
),
// Custom payload parameters can go here
'id' => '123',
's' => 'section'
))
))
);
The same goes for other protocols as well. The format of the json_encoded message must be like this (but you can omit keys if you don't use the transport):
{
"default": "<enter your message here>",
"email": "<enter your message here>",
"sqs": "<enter your message here>",
"http": "<enter your message here>",
"https": "<enter your message here>",
"sms": "<enter your message here>",
"APNS": "{"aps":{"alert": "<message>","sound":"default"} }",
"APNS_SANDBOX": "{"aps":{"alert": "<message>","sound":"default"} }",
"GCM": "{ "data": { "message": "<message>" } }",
"ADM": "{ "data": { "message": "<message>" } }"
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…