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



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.

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.



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.



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.



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.



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