@@ -7,20 +7,16 @@ class FCM
77 BASE_URI = "https://fcm.googleapis.com"
88 BASE_URI_V1 = "https://fcm.googleapis.com/v1/projects/"
99 DEFAULT_TIMEOUT = 30
10- FORMAT = :json
1110
12- # constants
1311 GROUP_NOTIFICATION_BASE_URI = "https://android.googleapis.com"
1412 INSTANCE_ID_API = "https://iid.googleapis.com"
1513 TOPIC_REGEX = /[a-zA-Z0-9\- _.~%]+/
1614
17- attr_accessor :timeout , :api_key , :json_key_path , :project_base_uri
18-
1915 def initialize ( api_key , json_key_path = "" , project_name = "" , client_options = { } )
2016 @api_key = api_key
2117 @client_options = client_options
2218 @json_key_path = json_key_path
23- @project_base_uri = BASE_URI_V1 + project_name . to_s
19+ @project_name = project_name
2420 end
2521
2622 # See https://firebase.google.com/docs/cloud-messaging/send-message
@@ -48,20 +44,22 @@ def initialize(api_key, json_key_path = "", project_name = "", client_options =
4844 # }
4945 # }
5046 # fcm = FCM.new(api_key, json_key_path, project_name)
51- # fcm.send (
47+ # fcm.send_v1 (
5248 # { "token": "4sdsx",, "to" : "notification": {}.. }
5349 # )
5450 def send_notification_v1 ( message )
55- return if @project_base_uri . empty?
51+ return if @project_name . empty?
5652
5753 post_body = { 'message' : message }
58-
59- response = Faraday . post ( "#{ @project_base_uri } /messages:send" ) do |req |
60- req . headers [ "Content-Type" ] = "application/json"
61- req . headers [ "Authorization" ] = "Bearer #{ jwt_token } "
62- req . body = post_body . to_json
54+ extra_headers = {
55+ 'Authorization' => "Bearer #{ jwt_token } "
56+ }
57+ for_uri ( BASE_URI_V1 , extra_headers ) do |connection |
58+ response = connection . post (
59+ "#{ @project_name } /messages:send" , post_body . to_json
60+ )
61+ build_response ( response )
6362 end
64- build_response ( response )
6563 end
6664
6765 alias send_v1 send_notification_v1
@@ -228,7 +226,7 @@ def for_uri(uri, extra_headers = {})
228226 ) do |faraday |
229227 faraday . adapter Faraday . default_adapter
230228 faraday . headers [ "Content-Type" ] = "application/json"
231- faraday . headers [ " Authorization" ] = "key=#{ api_key } "
229+ faraday . headers [ ' Authorization' ] = "key=#{ @ api_key} "
232230 extra_headers . each do |key , value |
233231 faraday . headers [ key ] = value
234232 end
0 commit comments