気軽なSNS Mobile Push の話

AWS Casual Talks#1 で話したSNS Mobile Pushの話です

8ヶ月前 にアップロード

(2013年11月01日)

スポンサーリンク

「気軽なSNS Mobile Push の話」の内容

  1. 気軽なSNS Mobile Push の話
  2. 星野 豊 (@con_mame) クックパッド株式会社 インフラストラクチャー部 AWS / MySQL / Redshift / DataStore etc... http://d.conma.me/ http://facebook.com/conmame
  3. Push Notification
  4. iPhone / iPad / Android / Kindle とかに通知を送るあれ
  5. APNS / GCM / ADM
  6. プラットホーム毎に 仕様が違う
  7. iOS / Android iOS / Android / WP8 / Win Store iOS / Android / WP8 / Win Store / Kindle / BlackBerry iOS / Android / Kindle
  8. SNS Mobile Push
  9. 100万Push当たり $1 (Push依頼 $0.5 + Push配信 $0.5) Topicを作成することにより異なるプラット ホームに同時に配信可能 安定してる AWS内で完結する 証明書などの認証情報をSNSに登録する APNS / APNS_SANDBOX / GCM / ADM
  10. Tokenの無効・変更などの処理を自動で行う Feedback通知も可能 無効なEndpoint Arnは送信対象から外 して余計なコストやパフォーマンス定 価を避ける (e.g. アプリケーションが アンインストールされた) 遅延あり (e.g. APNS feedback service) ∼数日
  11. Device token
  12. ことば Device token: アプリケーションを識別す るための各Platformが発行するもの Endpoint Arn: Device Tokenから生成さ れ、SNSで各deviceを識別するもの (SNSの操作ではこ れを使う) Application Arn: Pushをするアプリ ケーションの単位
  13. Device tokenのSNSへの登録方法 Device -> API Device -> SNS Token埋め込み Token Vending Machine SNSではEndpoint ArnとApplication Arnを 使用
  14. Device to API
  15. token DB M API token DB S API 1 Token request 2 Token response 3 Token send 4 Token create 5 Endpoint Arn 6 Save Endpoint Arn
  16. Device to SNS
  17. token DB M API token DB S API 1 Token request 2 Token response 3 Token send 4 Endpoint Arn 6 Save Endpoint Arn 5 Endpoint Arn
  18. token DB M API token DB S API 1 Token request 2 Token response 3 Token send 4 Endpoint Arn 6 Save Endpoint Arn 5 Endpoint Arn Application Arn AWS Access Key AWS Secret Key
  19. AWSアクセス情報をアプリケーションに埋め込む必要があ る Access Key / Secretの変更のためにはアプリケー ションの審査提出が必要 全ユーザが直ぐにUpdateするとは限らない アクセス情報が漏れた場合などを考えてIAMで権限を 絞っておく (e.g. Create Endpoint)
  20. Token Vending Machine
  21. token DB M API token DB S API 1 Token request 2 Token response 4 Token send 5 Endpoint Arn 7 Save Endpoint Arn 6 Endpoint Arn TVM 3 Get Temporary credentials http://amzn.to/16DN8Rb
  22. token DB M API token DB S API 1 Token request 2 Token response 4 Token send 5 Endpoint Arn 7 Save Endpoint Arn 6 Endpoint Arn TVM 3 Get Temporary credentials Application Arn AWS Tmp Access Key AWS Tmp Secret Key http://amzn.to/16DN8Rb
  23. Token Vending Machine (TVM) AWS Security Token Service (STS) STSに必要な権限だけをつけたIAM (Role)をつけたアプリケーション(イン スタンス)がAWSのIdentity / Access Management Serviceにアクセスして必 要な権限をリクエスト 一時的なアクセス情報を発行 標準では1時間
  24. アクセス情報が漏れた場合でも、その Keyなどは標準では1時間で使えなくなる Application Arnも一緒に返すこと推薦 Application Arn自体はSNS Mobile Push Applicationを識別するだけ arn:aws:sns:ap- northeast-1:xxxxxxxxxxx:app/APNS/ ApplicationName
  25. アクセス情報が漏れた場合でも、その Keyなどは標準では1時間で使えなくなる Application Arnも一緒に返すこと推薦 Application Arn自体はSNS Mobile Push Applicationを識別するだけ arn:aws:sns:ap- northeast-1:xxxxxxxxxxx:app/APNS/ ApplicationName AWS Account ID
  26. Push
  27. # アクセス情報の設定は省略 client = AWS::SNS.new.client # 登録 response = client.create_endpoint( platform_application_arn: application_arn, token: device_token, custom_user_data: '' ) # endpoint arnを取得 endpoint_arn = response[:endpoint_arn] # push (messageはJSONエンコード) client.publish(target_arn: endpoint_arn, message: 'hoge')
  28. グループにPushする場合はSNS Topic e.g. 年代・カテゴリ・機能・ユーザ ステータス... TopicはApplication Arnをまたげる 複数のEndpoint Arnをまとめられる Platformに関係なく送信可能
  29. { "default": "<enter your message here>", "email": "<enter your message here>", "sqs": "<enter your message here>", "http": "<enter your message here>", "https": "<enter your message here>", "sms": "<enter your message here>", "APNS": "{\"aps\":{\"alert\": \"<message>\",\"sound\": \"default\",\"badge\": 2} }", "GCM": "{ \"data\": { \"message\": \"<message>\" } }", "ADM": "{ \"data\": { \"message\": \"<message>\" } }" }
  30. Topicの制限 (default) 3,000 topics / account 10,000 subscribers / topic
  31. Batch or App 1 Get Endpoint Arn and user status Batch feedb ak 2 Generate message and put queue 4 Change Message Status 3 dequeue 5 Publish 6 Push to platform Feedback Notification Manage Tokens Retry Push Architecture
  32. Batch or App 1 Get Endpoint Arn and user status Batch feedb ak 4 Publish 5 Push to platform Feedback Notification Manage Tokens Simple Push Architecture 2 Make Push information file 3 Get information file and mark with sent
  33. metadata
  34. metadata Active?
  35. Application Action {"EndpointArn":"arn:aws:sns:ap-northeast-1:xxxx:endpoint/APNS/ApplicatonName/ 7bxxxx98-2xxxf-xxxx- bxx4-8cxxxxxx93","EventType":"EndpointUpdated","Resource":"arn:aws: sns:ap-northeast-1:xxxxx:app/APNS/ ApplicatonName","Service":"SNS","Time":"2013-10-27T20:44:12.545Z"}
  36. <GetEndpointAttributesResponse xmlns="http://sns.amazonaws.com/doc/2010-03-31/&quot;&gt;&lt;GetEndpointAttributesResult&gt;&lt;Attributes&gt;&lt;entry&gt;&lt;key&gt;Enabled&lt;/key&gt;&lt;value&gt;false&lt;/value&gt;&lt;/entry&gt;&lt;entry&gt;&lt;key&gt;Token&lt;/key&gt;&lt;value&gt;device token</value> </entry> </Attributes> </GetEndpointAttributesResult> <ResponseMetadata> <RequestId>4dbxxxxx-aaaa-vvvv-ddddd-xxxxxx</RequestId> </ResponseMetadata> </GetEndpointAttributesResponse> GetEndpointAttributes
  37. いろいろ
  38. 各Endpoint arnにはUser Dataを付与出来る 今のところPushを行う時の抽出用メタ データとして使えない Topicの制限 1つのTopic(カテゴリ)に10,000 device以 上の場合はtopicを分割する必要がある subscriberの属性が変更されたらTopicを 移動させないと… 属性を自前で管理して各deviceに個別Push
  39. いろいろ。。。
  40. とういうものがあって Device token: 11111... をCustom User Dataなしで登 録しようとする場合
  41. params = { 'Action' => 'CreatePlatformEndpoint', 'AWSAccessKeyId' => ACCESS_IDENTIFIER, 'SignatureMethod' => 'HmacSHA256', 'SignatureVersion' => 2, 'Timestamp' => Time.now.iso8601, 'Version' => '2010-03-31', 'CustomUserData' => 'userdata', 'Token' => 'DEVICE TOKEN', 'PlatformApplicationArn' => 'arn:aws:sns:ap-northeast-1:xxxx:app/APNS/hoge' } CreatePlatformEndpoint params = { 'Action' => 'CreatePlatformEndpoint', 'AWSAccessKeyId' => ACCESS_IDENTIFIER, 'SignatureMethod' => 'HmacSHA256', 'SignatureVersion' => 2, 'Timestamp' => Time.now.iso8601, 'Version' => '2010-03-31', 'Token' => 'DEVICE TOKEN', 'PlatformApplicationArn' => 'arn:aws:sns:ap-northeast-1:xxxx:app/APNS/hoge' }
  42. params = { 'Action' => 'CreatePlatformEndpoint', 'AWSAccessKeyId' => ACCESS_IDENTIFIER, 'SignatureMethod' => 'HmacSHA256', 'SignatureVersion' => 2, 'Timestamp' => Time.now.iso8601, 'Version' => '2010-03-31', 'CustomUserData' => 'userdata', 'Token' => 'DEVICE TOKEN', 'PlatformApplicationArn' => 'arn:aws:sns:ap-northeast-1:xxxx:app/APNS/hoge' } CreatePlatformEndpoint params = { 'Action' => 'CreatePlatformEndpoint', 'AWSAccessKeyId' => ACCESS_IDENTIFIER, 'SignatureMethod' => 'HmacSHA256', 'SignatureVersion' => 2, 'Timestamp' => Time.now.iso8601, 'Version' => '2010-03-31', 'Token' => 'DEVICE TOKEN', 'PlatformApplicationArn' => 'arn:aws:sns:ap-northeast-1:xxxx:app/APNS/hoge' } 成功して同一Endpoitn Arnがかえる♪
  43. params = { 'Action' => 'CreatePlatformEndpoint', 'AWSAccessKeyId' => ACCESS_IDENTIFIER, 'SignatureMethod' => 'HmacSHA256', 'SignatureVersion' => 2, 'Timestamp' => Time.now.iso8601, 'Version' => '2010-03-31', 'CustomUserData' => 'userdata', 'Token' => 'DEVICE TOKEN', 'PlatformApplicationArn' => 'arn:aws:sns:ap-northeast-1:xxxx:app/APNS/hoge' } CreatePlatformEndpoint params = { 'Action' => 'CreatePlatformEndpoint', 'AWSAccessKeyId' => ACCESS_IDENTIFIER, 'SignatureMethod' => 'HmacSHA256', 'SignatureVersion' => 2, 'Timestamp' => Time.now.iso8601, 'Version' => '2010-03-31', 'Token' => 'DEVICE TOKEN', 'PlatformApplicationArn' => 'arn:aws:sns:ap-northeast-1:xxxx:app/APNS/hoge' } 成功して同一Endpoitn Arnがかえる♪ エラー!
  44. <ErrorResponse xmlns="http://sns.amazonaws.com/doc/2010-03-31/&quot;&gt;&lt;Error&gt;&lt;Type&gt;Sender&lt;/Type&gt;&lt;Code&gt;InvalidParameter&lt;/Code&gt;&lt;Message&gt;Invalid parameter: Token Reason: Endpoint arn:aws:sns:ap- northeast-1:xxxx:endpoint/APNS/hoge/xxxx0e-aa2a-35xxe-xxx-xxxxxxxx already exists with the same Token, but different attributes.</Message> </Error> <RequestId>xxx-yy5-5zz8-b1zz-4aaaa7ccvvvvv</RequestId> </ErrorResponse>
  45. もし SNSに登録済み Device token を 登録す場合
  46. params = { 'Action' => 'CreatePlatformEndpoint', 'AWSAccessKeyId' => ACCESS_IDENTIFIER, 'SignatureMethod' => 'HmacSHA256', 'SignatureVersion' => 2, 'Timestamp' => Time.now.iso8601, 'Version' => '2010-03-31', 'CustomUserData' => 'userdata', 'Token' => 'DEVICE TOKEN', 'PlatformApplicationArn' => 'arn:aws:sns:ap-northeast-1:xxxx:app/APNS/hoge' } CreatePlatformEndpoint params = { 'Action' => 'CreatePlatformEndpoint', 'AWSAccessKeyId' => ACCESS_IDENTIFIER, 'SignatureMethod' => 'HmacSHA256', 'SignatureVersion' => 2, 'Timestamp' => Time.now.iso8601, 'Version' => '2010-03-31', 'Token' => 'DEVICE TOKEN', 'PlatformApplicationArn' => 'arn:aws:sns:ap-northeast-1:xxxx:app/APNS/hoge' }
  47. Custom User Dataまで 同一にする必要がある もともとつけていないなら気にする必要がない Management Consoleから開発者を識別させるためとかそうい う用途で使ってるとハマるかも
  48. 消したとしてもダメ! 一度User Dataを登録するとフィー ルドが内部的に生成されてしまう
  49. params = { 'Action' => 'CreatePlatformEndpoint', 'AWSAccessKeyId' => ACCESS_IDENTIFIER, 'SignatureMethod' => 'HmacSHA256', 'SignatureVersion' => 2, 'Timestamp' => Time.now.iso8601, 'Version' => '2010-03-31', 'CustomUserData' => '', 'Token' => 'DEVICE TOKEN', 'PlatformApplicationArn' => 'arn:aws:sns:ap-northeast-1:xxxx:app/APNS/hoge' }
  50. Metrics
  51. Conclusion
  52. 格安で安定してPush通知を送りたい・複 数Platformへ同報したいときに便利 AWS内で完結する Management ConsoleやAPIはまだ使いづら い 全device一斉通知やメタデータに応じ たPush通知は自前で管理しないといけ ない 仕様まわりはよく把握しておく
  53. Thank you!!

このスライドを共有する

  • このエントリーをはてなブックマークに追加

関連スライド

おすすめスライド

↑