简体   繁体   中英

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\\"} }" }

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

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

Going off of GrapplingCoder's answer, here is the JSON for sending a basic push notification with text that uses the default notification sound:

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

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