Class: Firebase::Admin::Messaging::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/firebase/admin/messaging/client.rb

Overview

A client for communicating with the Firebase Cloud Messaging service.

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Client

Returns a new instance of Client.



6
7
8
9
10
# File 'lib/firebase/admin/messaging/client.rb', line 6

def initialize(app)
  @project_id = app.project_id
  @http_client = Firebase::Admin::Internal::HTTPClient.new(credentials: app.credentials)
  @message_encoder = MessageEncoder.new
end

Instance Method Details

#send_all(messages, dry_run: false) ⇒ BatchResponse

Sends the given list of messages via Firebase Cloud Messaging (FCM) as a single batch.

If the ‘dry_run` flag is set, the messages will not be actually delivered to the recipients. Instead FCM performs all the usual validations, and emulates the send operation.

Parameters:

  • messages (Array<Message>)

    An array of messages to send.

  • dry_run (Boolean) (defaults to: false)

    A flag indicating whether to run the operation in dry run mode.

Returns:

  • (BatchResponse)

    A batch response.

Raises:

  • (NotImplementedError)


41
42
43
# File 'lib/firebase/admin/messaging/client.rb', line 41

def send_all(messages, dry_run: false)
  raise NotImplementedError
end

#send_multicast(multicast_message, dry_run: false) ⇒ BatchResponse

Sends the given multicast message to all tokens via Firebase Cloud Messaging (FCM).

If the ‘dry_run` flag is set, the message will not be actually delivered to the recipients. Instead FCM performs all the usual validations, and emulates the send operation.

Parameters:

  • multicast_message (MulticastMessage)

    A multicast message to send.

  • dry_run (Boolean) (defaults to: false)

    A flag indicating whether to run the operation in dry run mode.

Returns:

  • (BatchResponse)

    A batch response.



54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/firebase/admin/messaging/client.rb', line 54

def send_multicast(multicast_message, dry_run: false)
  messages = multicast_message.tokens.map do |token|
    Message.new(
      token: token,
      data: multicast_message.data,
      notification: multicast_message.notification,
      android: multicast_message.android,
      apns: multicast_message.apns,
      fcm_options: multicast_message.fcm_options
    )
  end
  send_all(messages, dry_run: dry_run)
end

#send_one(message, dry_run: false) ⇒ String

Sends a message via Firebase Cloud Messaging (FCM).

If the ‘dry_run` flag is set, the message will not be actually delivered to the recipients. Instead FCM performs all the usual validations, and emulates the send operation.

Parameters:

  • message (Message)

    A message to send.

  • dry_run (Boolean) (defaults to: false)

    A flag indicating whether to run the operation in dry run mode.

Returns:

  • (String)

    A message id that uniquely identifies the message.



21
22
23
24
25
26
27
28
29
30
# File 'lib/firebase/admin/messaging/client.rb', line 21

def send_one(message, dry_run: false)
  body = {
    validate_only: dry_run,
    message: @message_encoder.encode(message)
  }
  res = @http_client.post(send_url, body, FCM_HEADERS)
  res.body["name"]
rescue Faraday::Error => e
  raise parse_fcm_error(e)
end

#subscribe_to_topic(tokens, topic) ⇒ TopicManagementResponse

Subscribes a list of registration tokens to an FCM topic.

Parameters:

  • tokens (Array<String>, String)

    An array of device registration tokens (max 1000).

  • topic (String)

    Name of the topic to subscribe to. May contain the ‘/topics` prefix.

Returns:



74
75
76
# File 'lib/firebase/admin/messaging/client.rb', line 74

def subscribe_to_topic(tokens, topic)
  make_topic_mgmt_request(tokens, topic, "batchAdd")
end

#unsubscribe_from_topic(tokens, topic) ⇒ TopicManagementResponse

Unsubscribes a list of registration tokens from an FCM topic.

Parameters:

  • tokens (Array<String>, String)

    An array of device registration tokens (max 1000).

  • topic (String)

    Name of the topic to unsubscribe from. May contain the ‘/topics` prefix.

Returns:



84
85
86
# File 'lib/firebase/admin/messaging/client.rb', line 84

def unsubscribe_from_topic(tokens, topic)
  make_topic_mgmt_request(tokens, topic, "batchRemove")
end