The code powering m.abunchtell.com https://m.abunchtell.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

29 lines
874 B

  1. # frozen_string_literal: true
  2. class SendPushNotificationService < BaseService
  3. def call(notification)
  4. return if ENV['FCM_API_KEY'].blank?
  5. devices = Device.where(account: notification.account).pluck(:registration_id)
  6. fcm = FCM.new(ENV['FCM_API_KEY'])
  7. response = fcm.send(devices, data: { notification_id: notification.id }, collapse_key: :notifications, priority: :high)
  8. handle_response(response)
  9. end
  10. private
  11. def handle_response(response)
  12. update_canonical_ids(response[:canonical_ids]) if response[:canonical_ids]
  13. remove_bad_ids(response[:not_registered_ids]) if response[:not_registered_ids]
  14. end
  15. def update_canonical_ids(ids)
  16. ids.each { |pair| Device.find_by(registration_id: pair[:old]).update(registration_id: pair[:new]) }
  17. end
  18. def remove_bad_ids(bad_ids)
  19. Device.where(registration_id: bad_ids).delete_all
  20. end
  21. end