Class: Firebase::Admin::Messaging::Client
- Inherits:
-
Object
- Object
- Firebase::Admin::Messaging::Client
- Defined in:
- lib/firebase/admin/messaging/client.rb
Overview
A client for communicating with the Firebase Cloud Messaging service.
Instance Method Summary collapse
-
#initialize(app) ⇒ Client
constructor
A new instance of Client.
-
#send_all(messages, dry_run: false) ⇒ BatchResponse
Sends the given list of messages via Firebase Cloud Messaging (FCM) as a single batch.
-
#send_multicast(multicast_message, dry_run: false) ⇒ BatchResponse
Sends the given multicast message to all tokens via Firebase Cloud Messaging (FCM).
-
#send_one(message, dry_run: false) ⇒ String
Sends a message via Firebase Cloud Messaging (FCM).
-
#subscribe_to_topic(tokens, topic) ⇒ TopicManagementResponse
Subscribes a list of registration tokens to an FCM topic.
-
#unsubscribe_from_topic(tokens, topic) ⇒ TopicManagementResponse
Unsubscribes a list of registration tokens from an FCM topic.
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. = 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.
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(, dry_run: false) raise "messages must be an Array" unless .is_a?(Array) raise "messages must not contain more than 500 elements" unless .length < 500 responses = [] @service.batch do |service| = {skip_serialization: true} .each do || body = (, dry_run: dry_run) service.(@project_path, body, 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.
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(, dry_run: false) = .tokens.map do |token| Message.new( token: token, data: .data, notification: .notification, android: .android, apns: .apns, fcm_options: . ) end send_all(, 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.
26 27 28 29 30 31 32 |
# File 'lib/firebase/admin/messaging/client.rb', line 26 def send_one(, dry_run: false) body = (, dry_run: dry_run) res = @service.(@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.
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.
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 |