简体   繁体   中英

PHP AWS SNS SDK - Badge

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

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!

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM