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 = "") ⇒ 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 = "") ⇒ FCM
Returns a new instance of FCM.
15 16 17 18 |
# File 'lib/fcm.rb', line 15 def initialize(json_key_path = "", project_name = "") @json_key_path = json_key_path @project_name = project_name end |
Instance Method Details
#add_registration_ids(key_name, project_id, notification_key, registration_ids) ⇒ Object Also known as: add
78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/fcm.rb', line 78 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
138 139 140 |
# File 'lib/fcm.rb', line 138 def batch_topic_subscription(topic, registration_tokens) manage_topics_relationship(topic, registration_tokens, 'Add') end |
#batch_topic_unsubscription(topic, registration_tokens) ⇒ Object
142 143 144 |
# File 'lib/fcm.rb', line 142 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
62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/fcm.rb', line 62 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
155 156 157 158 159 160 161 162 |
# File 'lib/fcm.rb', line 155 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
146 147 148 149 150 151 152 153 |
# File 'lib/fcm.rb', line 146 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
112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/fcm.rb', line 112 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
95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/fcm.rb', line 95 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": {}.. }
)
48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/fcm.rb', line 48 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
164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/fcm.rb', line 164 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
177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/fcm.rb', line 177 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
125 126 127 128 129 130 131 132 |
# File 'lib/fcm.rb', line 125 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
134 135 136 |
# File 'lib/fcm.rb', line 134 def topic_unsubscription(topic, registration_token) batch_topic_unsubscription(topic, [registration_token]) end |