Class: Wor::Push::Notifications::Aws::PushNotifications

Inherits:
Object
  • Object
show all
Defined in:
lib/wor/push/notifications/aws/push_notifications.rb

Class Method Summary collapse

Class Method Details

.add_token(user, device_token, device_type) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
# 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) ⇒ Object



24
25
26
27
28
29
30
31
# 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) ⇒ Object



33
34
35
36
37
# File 'lib/wor/push/notifications/aws/push_notifications.rb', line 33

def send_message(user, message_content)
  PushNotificationsValidator.new(user).validate_send_message(message_content)
  message_content = badge_check(message_content)
  send_notifications_to_user(user, message_content)
end