LoginSignup
16
17

More than 5 years have passed since last update.

Amazon SNS の Mobile Push の送信側のコード(とりあえず動く版)

Last updated at Posted at 2013-09-06

AWS のサンプル で Push 側のコードサンプルが Java 版しかないので、参考にして Ruby で書いてみた版。

エラー処理などを追加して、後ほど完成させたい。
Android クライアントはサンプルをそのまま利用した。

参考: Amazon SNS の新機能「Mobile Push」を Android で使ってみた

require 'aws-sdk'

# AWS.config(access_key_id: 'xxx', secret_access_key: 'yyy', region: 'us-east-1')

class SnsAndroidPush
  def initialize(sns)
    @client = sns.client
  end

  def notify(endpoint_arn, message)
    @client.publish(
      target_arn: endpoint_arn,
      message: message
    )
  end

  def create_application(name)
    res = @client.create_platform_application(
      name: name,
      platform: 'GCM',
      attributes: {
        'PlatformPrincipal' => '',
        'PlatformCredential' => ENV['GOOGLE_PLAY_API_KEY']
      }
    )
    res[:platform_application_arn]
  end

  def create_endpoint(application_arn, registration_id, user_data = '')
    res = @client.create_platform_endpoint(
      platform_application_arn: application_arn,
      token: registration_id,
      custom_user_data: user_data
    )
    res[:endpoint_arn]
  end
end

registration_id = 'zzz'

sns = AWS::SNS.new
sns_push = SnsAndroidPush.new(sns)
app_arn = sns_push.create_application('testtest')
endpoint_arn = sns_push.create_endpoint(app_arn, registration_id)
sns_push.notify(endpoint_arn, "#{Time.now} test")
16
17
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
16
17