Class: Pushkin::SendPushService

Inherits:
Object
  • Object
show all
Defined in:
app/services/pushkin/send_push_service.rb

Instance Method Summary collapse

Constructor Details

#initialize(notification_id) ⇒ SendPushService



7
8
9
# File 'app/services/pushkin/send_push_service.rb', line 7

def initialize(notification_id)
  @notification = Notification.find(notification_id)
end

Instance Method Details

#callObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'app/services/pushkin/send_push_service.rb', line 11

def call
  # Получим все токены, на которые нужно отправлять уведомления.
  all_tokens = @notification.tokens_provider.get_tokens.to_a

  # Создаем список помощников, которые отфильтруют уведомления по платформам.
  # Они так же предоставляют соответствующие сервисы для отправки уведомлений.
  token_filters = [Pushkin::TokenFilters::IosTokenFilter.new,
                   Pushkin::TokenFilters::AndroidTokenFilter.new,
                   Pushkin::TokenFilters::WebTokenFilter.new]

  # Отправка на конкретные платформы
  push_sending_results = token_filters.map do |token_filter|
    tokens = token_filter.filter_tokens(all_tokens)
    token_filter.get_sending_service(tokens, @notification.payload).call if tokens.present?
  end

   # Актуализация информации об активности токенов
  push_sending_results.compact.each do |push_sending_result|
    push_sending_result.update_attributes(notification_id: @notification.id)
    push_sending_result.token_results.joins(:token).invalid.each do |token_result|
      token_result.token.update_attributes(is_active: false)
    end
  end

  @notification.update_attributes(finished_at: DateTime.now)
end