lib/j7w1/sns_push_client.rb in j7w1-0.0.13 vs lib/j7w1/sns_push_client.rb in j7w1-0.0.14

- old
+ new

@@ -2,10 +2,22 @@ module SNSPushClient def create_sns_client(configuration = J7W1.configuration) AWS::SNS.new configuration.account end + APS_TABLE = { + message: :alert, + badge: :badge, + sound: :sound, + }.freeze + + ANDROID_TABLE = { + message: :message, + badge: :badge, + sound: :sound, + }.freeze + def create_ios_application(name, certs, private_key, options) sandbox = !(options[:sandbox] == false) configuration = options[:sns_configuration] || J7W1.configuration private_key = content_from private_key @@ -37,13 +49,21 @@ sns_client = options[:sns_client] sns_client ||= create_sns_client(sns_configuration || J7W1.configuration) sns_config = J7W1.configuration - app_arn = platform == :ios ? sns_config.ios_endpoint.arn : - sns_config.android_endpoint.arn + app_arn = + case platform + when :ios + sns_config.ios_endpoint.arn + when :android + sns_config.android_endpoint.arn + else + + end + endpoint = sns_client.client.create_platform_endpoint( platform_application_arn: app_arn, token: device_identifier, custom_user_data: custom_user_data @@ -68,11 +88,11 @@ sound = options[:sound] sns_configuration = options[:sns_configuration] sns_client = options[:sns_client] message_value = {} - message_value.merge!(alert: message) unless message.blank? + message_value.merge!(message: message) unless message.blank? message_value.merge!(badge: badge) unless badge.blank? message_value.merge!(sound: sound) unless sound.blank? payload = payload_for(message_value, platform) @@ -84,25 +104,37 @@ ) end private def payload_for(message_value, platform) - case platform - when 'ios' + case platform.to_sym + when :ios ios_payload_for(message_value) - when 'android' + when :android android_payload_for(message_value) end end def ios_payload_for(message_value) prefix = J7W1.configuration.ios_endpoint.sandbox? ? :APNS_SANDBOX : :APNS - {prefix => {aps: message_value}.to_json} + + {prefix => {aps: message_content_with_table(message_value, APS_TABLE)}.to_json} end def android_payload_for(message_value) - # TODO Android Push Implementation + { + gcm: { + data: message_content_with_table(message_value, ANDROID_TABLE) + }.to_json + } + end + + def message_content_with_table(content, table) + table.keys.reduce({}) do |h, key| + h[table[key]] = content[key] + h + end end def content_from(argument) case argument when IO