This is not working:
AWS.sns.client.publish :message => { :aps => { :alert => params[:message]}}.to_json, :target_arn => target
I can send a message just fine. But if I try to structure the JSON it doesn't work.
Can you provide more details on what doesn't work? If you are getting an error, please provide the full stack trace.
I get the message on the phone but it's just the JSON. I expected it to play a sound and to display just the message not the json.
{
"APNS_SANDBOX":"{\"aps\":{\"alert\":\"
}
Basically it displays the whole text above when I expected
I am trying this now still does not work:
AWS.sns.client.publish :subject => 'test', :message => { :APNS_SANDBOX => {:aps => { :alert => params[:message]}}.to_json }.to_json, :target_arn => target
It works if sending from AWS Console
I'm reading through the Java sample application provided in the mobile push getting started guide, and it seems as though they set the MessageStructure parameter to 'json'. This might be required. See line 229 of the SNSSamples/src/com/amazonaws/sns/samples/mobilepush/SNSMobilePush.java file. I think you might want the structure to look like:
message = { :default => "Default message", :aps => { :alert => params[:message]}}.to_json
AWS.sns.client.publish :message => message, :target_arn => target, :message_structure => 'json'
Note that you also need a "default" key at the root of your JSON object if you use the 'json' message structure. See the Ruby docs for #publish and the API docs for more info on this parameter.
I can't test this myself, so let me know if this works for you.
NICE! It worked. Thank you again @lsegal .
Glad you got it working!
@isegal thanks you solve my problem! you rocks guy!
Hi i'm try to send notifications whit sound and badge but only recibe default text
message = { :default => "Credito aprobado", :APNS_SANDBOX => {:aps => { :alert =>'Test',:badge =>'1',:sound =>'default'}}}.to_json
AWS.sns.client.publish :message => message, :target_arn => topic_arn, :message_structure => 'json'
Im not understand what is the problem?
any ideas?
Regards
I use the following in PHP
$title = 'My Test Message';
$sound = 'doorbell.caf';
$msgpayload=json_encode(array('aps' => array('alert' => $title,'sound' => $sound,)));
$response = $sns->publish(array(
'TopicArn' => $TopicArn,
'MessageStructure' => 'json',
'Message' => json_encode(array(
'default' => $title,
'APNS_SANDBOX' => $msgpayload
))
));
@ingscjoshua did you resolve this issue? I'm having the same problem.
For anyone else who arrives here, the issue is very similar to the issue discussed in the aws-sdk-js library above. You have to encode your json twice (sigh). Working code in ruby for aws-sdk:
client = AWS::SNS::Client.new
apns_payload = { "aps" => { "alert" => "hey it worked!", "badge" => 14 } }.to_json
message = { "default" => "this is the default", "APNS" => apns_payload }.to_json
client.publish( message: message, target_arn: your_endpoint_arn, message_structure: 'json' )
This is also incorrect (or at least has changed as of today)
The following works for me and successfully plays a sound:
apns_payload = { "aps" => { "alert" => @message, "sound" => 'default' } }.to_json
message = { "default" => @message, "APNS_SANDBOX" => apns_payload }.to_json
sns.publish(target_arn: device.endpoint, message: message, message_structure: 'json')
Suddenly started having SNS turn endpoints 'Enabled' to 'false' all by itself. Also I tried bulding the json by hand and still not working. Here is what I tried. Any help is appreciated. Thanks
message = {"default" => 'Test text', "APNS_SANDBOX" => {"aps" => {"alert" => "Test text", 'badge' => 1, 'sound' => 'default'}}}.to_json
client.publish(target_arn: t_arn, message: message, message_structure: 'json')
gives me a message_id and a request_metadata with request_id as subkey.
Is there a place to look at the logs for these requests. At least on sandbox?
@jmstone617, your solution works for me.
Double to_json, and sound: 'default' seems to do it.
I'm use the AWS Java SDK, and this thread really helped me. Apparently all SNS SDKs require the platform-specific payload to be a string and not a nested object. Encoding my JSON twice fixed the issue.
Most helpful comment
For anyone else who arrives here, the issue is very similar to the issue discussed in the aws-sdk-js library above. You have to encode your json twice (sigh). Working code in ruby for aws-sdk: