decision-labs / fcm

Ruby bindings to Firebase Cloud Messaging (FCM) for Android, iOS or Web
MIT License
510 stars 153 forks source link

depricated authorization for topic management #122

Open aap17 opened 2 weeks ago

aap17 commented 2 weeks ago

it seems the latest library version works with legacy API for requests with topic

  def topic_subscription(topic, registration_id)
    for_uri(INSTANCE_ID_API) do |connection|
      response = connection.post("/iid/v1/#{registration_id}/rel/topics/#{topic}")
      build_response(response)
    end
  end

...

  connection = ::Faraday.new(
      url: uri,
      request: { timeout: DEFAULT_TIMEOUT }
    ) do |faraday|
      faraday.adapter Faraday.default_adapter
      faraday.headers["Content-Type"] = "application/json"
      faraday.headers['Authorization'] = "key=#{@api_key}"
      extra_headers.each do |key, value|
        faraday.headers[key] = value
      end
    end

No Bearer token inside connection, the request fails with error "Authentication using server key is depricated..."

But push messages works perfect with direct sending, it was fixed with V1 Bearer auth type:

  def send_notification_v1(message)
    return if @project_name.empty?

    post_body = { 'message': message }
    extra_headers = {
      'Authorization' => "Bearer #{jwt_token}"
    }
    for_uri(BASE_URI_V1, extra_headers) do |connection|
      response = connection.post(
        "#{@project_name}/messages:send", post_body.to_json
      )
      build_response(response)
    end
  end
nuzelac commented 2 weeks ago

I had to also add access_token_auth header for topic subscription.

From https://developers.google.com/instance-id/reference/server#create_relationship_maps_for_app_instances

https://iid.googleapis.com/iid/v1/nKctODamlM4:CKrh_PC8kIb7O...clJONHoA/rel/topics/movies Content-Type:application/json Content-Length: 0 Authorization: Bearer ya29.ElqKBGN2Ri_Uz...HnS_uNreA access_token_auth: true

aap17 commented 1 week ago

I have fixed it in my fork. https://github.com/aap17/fcm_v1

I would be happy if someone will update it here

robink commented 1 week ago

Also migrated send_to_topic_condition https://github.com/aap17/fcm_v1/pull/1