Advertisement

気軽なSNS Mobile Push の話

network/server/cloud engineer at Internet
Nov. 1, 2013
Advertisement

More Related Content

Slideshows for you(20)

Similar to 気軽なSNS Mobile Push の話(20)

Advertisement
Advertisement

気軽な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 / Kindle / BlackBerry iOS / Android / WP8 / Win Store 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. 1 Token request 3 Token send 2 Token response API 4 Token create API 5 Endpoint Arn 6 Save Endpoint Arn token DB M token DB S
  16. Device to SNS
  17. 1 Token request 5 Endpoint Arn 2 Token response 3 Token send API API 4 Endpoint Arn 6 Save Endpoint Arn token DB M token DB S
  18. 1 Token request 5 Endpoint Arn 2 Token response Application Arn AWS Access Key 3 Token send AWS Secret Key API API 4 Endpoint Arn 6 Save Endpoint Arn token DB M token DB S
  19. AWSアクセス情報をアプリケーションに埋め込む必要があ る Access Key / Secretの変更のためにはアプリケー ションの審査提出が必要 全ユーザが直ぐにUpdateするとは限らない アクセス情報が漏れた場合などを考えてIAMで権限を 絞っておく (e.g. Create Endpoint)
  20. Token Vending Machine
  21. 1 Token request 6 Endpoint Arn 2 Token response 3 Get Temporary credentials API API TVM 4 Token send 5 Endpoint Arn 7 Save Endpoint Arn token DB M token DB S http://amzn.to/16DN8Rb
  22. 1 Token request 6 Endpoint Arn 2 Token response 3 Get Temporary credentials API API 7 Save Endpoint Arn token DB M token DB S TVM 4 Token send 5 Endpoint Arn 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:apnortheast-1:xxxxxxxxxxx:app/APNS/ ApplicationName
  25. アクセス情報が漏れた場合でも、その Keyなどは標準では1時間で使えなくなる Application Arnも一緒に返すこと推薦 Application Arn自体はSNS Mobile Push Applicationを識別するだけ AWS Account ID arn:aws:sns:apnortheast-1:xxxxxxxxxxx:app/APNS/ ApplicationName
  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. Retry Push Architecture Feedback Notification feedb ak 6 Push to platform 2 Manage Tokens Generate message and put queue Batch or App 1 Get Endpoint Arn and user status 5 Publish 3 dequeue Batch 4 Change Message Status
  32. Simple Push Architecture Feedback Notification feedb ak 5 Push to platform Manage Tokens 4 Publish Batch or App 1 Get Endpoint Arn and user status 2 Make Push information file Batch 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-xxxxbxx4-8cxxxxxx93","EventType":"EndpointUpdated","Resource":"arn:aws: sns:ap-northeast-1:xxxxx:app/APNS/ ApplicatonName","Service":"SNS","Time":"2013-10-27T20:44:12.545Z"}
  36. GetEndpointAttributes <GetEndpointAttributesResponse xmlns="http://sns.amazonaws.com/doc/2010-03-31/"> <GetEndpointAttributesResult> <Attributes> <entry> <key>Enabled</key> <value>false</value> </entry> <entry> <key>Token</key> <value>device token</value> </entry> </Attributes> </GetEndpointAttributesResult> <ResponseMetadata> <RequestId>4dbxxxxx-aaaa-vvvv-ddddd-xxxxxx</RequestId> </ResponseMetadata> </GetEndpointAttributesResponse>
  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. CreatePlatformEndpoint 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' } 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. CreatePlatformEndpoint 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' } 成功して同一Endpoitn Arnがかえる♪ 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' }
  43. CreatePlatformEndpoint 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' } 成功して同一Endpoitn Arnがかえる♪ 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' } エラー!
  44. <ErrorResponse xmlns="http://sns.amazonaws.com/doc/2010-03-31/"> <Error> <Type>Sender</Type> <Code>InvalidParameter</Code> <Message>Invalid parameter: Token Reason: Endpoint arn:aws:sns:apnortheast-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. CreatePlatformEndpoint 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' } 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!!
Advertisement