Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send push notification in Laravel using AWS SNS with default sound in Android and iOS device?

I have code like the below to send push notification. It is sent but with no sound on notification.

$sns = App::make('aws')->createClient('sns');

$sns->publish(array(
'TargetArn' => ‘arn:aws:sns:us-east-1:757730885501:endpoint/APNS/…..’,
       'Message' => ‘Test message’
));

Any suggestions please?!

like image 650
Rooby Doo Avatar asked Jul 08 '16 12:07

Rooby Doo


1 Answers

Try this,

For iOS device, use like this

$apns_payload = json_encode(array("aps" => array("alert" => "This is a test message", "sound" => 'default')));
$message = json_encode(array( "default" => "This is a test message", "APNS" => $apns_payload));

For Android device, use like this

$gcm_payload = json_encode(array("data" => array("message" => "This is a test message", "sound" => 'default')));
$message = json_encode(array("default" => "This is a test message", "GCM" => $gcm_payload));

And finally, publish with your AWS SNS device ARN like below, TargetArn gets changed based on device token and platform

If iOS,

$target_arn = "arn:aws:sns:us-east-1:757730885501:endpoint/APNS/.....";

If Android,

$target_arn = "arn:aws:sns:us-east-1:757730885501:app/GCM/.....";

$sns->publish(array(
  'TargetArn' => $target_arn,
  'Message' => $message,
  'MessageStructure' => 'json'
));

If you need to play the default sound in the device, you can set it as default, or else you can also use like ‘sound’ => ‘doorbell.caf’.

like image 186
suguna Avatar answered Nov 20 '22 09:11

suguna