LoginSignup
29

More than 5 years have passed since last update.

RubyスクリプトとAWSのSNSで複数デバイスへのpush通知

Last updated at Posted at 2014-11-28

これが出来ると何が嬉しいの?

一括メッセージ設定、一括送信の自動化が出来ます!

前提条件

Ruby環境

gem install aws-sdk-coreを実施済みであること

iOS環境

AWS SNSでアプリを作成済みであること
AWS SNSへエンドポイントを自動登録出来る実装が完了していること
AWS SDKを導入済みであること

上記の内容は以下のサイトが参考になります!

概要図

スクリーンショット 2014-11-28 19.01.58.png

・流れ1:アプリ起動時にAWS SNSのアプリ側にデバイストークンを登録
・流れ2:対象のSNSからエンドポイントを取得
・流れ3:作成済みのTopicに取得したエンドポイントをSubscribeする
・流れ4:アプリに対してメッセージをpushし、即時に通知

Rubyスクリプト

sample.rb

require 'aws-sdk'

sns = AWS::SNS.new(
  :access_key_id => 'YOUR_ACCESS_KEY',
  :secret_access_key => 'YOUR_SECRET_ACCESS_KEY',
  :region => 'us-east-1')
client = sns.client

# 対象のSNSからエンドポイントを取得
num = client.list_endpoints_by_platform_application(platform_application_arn: 'arn:aws:sns:us-east-1:************:app/APNS_SANDBOX/DevelopAppPush')

# 作成済みのTopicに取得したエンドポイントをSubscribeする
num.endpoints.each{|value|
  client.subscribe(
    topic_arn: 'arn:aws:sns:us-east-1:************:test_topic',
    protocol: 'application',
    endpoint: value.endpoint_arn)
}

# 起動していないアプリに対してメッセージをpush通知
response = client.publish(
  target_arn: 'arn:aws:sns:us-east-1:************:test_topic',
  message: 'push通知のテスト成功'
  )



備考

AWSドキュメント
http://docs.aws.amazon.com/AWSRubySDK/latest/AWS/SNS/Client.html#create_platform_endpoint-instance_method

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
29