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
- GROUP_NOTIFICATION_BASE_URI =
"https://android.googleapis.com"
- INSTANCE_ID_API =
"https://iid.googleapis.com"
- TOPIC_REGEX =
/[a-zA-Z0-9\-_.~%]+/
Instance Method Summary collapse
- #add_registration_ids(key_name, project_id, notification_key, registration_ids) ⇒ Object (also: #add)
- #batch_topic_subscription(topic, registration_tokens) ⇒ Object
- #batch_topic_unsubscription(topic, registration_tokens) ⇒ Object
- #create_notification_key(key_name, project_id, registration_ids = []) ⇒ Object (also: #create)
- #get_instance_id_info(iid_token, options = {}) ⇒ Object
-
#initialize(json_key_path = "", project_name = "", http_options = {}) ⇒ FCM
constructor
A new instance of FCM.
- #manage_topics_relationship(topic, registration_tokens, 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_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(json_key_path, project_name) fcm.send_v1( { “token”: “4sdsx”,, “to” : “notification”: {}.. } ).
- #send_to_topic(topic, options = {}) ⇒ Object
- #send_to_topic_condition(condition, options = {}) ⇒ Object
- #topic_subscription(topic, registration_token) ⇒ Object
- #topic_unsubscription(topic, registration_token) ⇒ Object
Constructor Details
#initialize(json_key_path = "", project_name = "", http_options = {}) ⇒ FCM
Returns a new instance of FCM.
15 16 17 18 19 |
# File 'lib/fcm.rb', line 15 def initialize(json_key_path = "", project_name = "", = {}) @json_key_path = json_key_path @project_name = project_name @http_options = end |
Instance Method Details
#add_registration_ids(key_name, project_id, notification_key, registration_ids) ⇒ Object Also known as: add
79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/fcm.rb', line 79 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
139 140 141 |
# File 'lib/fcm.rb', line 139 def batch_topic_subscription(topic, registration_tokens) manage_topics_relationship(topic, registration_tokens, 'Add') end |
#batch_topic_unsubscription(topic, registration_tokens) ⇒ Object
143 144 145 |
# File 'lib/fcm.rb', line 143 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
63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/fcm.rb', line 63 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
156 157 158 159 160 161 162 163 |
# File 'lib/fcm.rb', line 156 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_tokens, action) ⇒ Object
147 148 149 150 151 152 153 154 |
# File 'lib/fcm.rb', line 147 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
113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/fcm.rb', line 113 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
96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# 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) 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 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": {}.. }
)
49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/fcm.rb', line 49 def send_notification_v1() return if @project_name.empty? post_body = { '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
165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/fcm.rb', line 165 def send_to_topic(topic, = {}) if topic.gsub(TOPIC_REGEX, '').length.zero? body = { 'message': { 'topic': topic }.merge() } for_uri(BASE_URI_V1) do |connection| response = connection.post( "#{@project_name}/messages:send", body.to_json ) build_response(response) end end end |
#send_to_topic_condition(condition, options = {}) ⇒ Object
178 179 180 181 182 183 184 185 186 187 188 189 |
# File 'lib/fcm.rb', line 178 def send_to_topic_condition(condition, = {}) if validate_condition?(condition) body = { 'message': { 'condition': condition }.merge() } for_uri(BASE_URI_V1) do |connection| response = connection.post( "#{@project_name}/messages:send", body.to_json ) build_response(response) end end end |
#topic_subscription(topic, registration_token) ⇒ Object
126 127 128 129 130 131 132 133 |
# File 'lib/fcm.rb', line 126 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
135 136 137 |
# File 'lib/fcm.rb', line 135 def topic_unsubscription(topic, registration_token) batch_topic_unsubscription(topic, [registration_token]) end |