Class: FCM

Inherits:
Object
  • Object
show all
Defined in:
lib/fcm.rb

Constant Summary collapse

BASE_URI =
'https://fcm.googleapis.com'
DEFAULT_TIMEOUT =
30
FORMAT =
:json
GROUP_NOTIFICATION_BASE_URI =

constants

'https://android.googleapis.com'
INSTANCE_ID_API =
'https://iid.googleapis.com'
TOPIC_REGEX =
/[a-zA-Z0-9\-_.~%]+/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key, client_options = {}) ⇒ FCM

Returns a new instance of FCM.



17
18
19
20
# File 'lib/fcm.rb', line 17

def initialize(api_key, client_options = {})
  @api_key = api_key
  @client_options = client_options
end

Instance Attribute Details

#api_keyObject

Returns the value of attribute api_key.



15
16
17
# File 'lib/fcm.rb', line 15

def api_key
  @api_key
end

#timeoutObject

Returns the value of attribute timeout.



15
16
17
# File 'lib/fcm.rb', line 15

def timeout
  @timeout
end

Instance Method Details

#add_registration_ids(key_name, project_id, notification_key, registration_ids) ⇒ Object Also known as: add



59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/fcm.rb', line 59

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_subscribe_instance_ids_to_topic(instance_ids, topic_name) ⇒ Object



162
163
164
# File 'lib/fcm.rb', line 162

def batch_subscribe_instance_ids_to_topic instance_ids, topic_name
  manage_topics_relationship(topic_name, instance_ids, 'Add')
end

#batch_topic_subscription(topic, registration_ids) ⇒ Object



120
121
122
# File 'lib/fcm.rb', line 120

def batch_topic_subscription(topic, registration_ids)
  manage_topics_relationship(topic, registration_ids, 'Add')
end

#batch_topic_unsubscription(topic, registration_ids) ⇒ Object



124
125
126
# File 'lib/fcm.rb', line 124

def batch_topic_unsubscription(topic, registration_ids)
  manage_topics_relationship(topic, registration_ids, 'Remove')
end

#batch_unsubscribe_instance_ids_from_topic(instance_ids, topic_name) ⇒ Object



166
167
168
# File 'lib/fcm.rb', line 166

def batch_unsubscribe_instance_ids_from_topic instance_ids, topic_name
  manage_topics_relationship(topic_name, instance_ids, 'Remove')
end

#create_notification_key(key_name, project_id, registration_ids = []) ⇒ Object Also known as: create



44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/fcm.rb', line 44

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



143
144
145
146
147
148
149
150
151
152
# File 'lib/fcm.rb', line 143

def get_instance_id_info iid_token, options={}
  params = {
    query: 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_ids, action) ⇒ Object



128
129
130
131
132
133
134
135
# File 'lib/fcm.rb', line 128

def manage_topics_relationship(topic, registration_ids, action)
  body = { to: "/topics/#{topic}", registration_tokens: registration_ids }

  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



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/fcm.rb', line 91

def recover_notification_key(key_name, project_id)
  params = {
    query: {
      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



75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/fcm.rb', line 75

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(registration_ids, options = {}) ⇒ Object Also known as: send

See developers.google.com/cloud-messaging/http for more details. { “notification”:

"title": "Portugal vs. Denmark",
"text": "5 to 1"

, “to” : “bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1…” } fcm = FCM.new(“API_KEY”) fcm.send(

["4sdsx", "8sdsd"], # registration_ids
{ "notification": { "title": "Portugal vs. Denmark", "text": "5 to 1" }, "to" : "bk3RNwTe3HdFQ3P1..." }

)



34
35
36
37
38
39
40
41
# File 'lib/fcm.rb', line 34

def send_notification(registration_ids, options = {})
  post_body = build_post_body(registration_ids, options)

  for_uri(BASE_URI) do |connection|
    response = connection.post('/fcm/send', post_body.to_json)
    build_response(response, registration_ids)
  end
end

#send_to_topic(topic, options = {}) ⇒ Object



137
138
139
140
141
# File 'lib/fcm.rb', line 137

def send_to_topic(topic, options = {})
  if topic.gsub(TOPIC_REGEX, "").length == 0
    send_with_notification_key('/topics/' + topic, options)
  end
end

#send_to_topic_condition(condition, options = {}) ⇒ Object



170
171
172
173
174
175
# File 'lib/fcm.rb', line 170

def send_to_topic_condition(condition, options = {})
  if validate_condition?(condition)
    body = { condition: condition }.merge(options)
    execute_notification(body)
  end
end

#send_with_notification_key(notification_key, options = {}) ⇒ Object



108
109
110
111
# File 'lib/fcm.rb', line 108

def send_with_notification_key(notification_key, options = {})
  body = { to: notification_key }.merge(options)
  execute_notification(body)
end

#subscribe_instance_id_to_topic(iid_token, topic_name) ⇒ Object



154
155
156
# File 'lib/fcm.rb', line 154

def subscribe_instance_id_to_topic iid_token, topic_name
  batch_subscribe_instance_ids_to_topic([iid_token], topic_name)
end

#topic_subscription(topic, registration_id) ⇒ Object



113
114
115
116
117
118
# File 'lib/fcm.rb', line 113

def topic_subscription(topic, registration_id)
  for_uri(INSTANCE_ID_API) do |connection|
    response = connection.post("/iid/v1/#{registration_id}/rel/topics/#{topic}")
    build_response(response)
  end
end

#unsubscribe_instance_id_from_topic(iid_token, topic_name) ⇒ Object



158
159
160
# File 'lib/fcm.rb', line 158

def unsubscribe_instance_id_from_topic iid_token, topic_name
  batch_unsubscribe_instance_ids_from_topic([iid_token], topic_name)
end