class FCM
Constants
- BASE_URI
- BASE_URI_V1
- DEFAULT_TIMEOUT
- GROUP_NOTIFICATION_BASE_URI
- INSTANCE_ID_API
- TOPIC_REGEX
Public Class Methods
new(json_key_path = "", project_name = "", http_options = {})
click to toggle source
# File lib/fcm.rb, line 15 def initialize(json_key_path = "", project_name = "", http_options = {}) @json_key_path = json_key_path @project_name = project_name @http_options = http_options end
Public Instance Methods
add_registration_ids(key_name, project_id, notification_key, registration_ids)
click to toggle source
# File lib/fcm.rb, line 79 def add_registration_ids(key_name, project_id, notification_key, registration_ids) post_body = build_post_body(registration_ids, operation: "add", notification_key_name: key_name, notification_key: notification_key) extra_headers = { "project_id" => project_id, } for_uri(GROUP_NOTIFICATION_BASE_URI, extra_headers) do |connection| response = connection.post("/gcm/notification", post_body.to_json) build_response(response) end end
Also aliased as: add
batch_topic_subscription(topic, registration_tokens)
click to toggle source
# File lib/fcm.rb, line 139 def batch_topic_subscription(topic, registration_tokens) manage_topics_relationship(topic, registration_tokens, 'Add') end
batch_topic_unsubscription(topic, registration_tokens)
click to toggle source
# File lib/fcm.rb, line 143 def batch_topic_unsubscription(topic, registration_tokens) manage_topics_relationship(topic, registration_tokens, 'Remove') end
create_notification_key(key_name, project_id, registration_ids = [])
click to toggle source
# File lib/fcm.rb, line 63 def create_notification_key(key_name, project_id, registration_ids = []) post_body = build_post_body(registration_ids, operation: "create", notification_key_name: key_name) extra_headers = { "project_id" => project_id, } for_uri(GROUP_NOTIFICATION_BASE_URI, extra_headers) do |connection| response = connection.post("/gcm/notification", post_body.to_json) build_response(response) end end
Also aliased as: create
get_instance_id_info(iid_token, options = {})
click to toggle source
# File lib/fcm.rb, line 156 def get_instance_id_info(iid_token, options = {}) params = options for_uri(INSTANCE_ID_API) do |connection| response = connection.get("/iid/info/#{iid_token}", params) build_response(response) end end
manage_topics_relationship(topic, registration_tokens, action)
click to toggle source
# File lib/fcm.rb, line 147 def manage_topics_relationship(topic, registration_tokens, action) body = { to: "/topics/#{topic}", registration_tokens: registration_tokens } for_uri(INSTANCE_ID_API) do |connection| response = connection.post("/iid/v1:batch#{action}", body.to_json) build_response(response) end end
recover_notification_key(key_name, project_id)
click to toggle source
# File lib/fcm.rb, line 113 def recover_notification_key(key_name, project_id) params = { notification_key_name: key_name } extra_headers = { "project_id" => project_id, } for_uri(GROUP_NOTIFICATION_BASE_URI, extra_headers) do |connection| response = connection.get("/gcm/notification", params) build_response(response) end end
remove_registration_ids(key_name, project_id, notification_key, registration_ids)
click to toggle source
# File lib/fcm.rb, line 96 def remove_registration_ids(key_name, project_id, notification_key, registration_ids) post_body = build_post_body(registration_ids, operation: "remove", notification_key_name: key_name, notification_key: notification_key) extra_headers = { "project_id" => project_id, } for_uri(GROUP_NOTIFICATION_BASE_URI, extra_headers) do |connection| response = connection.post("/gcm/notification", post_body.to_json) build_response(response) end end
Also aliased as: remove
send_notification_v1(message)
click to toggle source
See firebase.google.com/docs/cloud-messaging/send-message {
"token": "4sdsx", "notification": { "title": "Breaking News", "body": "New news story available." }, "data": { "story_id": "story_12345" }, "android": { "notification": { "click_action": "TOP_STORY_ACTIVITY", "body": "Check out the Top Story" } }, "apns": { "payload": { "aps": { "category" : "NEW_MESSAGE_CATEGORY" } } }
} fcm = FCM.new(json_key_path, project_name)
fcm.send_v1(
{ "token": "4sdsx",, "to" : "notification": {}.. }
)
# File lib/fcm.rb, line 49 def send_notification_v1(message) return if @project_name.empty? post_body = { 'message': message } for_uri(BASE_URI_V1) do |connection| response = connection.post( "#{@project_name}/messages:send", post_body.to_json ) build_response(response) end end
Also aliased as: send_v1
send_to_topic(topic, options = {})
click to toggle source
# File lib/fcm.rb, line 165 def send_to_topic(topic, options = {}) if topic.gsub(TOPIC_REGEX, '').length.zero? body = { 'message': { 'topic': topic }.merge(options) } for_uri(BASE_URI_V1) do |connection| response = connection.post( "#{@project_name}/messages:send", body.to_json ) build_response(response) end end end
send_to_topic_condition(condition, options = {})
click to toggle source
# File lib/fcm.rb, line 178 def send_to_topic_condition(condition, options = {}) if validate_condition?(condition) body = { 'message': { 'condition': condition }.merge(options) } for_uri(BASE_URI_V1) do |connection| response = connection.post( "#{@project_name}/messages:send", body.to_json ) build_response(response) end end end
topic_subscription(topic, registration_token)
click to toggle source
# File lib/fcm.rb, line 126 def topic_subscription(topic, registration_token) for_uri(INSTANCE_ID_API) do |connection| response = connection.post( "/iid/v1/#{registration_token}/rel/topics/#{topic}" ) build_response(response) end end
topic_unsubscription(topic, registration_token)
click to toggle source
# File lib/fcm.rb, line 135 def topic_unsubscription(topic, registration_token) batch_topic_unsubscription(topic, [registration_token]) end
Private Instance Methods
build_canonical_ids(body, registration_ids)
click to toggle source
# File lib/fcm.rb, line 235 def build_canonical_ids(body, registration_ids) canonical_ids = [] unless body.empty? if body["canonical_ids"] > 0 body["results"].each_with_index do |result, index| canonical_ids << { old: registration_ids[index], new: result["registration_id"] } if has_canonical_id?(result) end end end canonical_ids end
build_not_registered_ids(body, registration_id)
click to toggle source
# File lib/fcm.rb, line 247 def build_not_registered_ids(body, registration_id) not_registered_ids = [] unless body.empty? if body["failure"] > 0 body["results"].each_with_index do |result, index| not_registered_ids << registration_id[index] if is_not_registered?(result) end end end not_registered_ids end
build_post_body(registration_ids, options = {})
click to toggle source
# File lib/fcm.rb, line 209 def build_post_body(registration_ids, options = {}) ids = registration_ids.is_a?(String) ? [registration_ids] : registration_ids { registration_ids: ids }.merge(options) end
build_response(response, registration_ids = [])
click to toggle source
# File lib/fcm.rb, line 214 def build_response(response, registration_ids = []) body = response.body || {} response_hash = { body: body, headers: response.headers, status_code: response.status } case response.status when 200 response_hash[:response] = "success" body = JSON.parse(body) unless body.empty? response_hash[:canonical_ids] = build_canonical_ids(body, registration_ids) unless registration_ids.empty? response_hash[:not_registered_ids] = build_not_registered_ids(body, registration_ids) unless registration_ids.empty? when 400 response_hash[:response] = "Only applies for JSON requests. Indicates that the request could not be parsed as JSON, or it contained invalid fields." when 401 response_hash[:response] = "There was an error authenticating the sender account." when 503 response_hash[:response] = "Server is temporarily unavailable." when 500..599 response_hash[:response] = "There was an internal error in the FCM server while trying to process the request." end response_hash end
for_uri(uri, extra_headers = {}) { |connection| ... }
click to toggle source
# File lib/fcm.rb, line 193 def for_uri(uri, extra_headers = {}) connection = ::Faraday.new( url: uri, request: { timeout: @http_options.fetch(:timeout, DEFAULT_TIMEOUT) } ) do |faraday| faraday.adapter Faraday.default_adapter faraday.headers["Content-Type"] = "application/json" faraday.headers["Authorization"] = "Bearer #{jwt_token}" faraday.headers["access_token_auth"]= "true" extra_headers.each do |key, value| faraday.headers[key] = value end end yield connection end
has_canonical_id?(result)
click to toggle source
# File lib/fcm.rb, line 259 def has_canonical_id?(result) !result["registration_id"].nil? end
is_not_registered?(result)
click to toggle source
# File lib/fcm.rb, line 263 def is_not_registered?(result) result["error"] == "NotRegistered" end
json_key()
click to toggle source
# File lib/fcm.rb, line 294 def json_key @json_key ||= if @json_key_path.respond_to?(:read) @json_key_path else File.open(@json_key_path) end end
jwt_token()
click to toggle source
# File lib/fcm.rb, line 284 def jwt_token scope = "https://www.googleapis.com/auth/firebase.messaging" @authorizer ||= Google::Auth::ServiceAccountCredentials.make_creds( json_key_io: json_key, scope: scope, ) token = @authorizer.fetch_access_token! token["access_token"] end
validate_condition?(condition)
click to toggle source
# File lib/fcm.rb, line 267 def validate_condition?(condition) validate_condition_format?(condition) && validate_condition_topics?(condition) end
validate_condition_format?(condition)
click to toggle source
# File lib/fcm.rb, line 271 def validate_condition_format?(condition) bad_characters = condition.gsub( /(topics|in|\s|\(|\)|(&&)|[!]|(\|\|)|'([a-zA-Z0-9\-_.~%]+)')/, "" ) bad_characters.length == 0 end
validate_condition_topics?(condition)
click to toggle source
# File lib/fcm.rb, line 279 def validate_condition_topics?(condition) topics = condition.scan(/(?:^|\S|\s)'([^']*?)'(?:$|\S|\s)/).flatten topics.all? { |topic| topic.gsub(TOPIC_REGEX, "").length == 0 } end