Help us understand the problem. What is going on with this article?

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

More than 5 years have passed since last update.

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")
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away