@@ -7,20 +7,16 @@ class FCM
7
7
BASE_URI = "https://fcm.googleapis.com"
8
8
BASE_URI_V1 = "https://fcm.googleapis.com/v1/projects/"
9
9
DEFAULT_TIMEOUT = 30
10
- FORMAT = :json
11
10
12
- # constants
13
11
GROUP_NOTIFICATION_BASE_URI = "https://android.googleapis.com"
14
12
INSTANCE_ID_API = "https://iid.googleapis.com"
15
13
TOPIC_REGEX = /[a-zA-Z0-9\- _.~%]+/
16
14
17
- attr_accessor :timeout , :api_key , :json_key_path , :project_base_uri
18
-
19
15
def initialize ( api_key , json_key_path = "" , project_name = "" , client_options = { } )
20
16
@api_key = api_key
21
17
@client_options = client_options
22
18
@json_key_path = json_key_path
23
- @project_base_uri = BASE_URI_V1 + project_name . to_s
19
+ @project_name = project_name
24
20
end
25
21
26
22
# 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 =
48
44
# }
49
45
# }
50
46
# fcm = FCM.new(api_key, json_key_path, project_name)
51
- # fcm.send (
47
+ # fcm.send_v1 (
52
48
# { "token": "4sdsx",, "to" : "notification": {}.. }
53
49
# )
54
50
def send_notification_v1 ( message )
55
- return if @project_base_uri . empty?
51
+ return if @project_name . empty?
56
52
57
53
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 )
63
62
end
64
- build_response ( response )
65
63
end
66
64
67
65
alias send_v1 send_notification_v1
@@ -228,7 +226,7 @@ def for_uri(uri, extra_headers = {})
228
226
) do |faraday |
229
227
faraday . adapter Faraday . default_adapter
230
228
faraday . headers [ "Content-Type" ] = "application/json"
231
- faraday . headers [ " Authorization" ] = "key=#{ api_key } "
229
+ faraday . headers [ ' Authorization' ] = "key=#{ @ api_key} "
232
230
extra_headers . each do |key , value |
233
231
faraday . headers [ key ] = value
234
232
end
0 commit comments