Reputation: 1539
Simple question, how to send badge number in push notifications, using AWS SNS ?
I found out that it has to be formatted in the json format somehow, but not sure how to do it.
AWS docs don't have that info, or at least I didn't found it.
Thanks!
My code so far, which works:
$sns = SnsClient::factory(array(
'key' => $this->app()->getConfig()->get('aws.sns.key'),
'secret' => $this->app()->getConfig()->get('aws.sns.secret'),
'region' => $this->app()->getConfig()->get('aws.sns.region'),
));
$payload = [
'Message' => $this->_message,
'TargetArn' => $this->_device->getDeviceArn()
];
$sns->publish($payload);
Upvotes: 4
Views: 1524
Reputation: 1612
In case you need to reset the badge number, you can publish to SNS endpoint with {badge:0}
{
"APNS_SANDBOX":"{\"aps\":{\"alert\":\"Removing the badge\", \"badge\":0}}"
}
Hope that helps someone.
Upvotes: 0
Reputation: 700
First of all, take a look at this AWS Developer Forum article
To sum that article up, in your example, _message needs to look something like this:
{"APNS":"{\"aps\":{\"alert\":\"<message>\"}}"}
and you need to add
'MessageStructure' => 'json',
to $payload
This is the code I use for building the message JSON:
$contents = array();
$contents['badge'] = "1";
$contents['alert'] = addslashes($push_message);
$contents['sound'] = "default";
$push = array("aps" => $contents);
$push_json = json_encode($push);
$json = json_encode(array('APNS' => $push_json));
$sns->publish(array('MessageStructure' => 'json',
'Message' => $json,
'TargetArn' => $endpointArn));
Hope this helps!
Upvotes: 2