Class: FCM
Constant Summary collapse
- GROUP_NOTIFICATION_BASE_URI =
constants
'https://android.googleapis.com/gcm'- INSTANCE_ID_API =
'https://iid.googleapis.com/iid/v1'- TOPIC_REGEX =
/[a-zA-Z0-9\-_.~%]+/
Instance Attribute Summary collapse
-
#api_key ⇒ Object
Returns the value of attribute api_key.
-
#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_topic_subscription(topic, registration_ids) ⇒ Object
- #batch_topic_unsubscription(topic, registration_ids) ⇒ Object
- #create_notification_key(key_name, project_id, registration_ids = []) ⇒ Object (also: #create)
-
#initialize(api_key, 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_to_topic(topic, options = {}) ⇒ Object
- #send_to_topic_condition(condition, options = {}) ⇒ Object
- #send_with_notification_key(notification_key, options = {}) ⇒ Object
- #topic_subscription(topic, registration_id) ⇒ Object
Constructor Details
#initialize(api_key, client_options = {}) ⇒ FCM
Returns a new instance of FCM.
18 19 20 21 |
# File 'lib/fcm.rb', line 18 def initialize(api_key, = {}) @api_key = api_key @client_options = end |
Instance Attribute Details
#api_key ⇒ Object
Returns the value of attribute api_key.
16 17 18 |
# File 'lib/fcm.rb', line 16 def api_key @api_key end |
#timeout ⇒ Object
Returns the value of attribute timeout.
16 17 18 |
# File 'lib/fcm.rb', line 16 def timeout @timeout end |
Instance Method Details
#add_registration_ids(key_name, project_id, notification_key, registration_ids) ⇒ Object Also known as: add
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/fcm.rb', line 73 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) params = { body: post_body.to_json, headers: { 'Content-Type' => 'application/json', 'project_id' => project_id, 'Authorization' => "key=#{@api_key}" } } response = nil for_uri(GROUP_NOTIFICATION_BASE_URI) do response = self.class.post('/notification', params.merge(@client_options)) end build_response(response) end |
#batch_topic_subscription(topic, registration_ids) ⇒ Object
161 162 163 |
# File 'lib/fcm.rb', line 161 def batch_topic_subscription(topic, registration_ids) manage_topics_relationship(topic, registration_ids, 'Add') end |
#batch_topic_unsubscription(topic, registration_ids) ⇒ Object
165 166 167 |
# File 'lib/fcm.rb', line 165 def batch_topic_unsubscription(topic, registration_ids) manage_topics_relationship(topic, registration_ids, 'Remove') end |
#create_notification_key(key_name, project_id, registration_ids = []) ⇒ Object Also known as: create
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/fcm.rb', line 50 def create_notification_key(key_name, project_id, registration_ids = []) post_body = build_post_body(registration_ids, operation: 'create', notification_key_name: key_name) params = { body: post_body.to_json, headers: { 'Content-Type' => 'application/json', 'project_id' => project_id, 'Authorization' => "key=#{@api_key}" } } response = nil for_uri(GROUP_NOTIFICATION_BASE_URI) do response = self.class.post('/notification', params.merge(@client_options)) end build_response(response) end |
#manage_topics_relationship(topic, registration_ids, action) ⇒ Object
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
# File 'lib/fcm.rb', line 169 def manage_topics_relationship(topic, registration_ids, action) body = { to: "/topics/#{topic}", registration_tokens: registration_ids } params = { body: body.to_json, headers: { 'Authorization' => "key=#{@api_key}", 'Content-Type' => 'application/json' } } response = nil for_uri(INSTANCE_ID_API) do response = self.class.post("/:batch#{action}", params) end build_response(response) end |
#recover_notification_key(key_name, project_id) ⇒ Object
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/fcm.rb', line 119 def recover_notification_key(key_name, project_id) params = { query: { notification_key_name: key_name }, headers: { 'Content-Type' => 'application/json', 'project_id' => project_id, 'Authorization' => "key=#{@api_key}" } } response = nil for_uri(GROUP_NOTIFICATION_BASE_URI) do response = self.class.get('/notification', params.merge(@client_options)) end build_response(response) end |
#remove_registration_ids(key_name, project_id, notification_key, registration_ids) ⇒ Object Also known as: remove
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# 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) params = { body: post_body.to_json, headers: { 'Content-Type' => 'application/json', 'project_id' => project_id, 'Authorization' => "key=#{@api_key}" } } response = nil for_uri(GROUP_NOTIFICATION_BASE_URI) do response = self.class.post('/notification', params.merge(@client_options)) end build_response(response) 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..." }
)
35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/fcm.rb', line 35 def send_notification(registration_ids, = {}) post_body = build_post_body(registration_ids, ) params = { body: post_body.to_json, headers: { 'Authorization' => "key=#{@api_key}", 'Content-Type' => 'application/json' } } response = self.class.post('/send', params.merge(@client_options)) build_response(response, registration_ids) end |
#send_to_topic(topic, options = {}) ⇒ Object
190 191 192 193 194 |
# File 'lib/fcm.rb', line 190 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
196 197 198 199 200 201 |
# File 'lib/fcm.rb', line 196 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
139 140 141 142 |
# File 'lib/fcm.rb', line 139 def send_with_notification_key(notification_key, = {}) body = { to: notification_key }.merge() execute_notification(body) end |
#topic_subscription(topic, registration_id) ⇒ Object
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/fcm.rb', line 144 def topic_subscription(topic, registration_id) params = { headers: { 'Authorization' => "key=#{@api_key}", 'Content-Type' => 'application/json' } } response = nil for_uri(INSTANCE_ID_API) do response = self.class.post("/#{registration_id}/rel/topics/#{topic}", params) end build_response(response) end |