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.



8
9
10
11
12
13
14
15
# File 'lib/firebase/admin/messaging/client.rb', line 8

def initialize(app)
  @project_id = app.project_id
  @project_path = "projects/#{app.project_id}"
  @message_encoder = MessageEncoder.new
  @http_client = Firebase::Admin::Internal::HTTPClient.new(credentials: app.credentials)
  @service = Google::Apis::FcmV1::FirebaseCloudMessagingService.new
  @service.authorization = app.credentials
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:



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/firebase/admin/messaging/client.rb', line 43

def send_all(messages, dry_run: false)
  raise "messages must be an Array" unless messages.is_a?(Array)
  raise "messages must not contain more than 500 elements" unless messages.length < 500

  responses = []
  @service.batch do |service|
    options = {skip_serialization: true}
    messages.each do |message|
      body = encode_message(message, dry_run: dry_run)
      service.send_message(@project_path, body, options: options) do |res, err|
        wrapped_err = parse_fcm_error(err) unless err.nil?
        responses << SendResponse.new(message_id: res&.name, error: wrapped_err)
      end
    end
  end
  BatchResponse.new(responses: responses)
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:



70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/firebase/admin/messaging/client.rb', line 70

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.



26
27
28
29
30
31
32
# File 'lib/firebase/admin/messaging/client.rb', line 26

def send_one(message, dry_run: false)
  body = encode_message(message, dry_run: dry_run)
  res = @service.send_message(@project_path, body, options: {skip_serialization: true})
  res.name
rescue Google::Apis::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:



90
91
92
# File 'lib/firebase/admin/messaging/client.rb', line 90

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:



100
101
102
# File 'lib/firebase/admin/messaging/client.rb', line 100

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