Class: FCM
Constant Summary collapse
- GROUP_NOTIFICATION_BASE_URI =
constants
'https://android.googleapis.com/gcm'
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)
- #create_notification_key(key_name, project_id, registration_ids = []) ⇒ Object (also: #create)
-
#initialize(api_key, client_options = {}) ⇒ FCM
constructor
A new instance of FCM.
- #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)
{ “collapse_key”: “score_update”, “time_to_live”: 108, “delay_while_idle”: true, “registration_ids”: [“4”, “8”, “15”, “16”, “23”, “42”], “data” : { “score”: “5x1”, “time”: “15:10” } } fcm = FCM.new(“API_KEY”) fcm.send(registration_ids: [“4sdsx”, “8sdsd”], {score: “5x1”}).
- #send_to_topic(topic, options = {}) ⇒ Object
- #send_with_notification_key(notification_key, options = {}) ⇒ Object
Constructor Details
#initialize(api_key, client_options = {}) ⇒ FCM
16 17 18 19 |
# File 'lib/fcm.rb', line 16 def initialize(api_key, = {}) @api_key = api_key @client_options = end |
Instance Attribute Details
#api_key ⇒ Object
Returns the value of attribute api_key.
14 15 16 |
# File 'lib/fcm.rb', line 14 def api_key @api_key end |
#timeout ⇒ Object
Returns the value of attribute timeout.
14 15 16 |
# File 'lib/fcm.rb', line 14 def timeout @timeout end |
Instance Method Details
#add_registration_ids(key_name, project_id, notification_key, registration_ids) ⇒ Object Also known as: add
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/fcm.rb', line 71 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 |
#create_notification_key(key_name, project_id, registration_ids = []) ⇒ Object Also known as: create
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/fcm.rb', line 48 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 |
#recover_notification_key(key_name, project_id) ⇒ Object
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/fcm.rb', line 117 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.post('/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
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/fcm.rb', line 94 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
{
"collapse_key": "score_update",
"time_to_live": 108,
"delay_while_idle": true,
"registration_ids": ["4", "8", "15", "16", "23", "42"],
"data" : {
"score": "5x1",
"time": "15:10"
}
} fcm = FCM.new(“API_KEY”) fcm.send(registration_ids: [“4sdsx”, “8sdsd”], {score: “5x1”})
33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/fcm.rb', line 33 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
152 153 154 155 156 |
# File 'lib/fcm.rb', line 152 def send_to_topic(topic, = {}) if topic =~ /[a-zA-Z0-9\-_.~%]+/ send_with_notification_key('/topics/' + topic, ) end end |
#send_with_notification_key(notification_key, options = {}) ⇒ Object
137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/fcm.rb', line 137 def send_with_notification_key(notification_key, = {}) body = { to: notification_key }.merge() params = { body: body.to_json, headers: { 'Authorization' => "key=#{@api_key}", 'Content-Type' => 'application/json' } } response = self.class.post('/send', params.merge(@client_options)) build_response(response) end |