気軽なsns mobile push の話
Post on 08-Sep-2014
33 Views
Preview:
DESCRIPTION
TRANSCRIPT
気軽なSNS Mobile Push の話
星野 豊 (@con_mame)
クックパッド株式会社 インフラストラクチャー部
AWS / MySQL / Redshift / DataStore etc...
http://d.conma.me/
http://facebook.com/conmame
Push Notification
iPhone / iPad / Android /
Kindle とかに通知を送るあれ
APNS / GCM / ADM
プラットホーム毎に
仕様が違う
iOS / Android
iOS / Android / WP8 /
Win Store
iOS / Android / WP8 / Win
Store / Kindle / BlackBerry
iOS / Android / Kindle
SNS Mobile Push
100万Push当たり $1
(Push依頼 $0.5 + Push配信 $0.5)
Topicを作成することにより異なるプラット
ホームに同時に配信可能
安定してる
AWS内で完結する
証明書などの認証情報をSNSに登録する
APNS / APNS_SANDBOX / GCM / ADM
Tokenの無効・変更などの処理を自動で行う
Feedback通知も可能
無効なEndpoint Arnは送信対象から外
して余計なコストやパフォーマンス定
価を避ける (e.g. アプリケーションが
アンインストールされた)
遅延あり (e.g. APNS feedback service)
~数日
Device token
ことば
Device token: アプリケーションを識別するための各Platformが発行するもの
Endpoint Arn: Device Tokenから生成され、SNSで各deviceを識別するもの (SNSの操作ではこ
れを使う)
Application Arn: Pushをするアプリケーションの単位
Device tokenのSNSへの登録方法
Device -> API
Device -> SNS
Token埋め込み
Token Vending Machine
SNSではEndpoint ArnとApplication Arnを
使用
Device to API
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
Device to SNS
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
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
AWSアクセス情報をアプリケーションに埋め込む必要があ
る
Access Key / Secretの変更のためにはアプリケー
ションの審査提出が必要
全ユーザが直ぐにUpdateするとは限らない
アクセス情報が漏れた場合などを考えてIAMで権限を
絞っておく (e.g. Create Endpoint)
Token Vending Machine
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
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
Token Vending Machine (TVM)
AWS Security Token Service (STS)
STSに必要な権限だけをつけたIAM
(Role)をつけたアプリケーション(イン
スタンス)がAWSのIdentity / Access
Management Serviceにアクセスして必
要な権限をリクエスト
一時的なアクセス情報を発行
標準では1時間
アクセス情報が漏れた場合でも、その
Keyなどは標準では1時間で使えなくなる
Application Arnも一緒に返すこと推薦
Application Arn自体はSNS Mobile
Push Applicationを識別するだけ
arn:aws:sns:ap-
northeast-1:xxxxxxxxxxx:app/APNS/
ApplicationName
アクセス情報が漏れた場合でも、その
Keyなどは標準では1時間で使えなくなる
Application Arnも一緒に返すこと推薦
Application Arn自体はSNS Mobile
Push Applicationを識別するだけ
arn:aws:sns:ap-
northeast-1:xxxxxxxxxxx:app/APNS/
ApplicationName
AWS Account ID
Push
# アクセス情報の設定は省略
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')
グループにPushする場合はSNS Topic
e.g. 年代・カテゴリ・機能・ユーザ
ステータス...
TopicはApplication Arnをまたげる
複数のEndpoint Arnをまとめられる
Platformに関係なく送信可能
{
"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>\" } }"
}
Topicの制限 (default)
3,000 topics / account
10,000 subscribers / topic
Batchor
App
1 Get Endpoint Arn
and
user status
Batch
feedbak
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
Batchor
App
1 Get Endpoint Arn
and
user status
Batch
feedbak
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
metadata
metadata
Active?
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"}
<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>
GetEndpointAttributes
いろいろ
各Endpoint arnにはUser Dataを付与出来る
今のところPushを行う時の抽出用メタ
データとして使えない
Topicの制限
1つのTopic(カテゴリ)に10,000 device以
上の場合はtopicを分割する必要がある
subscriberの属性が変更されたらTopicを
移動させないと…
属性を自前で管理して各deviceに個別Push
いろいろ。。。
とういうものがあって
Device token: 11111...
をCustom User Dataなしで登
録しようとする場合
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'
}
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がかえる♪
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がかえる♪
エラー!
<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: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>
もし
SNSに登録済み
Device tokenを
登録す場合
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'
}
Custom User Dataまで
同一にする必要がある
もともとつけていないなら気にする必要がない
Management Consoleから開発者を識別させるためとかそうい
う用途で使ってるとハマるかも
消したとしてもダメ!
一度User Dataを登録するとフィー
ルドが内部的に生成されてしまう
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'
}
Metrics
Conclusion
格安で安定してPush通知を送りたい・複
数Platformへ同報したいときに便利
AWS内で完結する
Management ConsoleやAPIはまだ使いづら
い
全device一斉通知やメタデータに応じ
たPush通知は自前で管理しないといけ
ない
仕様まわりはよく把握しておく
Thank you!!
top related