LoginSignup
70
71

More than 5 years have passed since last update.

AWS SDK for PHPを利用したAmazon SNSのMobile Push

Last updated at Posted at 2013-09-06

Amazon SNSでMobile PushがサポートされたのでiOS向けだけ試してみました。
今回は下記の環境で行っています。
■環境
PHP5.5.1
AWD SDK for PHP2.4.5

たぶんMobile Pushは2.4.4からです。

まず、SDKを使う前に、AWSのManagement Consoleからでpushできるので、
そちらを試してみてください。
■参考URL
http://dev.classmethod.jp/cloud/aws-amazon-sns-mobile-push-ios/

次に、AWSのブログですね。SDKを使ったサンプルです。
■Amazon Web Services ブログ
http://aws.typepad.com/aws_japan/2013/08/push-notifications-to-mobile-devices-using-amazon-sns.html

で、サンプルコードです。
最低限のコードなので、マニュアルを見た上で自己責任でお願いします。


<?php
require'/aws_sdk_path/vendor/autoload.php';
use Aws\Common\Aws;
use Aws\Common\Enum\Region;
use Aws\Sns\SnsClient;

class Sample {

    // Amazon SDKのインスタンス
    private $obj = null;

    // ManagementConsoleで登録したアプリ(APNS_SANDBOX:開発用)
    private $PlatformApplicationArn = 'arn:aws:sns:ap-northeast-1:000000000000:app/APNS_SANDBOX/sample_app'; // dummy

    /**
     * AWS SDK for PHP
     */
    private function getInstance() {
        if (is_null($this->obj)) {
            $this->obj = Aws::factory(array(
                'key'    => 'dummy',
                'secret' => 'dummy',
                'region' => Region::TOKYO
            ))->get('sns');
        }
        return $this->obj;
    }

    /**
     * Push通知先(EndpointArn)を登録
     */
    public function createPlatformEndpoint($token) {
        $options = array(
            'PlatformApplicationArn' => $this->PlatformApplicationArn,
            'Token'                  => $token,
        );
        try {
            $res = $this->getInstance()->createPlatformEndpoint($options);
        } catch (Exception $e) {
//          echo $e->getMessage();
            return false;
        }
        return $res; // $res['EndpointArn']
    }

    /**
     * 通知
     */
    public function publish($message, $EndpointArn) {
        try {
            $res = $this->getInstance()->publish(array(
                'Message'   => $message,
                'TargetArn' => $EndpointArn
            ));
        } catch (Exception $e) {
//          echo $e->getMessage();
            return false;
        }
        return $res;
    }

    /**
     * 通知(JSON)
     */
    public function publishJson($args) {
        try {
            $res = $this->getInstance()->publish($args);
        } catch (Exception $e) {
//          echo $e->getMessage();
            return false;
        }
        return $res;
    }
}

$message = 'Amazon SNS Mobile Push message.';

// メッセージのみを通知
$AmazonSns = new Sample();
$res = $AmazonSns->createPlatformEndpoint('dummy');
$AmazonSns->publish($message, $res['EndpointArn']);

// JSON形式で色々な値を送る ※文字列の上限に達すると送られないので注意
$AmazonSns->publishJson(array(
    'MessageStructure' => 'json',
    'TargetArn' => $res['EndpointArn'],
    'Message' => json_encode(array(
        'APNS_SANDBOX' => json_encode(array(
            'aps' => array(
                'alert' => $message,
                'badge' => 10, // int型でアプリアイコンに表示するbadge数を指定
                'test_id' => 1 // 好きなパラメータを設定可能
            )
        ))
    )),
));

使い方としては、createPlatformEndpoint()でtokenを登録し、
そのレスポンスで返ってきたEndpointArnを利用してpublish()するだけです。
注意点としては、アプリ名(PlatformApplicationArn)も通知先(EndpointArn)も
AWS側で生成されるものだということです。

実際にサービスで利用する場合は、
1,Management Console上でアプリを登録してPlatformApplicationArnを取得
2,createPlatformEndpoint()のレスポンスのEndpointArnをDB等に登録
3,必要なユーザに対してだけpublish()
という形かなと思います。

AWS側で生成される値を使うのはちょっと面倒かな・・・

70
71
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
70
71