Class: FCM
- Inherits:
-
Object
show all
- Defined in:
- lib/fcm.rb
Defined Under Namespace
Classes: InvalidCredentialError
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
-
#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.
17
18
19
20
21
|
# File 'lib/fcm.rb', line 17
def initialize(json_key_path = "", project_name = "", http_options = {})
@json_key_path = json_key_path
@project_name = project_name
@http_options = http_options
end
|
Instance Method Details
#add_registration_ids(key_name, project_id, notification_key, registration_ids) ⇒ Object
Also known as:
add
81
82
83
84
85
86
87
88
89
90
91
92
93
94
|
# File 'lib/fcm.rb', line 81
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)
= {
"project_id" => project_id,
}
for_uri(GROUP_NOTIFICATION_BASE_URI, ) do |connection|
response = connection.post("/gcm/notification", post_body.to_json)
build_response(response)
end
end
|
#batch_topic_subscription(topic, registration_tokens) ⇒ Object
141
142
143
|
# File 'lib/fcm.rb', line 141
def batch_topic_subscription(topic, registration_tokens)
manage_topics_relationship(topic, registration_tokens, 'Add')
end
|
#batch_topic_unsubscription(topic, registration_tokens) ⇒ Object
145
146
147
|
# File 'lib/fcm.rb', line 145
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
65
66
67
68
69
70
71
72
73
74
75
76
77
|
# File 'lib/fcm.rb', line 65
def create_notification_key(key_name, project_id, registration_ids = [])
post_body = build_post_body(registration_ids, operation: "create",
notification_key_name: key_name)
= {
"project_id" => project_id,
}
for_uri(GROUP_NOTIFICATION_BASE_URI, ) do |connection|
response = connection.post("/gcm/notification", post_body.to_json)
build_response(response)
end
end
|
#get_instance_id_info(iid_token, options = {}) ⇒ Object
158
159
160
161
162
163
164
165
|
# File 'lib/fcm.rb', line 158
def get_instance_id_info(iid_token, options = {})
params = options
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
149
150
151
152
153
154
155
156
|
# File 'lib/fcm.rb', line 149
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
115
116
117
118
119
120
121
122
123
124
125
126
|
# File 'lib/fcm.rb', line 115
def recover_notification_key(key_name, project_id)
params = { notification_key_name: key_name }
= {
"project_id" => project_id,
}
for_uri(GROUP_NOTIFICATION_BASE_URI, ) 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
|
# File 'lib/fcm.rb', line 98
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)
= {
"project_id" => project_id,
}
for_uri(GROUP_NOTIFICATION_BASE_URI, ) 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": {}.. }
)
51
52
53
54
55
56
57
58
59
60
61
|
# File 'lib/fcm.rb', line 51
def send_notification_v1(message)
return if @project_name.empty?
post_body = { 'message': 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
167
168
169
170
171
172
173
174
175
176
177
178
|
# File 'lib/fcm.rb', line 167
def send_to_topic(topic, options = {})
if topic.gsub(TOPIC_REGEX, '').length.zero?
body = { 'message': { 'topic': topic }.merge(options) }
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
180
181
182
183
184
185
186
187
188
189
190
191
|
# File 'lib/fcm.rb', line 180
def send_to_topic_condition(condition, options = {})
if validate_condition?(condition)
body = { 'message': { 'condition': condition }.merge(options) }
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
128
129
130
131
132
133
134
135
|
# File 'lib/fcm.rb', line 128
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
137
138
139
|
# File 'lib/fcm.rb', line 137
def topic_unsubscription(topic, registration_token)
batch_topic_unsubscription(topic, [registration_token])
end
|