Class: FCM

Inherits:
Object
  • Object
show all
Defined in:
lib/fcm.rb

Defined Under Namespace

Classes: InvalidCredentialError

Constant Summary collapse

BASE_URI =
"https://fcm.googleapis.com"
BASE_URI_V1 =
"https://fcm.googleapis.com/v1/projects/"
DEFAULT_TIMEOUT =
30
DEFAULT_KEEP_ALIVE_IDLE_TIMEOUT_SECONDS =
30
DEFAULT_KEEP_ALIVE_POOL_SIZE =
1
GROUP_NOTIFICATION_BASE_URI =
"https://android.googleapis.com"
INSTANCE_ID_API =
"https://iid.googleapis.com"
TOPIC_REGEX =
/[a-zA-Z0-9\-_.~%]+/.freeze

Instance Method Summary collapse

Constructor Details

#initialize(json_key_path = "", project_name = "", http_options = {}) ⇒ FCM

Returns a new instance of FCM.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/fcm.rb', line 20

def initialize(json_key_path = "", project_name = "", http_options = {})
  @json_key_path = json_key_path
  @project_name = project_name
  @http_options = http_options
  @keep_alive_connections = http_options.fetch(:keep_alive_connections, false)
  @keep_alive_idle_timeout_seconds =
    http_options.fetch(:keep_alive_idle_timeout_seconds, DEFAULT_KEEP_ALIVE_IDLE_TIMEOUT_SECONDS)
  @keep_alive_pool_size = http_options.fetch(:keep_alive_pool_size, DEFAULT_KEEP_ALIVE_POOL_SIZE)

  # Per-instance key for the thread-local connection cache so multiple FCM
  # clients in the same process do not share sockets.
  @thread_connections_key = :"_fcm_connections_#{object_id}"

  require "faraday/net_http_persistent" if @keep_alive_connections
end

Instance Method Details

#add_registration_ids(key_name, project_id, notification_key, registration_ids) ⇒ Object Also known as: add



94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/fcm.rb', line 94

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

#batch_topic_subscription(topic, registration_tokens) ⇒ Object



154
155
156
# File 'lib/fcm.rb', line 154

def batch_topic_subscription(topic, registration_tokens)
  manage_topics_relationship(topic, registration_tokens, "Add")
end

#batch_topic_unsubscription(topic, registration_tokens) ⇒ Object



158
159
160
# File 'lib/fcm.rb', line 158

def batch_topic_unsubscription(topic, registration_tokens)
  manage_topics_relationship(topic, registration_tokens, "Remove")
end

#create_notification_key(key_name, project_id, registration_ids = []) ⇒ Object Also known as: create



78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/fcm.rb', line 78

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

#get_instance_id_info(iid_token, options = {}) ⇒ Object



171
172
173
174
175
176
177
178
# File 'lib/fcm.rb', line 171

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) ⇒ Object



162
163
164
165
166
167
168
169
# File 'lib/fcm.rb', line 162

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) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/fcm.rb', line 128

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) ⇒ Object Also known as: remove



111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/fcm.rb', line 111

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

#send_notification_v1(message) ⇒ Object Also known as: send_v1

See https://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": {}.. } )



64
65
66
67
68
69
70
71
72
73
74
# File 'lib/fcm.rb', line 64

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

#send_to_topic(topic, options = {}) ⇒ Object



180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/fcm.rb', line 180

def send_to_topic(topic, options = {})
  return unless topic.gsub(TOPIC_REGEX, "").empty?

  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

#send_to_topic_condition(condition, options = {}) ⇒ Object



193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/fcm.rb', line 193

def send_to_topic_condition(condition, options = {})
  return unless 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

#topic_subscription(topic, registration_token) ⇒ Object



141
142
143
144
145
146
147
148
# File 'lib/fcm.rb', line 141

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) ⇒ Object



150
151
152
# File 'lib/fcm.rb', line 150

def topic_unsubscription(topic, registration_token)
  batch_topic_unsubscription(topic, [registration_token])
end