LoginSignup
20
20

More than 5 years have passed since last update.

AWS SNSからiOSにPush通知を送る(デバイストークン指定)

Posted at

端末のデバイストークンをサーバサイドで保持していれば、下記にて個別に通知できる。

# @access_key_id、@secret_access_key、@regionは、適切にセットする。
# Pushしたい端末のデバイストークンを@device_tokenにセットする。

# 送信データ
data = {"aps"=>{
          "alert"=>"メッセージ",
          "badge"=>badge_cnt,
          "sound"=>"default"}
        }.to_json
message = {"APNS"=>data}.to_json

# エンドポイント作成
sns = AWS::SNS.new(:access_key_id => @access_key_id,
                   :secret_access_key => @secret_access_key,
                   :region => @region)
client = sns.client
response = client.create_platform_endpoint(
                      platform_application_arn: @application_arn,
                      token: @device_token)
endpoint_arn = response[:endpoint_arn]

# 送信
if client.publish(target_arn: endpoint_arn,
                  message: message,
                  message_structure: 'json')
  Rails.logger.info "publish success"
else
  Rails.logger.error "publish failed"
end
20
20
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
20
20