Skip to content

Commit 0f10803

Browse files
committed
apis related to topic management such as topic (un)subscription
1 parent 27fbde2 commit 0f10803

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# frozen_string_literal: true
2+
3+
module Fcm
4+
class Client
5+
# A Fcm Client class to handle notification setting methods
6+
module InstanceTopicManagement
7+
INSTANCE_ID_API = 'https://iid.googleapis.com'
8+
9+
def manage_topics_relationship(topic, registration_ids, action)
10+
body = { to: "/topics/#{topic}", registration_tokens: registration_ids }
11+
end_point = "/iid/v1:batch#{action}"
12+
res = make_request(
13+
:post, INSTANCE_ID_API, end_point, body, authorization_headers
14+
)
15+
Fcm::Response.build_fcm_response(res, registration_ids)
16+
end
17+
18+
def topic_subscription(topic, registration_id)
19+
end_point = "/iid/v1/#{registration_id}/rel/topics/#{topic}"
20+
res = make_request(
21+
:post, INSTANCE_ID_API, end_point, nil, authorization_headers
22+
)
23+
Fcm::Response.build_fcm_response(res)
24+
end
25+
26+
def get_instance_id_info(iid_token, options = {})
27+
params = options
28+
end_point = "/iid/info/#{iid_token}"
29+
res = make_request(
30+
:get, INSTANCE_ID_API, end_point, params, authorization_headers
31+
)
32+
Fcm::Response.build_fcm_response(res)
33+
end
34+
35+
def batch_topic_subscription(topic, registration_ids)
36+
manage_topics_relationship(topic, registration_ids, 'Add')
37+
end
38+
39+
def batch_topic_unsubscription(topic, registration_ids)
40+
manage_topics_relationship(topic, registration_ids, 'Remove')
41+
end
42+
43+
def batch_subscribe_instance_ids_to_topic(instance_ids, topic_name)
44+
manage_topics_relationship(topic_name, instance_ids, 'Add')
45+
end
46+
47+
def batch_unsubscribe_instance_ids_from_topic(instance_ids, topic_name)
48+
manage_topics_relationship(topic_name, instance_ids, 'Remove')
49+
end
50+
51+
def subscribe_instance_id_to_topic(iid_token, topic_name)
52+
batch_subscribe_instance_ids_to_topic([iid_token], topic_name)
53+
end
54+
55+
def unsubscribe_instance_id_from_topic(iid_token, topic_name)
56+
batch_unsubscribe_instance_ids_from_topic([iid_token], topic_name)
57+
end
58+
end
59+
end
60+
end

0 commit comments

Comments
 (0)