Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to send an APN alert message with a sound using SNS mobile push notifications? #336

Closed
goleador opened this issue Aug 21, 2013 · 16 comments
Labels
guidance Question that needs advice or information.

Comments

@goleador
Copy link

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.

@lsegal
Copy link
Contributor

lsegal commented Aug 21, 2013

Can you provide more details on what doesn't work? If you are getting an error, please provide the full stack trace.

@goleador
Copy link
Author

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

@goleador
Copy link
Author

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

@goleador
Copy link
Author

It works if sending from AWS Console

@ghost ghost assigned awood45 Aug 21, 2013
@lsegal
Copy link
Contributor

lsegal commented Aug 21, 2013

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.

@goleador
Copy link
Author

NICE! It worked. Thank you again @lsegal .

@lsegal
Copy link
Contributor

lsegal commented Aug 21, 2013

Glad you got it working!

@sunnycmf
Copy link

@isegal thanks you solve my problem! you rocks guy!

@ingscjoshua
Copy link

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

@rajuptb
Copy link

rajuptb commented Apr 20, 2014

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

@thebucknerlife
Copy link

@ingscjoshua did you resolve this issue? I'm having the same problem.

@thebucknerlife
Copy link

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

@jmstone617
Copy link

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

@anoojr
Copy link

anoojr commented Dec 15, 2014

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?

@Sailias
Copy link

Sailias commented Jan 12, 2015

@jmstone617, your solution works for me.

Double to_json, and sound: 'default' seems to do it.

@jayd3e
Copy link

jayd3e commented Feb 13, 2017

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.

@diehlaws diehlaws added guidance Question that needs advice or information. and removed question labels Jan 4, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
guidance Question that needs advice or information.
Projects
None yet
Development

No branches or pull requests