Class: FCM
- Inherits:
-
Object
- Object
- FCM
- Defined in:
- lib/fcm.rb
Constant Summary collapse
- BASE_URI =
"https://fcm.googleapis.com"
- BASE_URI_V1 =
"https://fcm.googleapis.com/v1/projects/"
- DEFAULT_TIMEOUT =
30
- FORMAT =
:json
- GROUP_NOTIFICATION_BASE_URI =
constants
"https://android.googleapis.com"
- INSTANCE_ID_API =
"https://iid.googleapis.com"
- TOPIC_REGEX =
/[a-zA-Z0-9\-_.~%]+/
Instance Attribute Summary collapse
-
#api_key ⇒ Object
Returns the value of attribute api_key.
-
#json_key_path ⇒ Object
Returns the value of attribute json_key_path.
-
#project_base_uri ⇒ Object
Returns the value of attribute project_base_uri.
-
#timeout ⇒ Object
Returns the value of attribute timeout.
Instance Method Summary collapse
- #add_registration_ids(key_name, project_id, notification_key, registration_ids) ⇒ Object (also: #add)
- #batch_subscribe_instance_ids_to_topic(instance_ids, topic_name) ⇒ Object
- #batch_topic_subscription(topic, registration_ids) ⇒ Object
- #batch_topic_unsubscription(topic, registration_ids) ⇒ Object
- #batch_unsubscribe_instance_ids_from_topic(instance_ids, topic_name) ⇒ Object
- #create_notification_key(key_name, project_id, registration_ids = []) ⇒ Object (also: #create)
- #get_instance_id_info(iid_token, options = {}) ⇒ Object
-
#initialize(api_key, json_key_path = "", project_name = "", client_options = {}) ⇒ FCM
constructor
A new instance of FCM.
- #manage_topics_relationship(topic, registration_ids, action) ⇒ Object
- #recover_notification_key(key_name, project_id) ⇒ Object
- #remove_registration_ids(key_name, project_id, notification_key, registration_ids) ⇒ Object (also: #remove)
-
#send_notification(registration_ids, options = {}) ⇒ Object
(also: #send)
See developers.google.com/cloud-messaging/http for more details.
-
#send_notification_v1(message) ⇒ Object
(also: #send_v1)
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(api_key, json_key_path, project_name) fcm.send( { “token”: “4sdsx”,, “to” : “notification”: {}.. } ).
- #send_to_topic(topic, options = {}) ⇒ Object
- #send_to_topic_condition(condition, options = {}) ⇒ Object
- #send_with_notification_key(notification_key, options = {}) ⇒ Object
- #subscribe_instance_id_to_topic(iid_token, topic_name) ⇒ Object
- #topic_subscription(topic, registration_id) ⇒ Object
- #unsubscribe_instance_id_from_topic(iid_token, topic_name) ⇒ Object
Constructor Details
#initialize(api_key, json_key_path = "", project_name = "", client_options = {}) ⇒ FCM
Returns a new instance of FCM.
19 20 21 22 23 24 |
# File 'lib/fcm.rb', line 19 def initialize(api_key, json_key_path = "", project_name = "", = {}) @api_key = api_key @client_options = @json_key_path = json_key_path @project_base_uri = BASE_URI_V1 + project_name.to_s end |
Instance Attribute Details
#api_key ⇒ Object
Returns the value of attribute api_key.
17 18 19 |
# File 'lib/fcm.rb', line 17 def api_key @api_key end |
#json_key_path ⇒ Object
Returns the value of attribute json_key_path.
17 18 19 |
# File 'lib/fcm.rb', line 17 def json_key_path @json_key_path end |
#project_base_uri ⇒ Object
Returns the value of attribute project_base_uri.
17 18 19 |
# File 'lib/fcm.rb', line 17 def project_base_uri @project_base_uri end |
#timeout ⇒ Object
Returns the value of attribute timeout.
17 18 19 |
# File 'lib/fcm.rb', line 17 def timeout @timeout end |
Instance Method Details
#add_registration_ids(key_name, project_id, notification_key, registration_ids) ⇒ Object Also known as: add
108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/fcm.rb', line 108 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_subscribe_instance_ids_to_topic(instance_ids, topic_name) ⇒ Object
207 208 209 |
# File 'lib/fcm.rb', line 207 def batch_subscribe_instance_ids_to_topic(instance_ids, topic_name) manage_topics_relationship(topic_name, instance_ids, "Add") end |
#batch_topic_subscription(topic, registration_ids) ⇒ Object
167 168 169 |
# File 'lib/fcm.rb', line 167 def batch_topic_subscription(topic, registration_ids) manage_topics_relationship(topic, registration_ids, "Add") end |
#batch_topic_unsubscription(topic, registration_ids) ⇒ Object
171 172 173 |
# File 'lib/fcm.rb', line 171 def batch_topic_unsubscription(topic, registration_ids) manage_topics_relationship(topic, registration_ids, "Remove") end |
#batch_unsubscribe_instance_ids_from_topic(instance_ids, topic_name) ⇒ Object
211 212 213 |
# File 'lib/fcm.rb', line 211 def batch_unsubscribe_instance_ids_from_topic(instance_ids, topic_name) manage_topics_relationship(topic_name, instance_ids, "Remove") end |
#create_notification_key(key_name, project_id, registration_ids = []) ⇒ Object Also known as: create
92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/fcm.rb', line 92 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
190 191 192 193 194 195 196 197 |
# File 'lib/fcm.rb', line 190 def get_instance_id_info(iid_token, = {}) params = 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_ids, action) ⇒ Object
175 176 177 178 179 180 181 182 |
# File 'lib/fcm.rb', line 175 def manage_topics_relationship(topic, registration_ids, action) body = { to: "/topics/#{topic}", registration_tokens: registration_ids } 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
142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/fcm.rb', line 142 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
125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/fcm.rb', line 125 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(registration_ids, options = {}) ⇒ Object Also known as: send
See developers.google.com/cloud-messaging/http for more details. { “notification”:
"title": "Portugal vs. Denmark",
"text": "5 to 1"
, “to” : “bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1…” } fcm = FCM.new(“API_KEY”) fcm.send(
["4sdsx", "8sdsd"], # registration_ids
{ "notification": { "title": "Portugal vs. Denmark", "text": "5 to 1" }, "to" : "bk3RNwTe3HdFQ3P1..." }
)
81 82 83 84 85 86 87 88 |
# File 'lib/fcm.rb', line 81 def send_notification(registration_ids, = {}) post_body = build_post_body(registration_ids, ) for_uri(BASE_URI) do |connection| response = connection.post("/fcm/send", post_body.to_json) build_response(response, registration_ids) end end |
#send_notification_v1(message) ⇒ Object Also known as: send_v1
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(api_key, json_key_path, project_name) fcm.send(
{ "token": "4sdsx",, "to" : "notification": {}.. }
)
54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/fcm.rb', line 54 def send_notification_v1() return if @project_base_uri.empty? post_body = { 'message': } response = Faraday.post("#{@project_base_uri}/messages:send") do |req| req.headers["Content-Type"] = "application/json" req.headers["Authorization"] = "Bearer #{jwt_token}" req.body = post_body.to_json end build_response(response) end |
#send_to_topic(topic, options = {}) ⇒ Object
184 185 186 187 188 |
# File 'lib/fcm.rb', line 184 def send_to_topic(topic, = {}) if topic.gsub(TOPIC_REGEX, "").length == 0 send_with_notification_key("/topics/" + topic, ) end end |
#send_to_topic_condition(condition, options = {}) ⇒ Object
215 216 217 218 219 220 |
# File 'lib/fcm.rb', line 215 def send_to_topic_condition(condition, = {}) if validate_condition?(condition) body = { condition: condition }.merge() execute_notification(body) end end |
#send_with_notification_key(notification_key, options = {}) ⇒ Object
155 156 157 158 |
# File 'lib/fcm.rb', line 155 def send_with_notification_key(notification_key, = {}) body = { to: notification_key }.merge() execute_notification(body) end |
#subscribe_instance_id_to_topic(iid_token, topic_name) ⇒ Object
199 200 201 |
# File 'lib/fcm.rb', line 199 def subscribe_instance_id_to_topic(iid_token, topic_name) batch_subscribe_instance_ids_to_topic([iid_token], topic_name) end |
#topic_subscription(topic, registration_id) ⇒ Object
160 161 162 163 164 165 |
# File 'lib/fcm.rb', line 160 def topic_subscription(topic, registration_id) for_uri(INSTANCE_ID_API) do |connection| response = connection.post("/iid/v1/#{registration_id}/rel/topics/#{topic}") build_response(response) end end |
#unsubscribe_instance_id_from_topic(iid_token, topic_name) ⇒ Object
203 204 205 |
# File 'lib/fcm.rb', line 203 def unsubscribe_instance_id_from_topic(iid_token, topic_name) batch_unsubscribe_instance_ids_from_topic([iid_token], topic_name) end |