Class: SimpleFCM::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/simple_fcm/client.rb

Instance Method Summary collapse

Constructor Details

#initialize(config = nil) {|conn| ... } ⇒ Client

Returns a new instance of Client.

Parameters:

  • config (string, nil) (defaults to: nil)

    Either the path to a service account json config file, the contents of that file as a string, or nil to load config options from environment configs.

Yields:

  • (conn)

    Yields into the connection building process

Yield Parameters:

  • The (Faraday::Connection)

    backend connection used for requests



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/simple_fcm/client.rb', line 22

def initialize(config = nil)
  @credentials = build_credentials(config)

  @client = Faraday::Connection.new(ENDPOINT) do |conn|
    conn.request :simple_fcm_auth, @credentials
    conn.request :json
    conn.response :json, parser_options: {symbolize_names: true}

    if block_given?
      yield conn
    end
  end
end

Instance Method Details

#push(message) ⇒ string

Send a push notification to a device or topic through Firebase Cloud Messaging.

Parameters:

  • message (Hash)

    Message payload

Returns:

  • (string)

    Message ID returned by the API

Raises:

See Also:



45
46
47
48
49
50
51
52
53
# File 'lib/simple_fcm/client.rb', line 45

def push(message)
  resp = @client.post("/v1/projects/#{project_id}/messages:send", message)

  if !resp.success?
    raise ::SimpleFCM::APIError, resp
  end

  resp.body[:name]
end