Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send iOS pushnotifications in JSON format trough Amazon SNS

I'm in the works of migrating my own iOS pushservice into Amazon SNS. Previously I was pushing messages trough our own server which no longer is enough.

The backend is built with PHP and this is some code of how I send a 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'
    );

That is the body of the notification I sent. Now I want to get the same notification to be sent trough SNS, with the AWS PHP SDK publish-method.

I've figured out that I have to send the pushnotification trough this:

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

What would $message be, in the codeexample above? All help is appreciated!

Edit: I've successfully sent the pushnotification trough SNS dashboard with the following JSON. My problem is to reproduce this trough the PHP SDK.

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

like image 376
Nicklas Wiborg Avatar asked Nov 30 '22 18:11

Nicklas Wiborg


2 Answers

I managed to solve the problem!

The following code did the trick, my previously attempts did not create either a good json-string (notice that there are two json_encodes in there) or didn't have the "default" json key which is required.

$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
        ));
like image 69
Nicklas Wiborg Avatar answered Dec 03 '22 07:12

Nicklas Wiborg


I don't know if this would work for your purposes, but in the AWS SNS API dashboard to send a single message out the format is like this:

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

To send to a topic in the dashboard you use a string like this:

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

The escaped quotes are required when sending from the dashboard.

When sending to production you replace APNS_SANDBOX with APNS

like image 43
Austen Chongpison Avatar answered Dec 03 '22 07:12

Austen Chongpison