Send iOS pushnotifications in JSON format through Amazon SNS

I am working on porting my own iOS pushservice to Amazon SNS. I used to push messages through our own server, which was no longer enough.

The backend is built with PHP, and this is the code of how I send pushnotification with the old solution:

$body = array(
    'alert' => array('body' => $id,
                   'action-loc-key' => 'read this',
                   'loc-key' => '%@',
                   'loc-args' => array($message)),
    'badge' => '0',
    'sound' => 'default',
    'content-available' => '1'
    );

This is the body of the notification that I sent. Now I want the same notification to be sent via SNS, with the AWS PHP SDK publish-method .

I realized that I needed to send pushnotification through this:

$result = $snsClient->publish(array(
        'TargetArn' => $target,
        // Message is required
        'Message' => $message,
        'MessageStructure' => 'json'
    ));

What will be the message $ message in the codeexample example above? All help is appreciated!

Edit: I successfully sent the SNN launcher using the following JSON. My problem is to reproduce this through the PHP SDK.

{   "APNS": "{\" aps\ ": {\" alert\ ": {\" body\ ": \" 7500\ ", \" action-loc-key\ ": \" read this\ ",\" loc-key \ ":\" % @\ ",\" loc-args \ ": [\" Message \ "]},\" \ ":\" 0 \ ",\" sound \ ":\" default \ ",\" content-available \ ":\" 1 \ "}}" }

+4
3

!

, json- ( , json_encodes), "json- ", .

$message = json_encode(array(
                'default' => $message,
                'APNS' => json_encode(array(
                    'aps' => array(
                        'alert' => array('body' => $id,
                               'action-loc-key' => 'read this',
                               'loc-key' => '%@',
                               'loc-args' => array($message)),
                    ),
                    'badge' => '0',
                    'sound' => 'default',
                    'content-available' => '1'
                ))
            ));

$result = $snsClient->publish(array(
            'TargetArn' => $target,
            'MessageStructure' => 'json',
            'Message' => $message
        ));
+10

, , AWS SNS API :

{"APNS_SANDBOX": "{\"aps\": {\"alert\":\"HERE IS AN ALERT, BADGE, and SOUND!\",\"badge\": 1,\"sound\":\"bingbong.aiff\"}}"}

​​:

{ 
    "default": "HERE IS AN ALERT, BADGE, and SOUND",
    "APNS_SANDBOX": "{\"aps\": {\"alert\":\"HERE IS AN ALERT, BADGE, and SOUND!\",\"badge\": 1,\"sound\":\"bingbong.aiff\"}}"
}

.

APNS_SANDBOX APNS

+6

GrapplingCoder, JSON push- , :

{
"APNS": "{\"aps\":{\"alert\": \"This is your message text!\",\"sound\":\"default\"} }"
}
+2

All Articles