class Wor::Push::Notifications::Aws::PushNotifications

Public Class Methods

add_token(user, device_token, device_type) click to toggle source
# File lib/wor/push/notifications/aws/push_notifications.rb, line 12
def add_token(user, device_token, device_type)
  PushNotificationsValidator.new(user, device_token, device_type).validate_add_token
  device_token = device_token.to_s.gsub(/\s+/, '')
  return true if user.device_tokens.key?(device_token)
  endpoint = sns.create_platform_endpoint(
    platform_application_arn: app_arn(device_type), token: device_token
  )
  user.device_tokens[device_token] = { 'device_type' => device_type,
                                       'endpoint_arn' => endpoint[:endpoint_arn] }
  user.save
end
delete_token(user, device_token) click to toggle source
# File lib/wor/push/notifications/aws/push_notifications.rb, line 24
def delete_token(user, device_token)
  PushNotificationsValidator.new(user).validate_delete_token
  device_token = device_token.to_s.gsub(/\s+/, '')
  return true unless user.device_tokens.key?(device_token)
  sns.delete_endpoint(endpoint_arn: user.device_tokens[device_token]['endpoint_arn'])
  user.device_tokens.delete(device_token)
  user.save
end
send_message(user, message_content) click to toggle source
# File lib/wor/push/notifications/aws/push_notifications.rb, line 33
def send_message(user, message_content)
  PushNotificationsValidator.new(user).validate_model
  message_content = PushNotificationsValidator.validate_message_content(
    message_content
  )
  send_notifications_to_user(user, message_content)
end

Private Class Methods

app_arn(device_type) click to toggle source
# File lib/wor/push/notifications/aws/push_notifications.rb, line 72
def app_arn(device_type)
  app_arn_for_device[device_type.to_sym][:arn]
end
app_arn_for_device() click to toggle source
# File lib/wor/push/notifications/aws/push_notifications.rb, line 76
def app_arn_for_device
  {
    ios: {
      arn: Wor::Push::Notifications::Aws.aws_ios_arn,
      json_builder: IosPushJsonBuilder
    },
    android: {
      arn: Wor::Push::Notifications::Aws.aws_android_arn,
      json_builder: AndroidPushJsonBuilder
    }
  }
end
prepare_message(device_type, message_content) click to toggle source
# File lib/wor/push/notifications/aws/push_notifications.rb, line 62
def prepare_message(device_type, message_content)
  app_arn_for_device[device_type.to_sym][:json_builder].build_json(message_content)
end
send_notifications_to_devices(user, message_content) click to toggle source
# File lib/wor/push/notifications/aws/push_notifications.rb, line 49
def send_notifications_to_devices(user, message_content)
  user.device_tokens.each do |device_token, token|
    message = prepare_message(token['device_type'], message_content)
    begin
      sns.publish(target_arn: token['endpoint_arn'], message: message.to_json,
                  message_structure: 'json')
    rescue
      user.device_tokens.delete(device_token)
    end
  end
  user.save if user.device_tokens_changed?
end
send_notifications_to_user(user, message_content) click to toggle source
# File lib/wor/push/notifications/aws/push_notifications.rb, line 43
def send_notifications_to_user(user, message_content)
  return false if user.device_tokens.values.empty?
  send_notifications_to_devices(user, message_content)
  true
end
sns() click to toggle source
# File lib/wor/push/notifications/aws/push_notifications.rb, line 66
def sns
  @sns_client ||= ::Aws::SNS::Client.new(
    region: Wor::Push::Notifications::Aws.aws_region
  )
end