yuki24 / pushing

Finally, push notification framework that does not hurt. currently supports Android (FCM) and iOS (APNs)
MIT License
46 stars 6 forks source link

User should be able to observe a response with an observer #6

Closed yuki24 closed 7 years ago

yuki24 commented 7 years ago

FCM could return canonical_ids with a successful response. This means rescue_from ... wouldn't be helpful when having to capture them and update registration tokens. User should be able to update registration tokens using an observer:

# app/observers/fcm_token_handler.rb
class FcmTokenHandler
  # Using an instance method allows for injecting dependencies if needed later.
  def delivered_notification(payload, fcm_response)
    # Make sure the response ojbect contains `canonical_ids`
    return if !fcm_response.respond_to?(:json)

    canonical_ids = fcm_response.json[:canonical_ids] || []
    canonical_ids.each do |canonical_id|
      # Update registration tokens accordingly
    end
  end
end
# app/notifiers/application_notifier.rb
class ApplicationNotifier < Fourseam::Base
  register_observer FcmTokenHandler.new

  ...
end