lib/j7w1/sns_push_client.rb in j7w1-0.0.17 vs lib/j7w1/sns_push_client.rb in j7w1-0.0.18

- old
+ new

@@ -2,20 +2,23 @@ module SNSPushClient def create_sns_client(configuration = J7W1.configuration) AWS::SNS.new configuration.account end + APNS_MAX_MESSAGE_SIZE = 40 + APS_TABLE = { message: :alert, badge: :badge, sound: :sound, }.freeze ANDROID_TABLE = { message: :message, badge: :badge, sound: :sound, + data: :data, }.freeze def create_ios_application(name, certs, private_key, options) sandbox = !(options[:sandbox] == false) configuration = options[:sns_configuration] || J7W1.configuration @@ -84,19 +87,20 @@ return unless endpoint_arn && platform message = options[:message] badge = options[:badge] sound = options[:sound] + data = options[:data] sns_configuration = options[:sns_configuration] sns_client = options[:sns_client] message_value = {} 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) + payload = payload_for(message_value, data, platform) sns_client ||= create_sns_client(sns_configuration || J7W1.configuration) client = sns_client.client enabled = (client.get_endpoint_attributes(endpoint_arn: endpoint_arn)[:attributes]['Enabled'] == 'true') @@ -110,26 +114,36 @@ message_structure: 'json', ) end private - def payload_for(message_value, platform) + def payload_for(message_value, data, platform) case platform.to_sym when :ios - ios_payload_for(message_value) + ios_payload_for(message_value, data) when :android - android_payload_for(message_value) + android_payload_for(message_value, data) end end - def ios_payload_for(message_value) + def ios_payload_for(message_value, data) prefix = J7W1.configuration.ios_endpoint.sandbox? ? :APNS_SANDBOX : :APNS - {prefix => {aps: message_content_with_table(message_value, APS_TABLE)}.to_json} + if message_value[:message] && message_value[:message] > APNS_MAX_MESSAGE_SIZE + message_value[:message] = message_value[:message][0...(APNS_MAX_MESSAGE_SIZE - 3)] + '...' + end + + content = { + aps: message_content_with_table(message_value, APS_TABLE), + } + content.merge! data: data if data + + {prefix => content.to_json} end - def android_payload_for(message_value) + def android_payload_for(message_value, data) + message_value.merge!(data: data) { GCM: { data: message_content_with_table(message_value, ANDROID_TABLE) }.to_json }