fcm 0.0.5 → 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: c66c86d3d3691b424672e80b59a82960bb4ff54d
4
- data.tar.gz: '091c4d8413d499f23d87f419e0b0f0f560242bb7'
2
+ SHA256:
3
+ metadata.gz: 2e620131ea8e4a53c7a3927da1c48f4893fbeffbbcce88c9306a41614c3e5768
4
+ data.tar.gz: 9dae9eb3980e57fe84344afb22f68b29d874dbde580dc28ccd6cf3028cc9e16a
5
5
  SHA512:
6
- metadata.gz: a9540aeb7f04c0a80d2777511261a87d5e6fc08e88dc6806775c23b83aa2df10cf10c80d6aa7abfdeb9140e92ab47e37c6e1c507555b813500fa5b85892be24f
7
- data.tar.gz: 96121c74b6e7ecea819fbc9db61b5cea64b8d2eff054620950bc9f73d4da270fe0b5cfd8ce31152f0075c71b87d4c7620bc1be12f1e387917dc4f7435250c0cb
6
+ metadata.gz: 63ee034d8813fce59b32673c846b1c86d4dddfc3b1136caa3e6d25fdcc0ca2dc00bfc7a8aa6ef4871bdcfd66790bbf28321c70312171ae994463aac2ce18b6e8
7
+ data.tar.gz: 45886b059e48c8497767a2747842178ae2db282a0dde6438cc67fedc54c5264de2f0707c52863dd5015bc0389e0fdc93c23ca8268238210809299fe29d834364
data/.gitignore CHANGED
@@ -48,4 +48,4 @@ specifications
48
48
  Gemfile.lock
49
49
  .rvmrc
50
50
  spec/reports
51
- *.gem
51
+ *.gem
@@ -1,6 +1,7 @@
1
1
  language: ruby
2
2
  rvm:
3
- - 2.0.0
4
- - 2.1.10
5
- - 2.2.5
6
- - 2.3.1
3
+ - 2.4.0
4
+ - 2.4.5
5
+ - 2.5.3
6
+ - 2.6.1
7
+ - 2.7.0
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Firebase Cloud Messaging (FCM) for Android and iOS
2
2
 
3
- [![Gem Version](https://badge.fury.io/rb/fcm.svg)](http://badge.fury.io/rb/fcm) [![Build Status](https://secure.travis-ci.org/spacialdb/fcm.png?branch=master)](http://travis-ci.org/spacialdb/fcm)
3
+ [![Gem Version](https://badge.fury.io/rb/fcm.svg)](http://badge.fury.io/rb/fcm) [![Build Status](https://secure.travis-ci.org/spacialdb/fcm.svg?branch=master)](http://travis-ci.org/spacialdb/fcm)
4
4
 
5
5
  The FCM gem lets your ruby backend send notifications to Android and iOS devices via [
6
6
  Firebase Cloud Messaging](https://firebase.google.com/docs/cloud-messaging/).
@@ -19,12 +19,10 @@ gem 'fcm'
19
19
 
20
20
  For Android you will need a device running 2.3 (or newer) that also have the Google Play Store app installed, or an emulator running Android 2.3 with Google APIs. iOS devices are also supported.
21
21
 
22
- One of the following, tested Ruby versions:
23
22
 
24
- - `2.0.0`
25
- - `2.1.9`
26
- - `2.2.5`
27
- - `2.3.1`
23
+ A version of supported Ruby, currently:
24
+ `ruby >= 2.4`
25
+
28
26
 
29
27
  ## Usage
30
28
 
@@ -43,12 +41,11 @@ fcm = FCM.new("my_server_key")
43
41
 
44
42
  registration_ids= ["12", "13"] # an array of one or more client registration tokens
45
43
 
46
- # See https://developers.google.com/cloud-messaging/http for all available options.
44
+ # See https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages for all available options.
47
45
  options = { "notification": {
48
46
  "title": "Portugal vs. Denmark",
49
- "text": "5 to 1"
50
- },
51
- "to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1..."
47
+ "body": "5 to 1"
48
+ }
52
49
  }
53
50
  response = fcm.send(registration_ids, options)
54
51
  ```
@@ -104,14 +101,14 @@ FCM [topic messaging](https://firebase.google.com/docs/cloud-messaging/topic-mes
104
101
 
105
102
  ```ruby
106
103
  response = fcm.send_with_notification_key("/topics/yourTopic",
107
- data: {message: "This is a FCM Topic Message!"})
104
+ notification: {body: "This is a FCM Topic Message!"})
108
105
  ```
109
106
 
110
107
  Or you can use the helper:
111
108
 
112
109
  ```ruby
113
110
  response = fcm.send_to_topic("yourTopic",
114
- data: {message: "This is a FCM Topic Message!"})
111
+ notification: {body: "This is a FCM Topic Message!"})
115
112
  ```
116
113
 
117
114
  ### Sending to Multiple Topics
@@ -140,8 +137,8 @@ The `send_to_topic_condition` method within this library allows you to specicy a
140
137
  ```ruby
141
138
  response = fcm.send_to_topic_condition(
142
139
  "'TopicA' in topics && ('TopicB' in topics || 'TopicC' in topics)",
143
- data: {
144
- message: "This is an FCM Topic Message sent to a condition!"
140
+ notification: {
141
+ body: "This is an FCM Topic Message sent to a condition!"
145
142
  }
146
143
  )
147
144
  ```
@@ -173,6 +170,18 @@ You can find a guide to implement an Android Client app to receive notifications
173
170
  The guide to set up an iOS app to get notifications is here: [Setting up a FCM Client App on iOS](https://firebase.google.com/docs/cloud-messaging/ios/client).
174
171
 
175
172
  ## ChangeLog
173
+ ### 1.0.2
174
+
175
+ - Bug fix: retrieve notification key" params: https://github.com/spacialdb/fcm/commit/b328a75c11d779a06d0ceda83527e26aa0495774
176
+
177
+ ### 1.0.0
178
+
179
+ - Bumped supported ruby to `>= 2.4`
180
+ - Fix deprecation warnings from `faraday` by changing dependency version to `faraday 1.0.0`
181
+
182
+ ### 0.0.7
183
+
184
+ - replace `httparty` with `faraday`
176
185
 
177
186
  ### 0.0.2
178
187
 
@@ -191,8 +200,15 @@ The guide to set up an iOS app to get notifications is here: [Setting up a FCM C
191
200
 
192
201
  - [Contributors](https://github.com/spacialdb/fcm/contributors)
193
202
 
194
- ## Donations
203
+ ## Cutting a release
195
204
 
196
- We accept tips through [Gratipay](https://gratipay.com/spacialdb/).
205
+ Update version in `fcm.gemspec` with `VERSION` and update `README.md` `## ChangeLog` section.
197
206
 
198
- [![Gratipay](https://img.shields.io/gratipay/spacialdb.svg)](https://www.gittip.com/spacialdb/)
207
+ ```bash
208
+ # set the version
209
+ # VERSION="1.0.2"
210
+ gem build fcm.gemspec
211
+ gem push fcm-${VERSION}.gem
212
+ git tag -a v${VERSION} -m "Releasing version v${VERSION}"
213
+ git push origin --tags
214
+ ```
@@ -3,7 +3,7 @@ $:.push File.expand_path("../lib", __FILE__)
3
3
 
4
4
  Gem::Specification.new do |s|
5
5
  s.name = "fcm"
6
- s.version = "0.0.5"
6
+ s.version = "1.0.2"
7
7
  s.platform = Gem::Platform::RUBY
8
8
  s.authors = ["Kashif Rasul", "Shoaib Burq"]
9
9
  s.email = ["kashif@spacialdb.com", "shoaib@spacialdb.com"]
@@ -12,14 +12,12 @@ Gem::Specification.new do |s|
12
12
  s.description = %q{fcm provides ruby bindings to Firebase Cloud Messaging (FCM) a cross-platform messaging solution that lets you reliably deliver messages and notifications at no cost to Android, iOS or Web browsers.}
13
13
  s.license = "MIT"
14
14
 
15
- s.required_ruby_version = '>= 2.0.0'
16
-
17
- s.rubyforge_project = "fcm"
15
+ s.required_ruby_version = '>= 2.4.0'
18
16
 
19
17
  s.files = `git ls-files`.split("\n")
20
18
  s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
21
19
  s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
22
20
  s.require_paths = ["lib"]
23
21
 
24
- s.add_runtime_dependency('httparty', '~> 0.1')
22
+ s.add_runtime_dependency('faraday', '~> 1.0.0')
25
23
  end
data/lib/fcm.rb CHANGED
@@ -1,16 +1,15 @@
1
- require 'httparty'
1
+ require 'faraday'
2
2
  require 'cgi'
3
3
  require 'json'
4
4
 
5
5
  class FCM
6
- include HTTParty
7
- base_uri 'https://fcm.googleapis.com/fcm'
8
- default_timeout 30
9
- format :json
6
+ BASE_URI = 'https://fcm.googleapis.com'
7
+ DEFAULT_TIMEOUT = 30
8
+ FORMAT = :json
10
9
 
11
10
  # constants
12
- GROUP_NOTIFICATION_BASE_URI = 'https://android.googleapis.com/gcm'
13
- INSTANCE_ID_API = 'https://iid.googleapis.com/iid/v1'
11
+ GROUP_NOTIFICATION_BASE_URI = 'https://android.googleapis.com'
12
+ INSTANCE_ID_API = 'https://iid.googleapis.com'
14
13
  TOPIC_REGEX = /[a-zA-Z0-9\-_.~%]+/
15
14
 
16
15
  attr_accessor :timeout, :api_key
@@ -35,15 +34,10 @@ class FCM
35
34
  def send_notification(registration_ids, options = {})
36
35
  post_body = build_post_body(registration_ids, options)
37
36
 
38
- params = {
39
- body: post_body.to_json,
40
- headers: {
41
- 'Authorization' => "key=#{@api_key}",
42
- 'Content-Type' => 'application/json'
43
- }
44
- }
45
- response = self.class.post('/send', params.merge(@client_options))
46
- build_response(response, registration_ids)
37
+ for_uri(BASE_URI) do |connection|
38
+ response = connection.post('/fcm/send', post_body.to_json)
39
+ build_response(response, registration_ids)
40
+ end
47
41
  end
48
42
  alias send send_notification
49
43
 
@@ -51,22 +45,14 @@ class FCM
51
45
  post_body = build_post_body(registration_ids, operation: 'create',
52
46
  notification_key_name: key_name)
53
47
 
54
- params = {
55
- body: post_body.to_json,
56
- headers: {
57
- 'Content-Type' => 'application/json',
58
- 'project_id' => project_id,
59
- 'Authorization' => "key=#{@api_key}"
60
- }
48
+ extra_headers = {
49
+ 'project_id' => project_id
61
50
  }
62
51
 
63
- response = nil
64
-
65
- for_uri(GROUP_NOTIFICATION_BASE_URI) do
66
- response = self.class.post('/notification', params.merge(@client_options))
52
+ for_uri(GROUP_NOTIFICATION_BASE_URI, extra_headers) do |connection|
53
+ response = connection.post('/gcm/notification', post_body.to_json)
54
+ build_response(response)
67
55
  end
68
-
69
- build_response(response)
70
56
  end
71
57
  alias create create_notification_key
72
58
 
@@ -75,21 +61,14 @@ class FCM
75
61
  notification_key_name: key_name,
76
62
  notification_key: notification_key)
77
63
 
78
- params = {
79
- body: post_body.to_json,
80
- headers: {
81
- 'Content-Type' => 'application/json',
82
- 'project_id' => project_id,
83
- 'Authorization' => "key=#{@api_key}"
84
- }
64
+ extra_headers = {
65
+ 'project_id' => project_id
85
66
  }
86
67
 
87
- response = nil
88
-
89
- for_uri(GROUP_NOTIFICATION_BASE_URI) do
90
- response = self.class.post('/notification', params.merge(@client_options))
68
+ for_uri(GROUP_NOTIFICATION_BASE_URI, extra_headers) do |connection|
69
+ response = connection.post('/gcm/notification', post_body.to_json)
70
+ build_response(response)
91
71
  end
92
- build_response(response)
93
72
  end
94
73
  alias add add_registration_ids
95
74
 
@@ -98,42 +77,28 @@ class FCM
98
77
  notification_key_name: key_name,
99
78
  notification_key: notification_key)
100
79
 
101
- params = {
102
- body: post_body.to_json,
103
- headers: {
104
- 'Content-Type' => 'application/json',
105
- 'project_id' => project_id,
106
- 'Authorization' => "key=#{@api_key}"
107
- }
80
+ extra_headers = {
81
+ 'project_id' => project_id
108
82
  }
109
83
 
110
- response = nil
111
-
112
- for_uri(GROUP_NOTIFICATION_BASE_URI) do
113
- response = self.class.post('/notification', params.merge(@client_options))
84
+ for_uri(GROUP_NOTIFICATION_BASE_URI, extra_headers) do |connection|
85
+ response = connection.post('/gcm/notification', post_body.to_json)
86
+ build_response(response)
114
87
  end
115
- build_response(response)
116
88
  end
117
89
  alias remove remove_registration_ids
118
90
 
119
91
  def recover_notification_key(key_name, project_id)
120
- params = {
121
- query: {
122
- notification_key_name: key_name
123
- },
124
- headers: {
125
- 'Content-Type' => 'application/json',
126
- 'project_id' => project_id,
127
- 'Authorization' => "key=#{@api_key}"
128
- }
92
+ params = {notification_key_name: key_name}
93
+
94
+ extra_headers = {
95
+ 'project_id' => project_id
129
96
  }
130
97
 
131
- response = nil
132
-
133
- for_uri(GROUP_NOTIFICATION_BASE_URI) do
134
- response = self.class.get('/notification', params.merge(@client_options))
98
+ for_uri(GROUP_NOTIFICATION_BASE_URI, extra_headers) do |connection|
99
+ response = connection.get('/gcm/notification', params)
100
+ build_response(response)
135
101
  end
136
- build_response(response)
137
102
  end
138
103
 
139
104
  def send_with_notification_key(notification_key, options = {})
@@ -142,20 +107,10 @@ class FCM
142
107
  end
143
108
 
144
109
  def topic_subscription(topic, registration_id)
145
- params = {
146
- headers: {
147
- 'Authorization' => "key=#{@api_key}",
148
- 'Content-Type' => 'application/json'
149
- }
150
- }
151
-
152
- response = nil
153
-
154
- for_uri(INSTANCE_ID_API) do
155
- response = self.class.post("/#{registration_id}/rel/topics/#{topic}", params)
110
+ for_uri(INSTANCE_ID_API) do |connection|
111
+ response = connection.post("/iid/v1/#{registration_id}/rel/topics/#{topic}")
112
+ build_response(response)
156
113
  end
157
-
158
- build_response(response)
159
114
  end
160
115
 
161
116
  def batch_topic_subscription(topic, registration_ids)
@@ -168,31 +123,46 @@ class FCM
168
123
 
169
124
  def manage_topics_relationship(topic, registration_ids, action)
170
125
  body = { to: "/topics/#{topic}", registration_tokens: registration_ids }
171
- params = {
172
- body: body.to_json,
173
- headers: {
174
- 'Authorization' => "key=#{@api_key}",
175
- 'Content-Type' => 'application/json'
176
- }
177
- }
178
126
 
179
- response = nil
180
-
181
- for_uri(INSTANCE_ID_API) do
182
- response = self.class.post("/:batch#{action}", params)
127
+ for_uri(INSTANCE_ID_API) do |connection|
128
+ response = connection.post("/iid/v1:batch#{action}", body.to_json)
129
+ build_response(response)
183
130
  end
184
-
185
- build_response(response)
186
131
  end
187
132
 
188
-
189
-
190
133
  def send_to_topic(topic, options = {})
191
134
  if topic.gsub(TOPIC_REGEX, "").length == 0
192
135
  send_with_notification_key('/topics/' + topic, options)
193
136
  end
194
137
  end
195
138
 
139
+ def get_instance_id_info iid_token, options={}
140
+ params = {
141
+ query: options
142
+ }
143
+
144
+ for_uri(INSTANCE_ID_API) do |connection|
145
+ response = connection.get('/iid/info/'+iid_token, params)
146
+ build_response(response)
147
+ end
148
+ end
149
+
150
+ def subscribe_instance_id_to_topic iid_token, topic_name
151
+ batch_subscribe_instance_ids_to_topic([iid_token], topic_name)
152
+ end
153
+
154
+ def unsubscribe_instance_id_from_topic iid_token, topic_name
155
+ batch_unsubscribe_instance_ids_from_topic([iid_token], topic_name)
156
+ end
157
+
158
+ def batch_subscribe_instance_ids_to_topic instance_ids, topic_name
159
+ manage_topics_relationship(topic_name, instance_ids, 'Add')
160
+ end
161
+
162
+ def batch_unsubscribe_instance_ids_from_topic instance_ids, topic_name
163
+ manage_topics_relationship(topic_name, instance_ids, 'Remove')
164
+ end
165
+
196
166
  def send_to_topic_condition(condition, options = {})
197
167
  if validate_condition?(condition)
198
168
  body = { condition: condition }.merge(options)
@@ -202,11 +172,16 @@ class FCM
202
172
 
203
173
  private
204
174
 
205
- def for_uri(uri)
206
- current_uri = self.class.base_uri
207
- self.class.base_uri uri
208
- yield
209
- self.class.base_uri current_uri
175
+ def for_uri(uri, extra_headers = {})
176
+ connection = ::Faraday.new(:url => uri) do |faraday|
177
+ faraday.adapter Faraday.default_adapter
178
+ faraday.headers["Content-Type"] = "application/json"
179
+ faraday.headers["Authorization"] = "key=#{api_key}"
180
+ extra_headers.each do |key, value|
181
+ faraday.headers[key] = value
182
+ end
183
+ end
184
+ yield connection
210
185
  end
211
186
 
212
187
  def build_post_body(registration_ids, options = {})
@@ -216,8 +191,8 @@ class FCM
216
191
 
217
192
  def build_response(response, registration_ids = [])
218
193
  body = response.body || {}
219
- response_hash = { body: body, headers: response.headers, status_code: response.code }
220
- case response.code
194
+ response_hash = { body: body, headers: response.headers, status_code: response.status }
195
+ case response.status
221
196
  when 200
222
197
  response_hash[:response] = 'success'
223
198
  body = JSON.parse(body) unless body.empty?
@@ -260,16 +235,10 @@ class FCM
260
235
  end
261
236
 
262
237
  def execute_notification(body)
263
- params = {
264
- body: body.to_json,
265
- headers: {
266
- 'Authorization' => "key=#{@api_key}",
267
- 'Content-Type' => 'application/json'
268
- }
269
- }
270
-
271
- response = self.class.post('/send', params.merge(@client_options))
272
- build_response(response)
238
+ for_uri(BASE_URI) do |connection|
239
+ response = connection.post('/fcm/send', body.to_json)
240
+ build_response(response)
241
+ end
273
242
  end
274
243
 
275
244
  def has_canonical_id?(result)
@@ -1,8 +1,8 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe FCM do
4
- let(:send_url) { "#{FCM.base_uri}/send" }
5
- let(:group_notification_base_uri) { "https://android.googleapis.com/gcm/notification" }
4
+ let(:send_url) { "#{FCM::BASE_URI}/fcm/send" }
5
+ let(:group_notification_base_uri) { "#{FCM::GROUP_NOTIFICATION_BASE_URI}/gcm/notification" }
6
6
  let(:api_key) { 'AIzaSyB-1uEai2WiUapxCs2Q0GZYzPu7Udno5aA' }
7
7
  let(:registration_id) { '42' }
8
8
  let(:registration_ids) { ['42'] }
@@ -254,7 +254,7 @@ describe FCM do
254
254
 
255
255
  it 'should not send notification due to 599' do
256
256
  subject.send(registration_ids).should eq(body: '{"body-key" => "Body value"}',
257
- headers: { 'header-key' => ['Header value'] },
257
+ headers: { 'header-key' => 'Header value' },
258
258
  response: 'There was an internal error in the FCM server while trying to process the request.',
259
259
  status_code: 599)
260
260
  end
@@ -472,6 +472,25 @@ describe FCM do
472
472
  end # remove context
473
473
  end
474
474
 
475
+ describe "#recover_notification_key" do
476
+ it "sends a 'retrieve notification key' request" do
477
+ uri = "#{FCM::GROUP_NOTIFICATION_BASE_URI}/gcm/notification"
478
+ endpoint = stub_request(:get, uri).with(
479
+ headers: {
480
+ 'Content-Type' => 'application/json',
481
+ 'Authorization' => "key=TEST_SERVER_KEY",
482
+ 'project_id' => "TEST_PROJECT_ID"
483
+ },
484
+ query: {notification_key_name: "TEST_KEY_NAME"}
485
+ )
486
+ client = FCM.new("TEST_SERVER_KEY")
487
+
488
+ client.recover_notification_key("TEST_KEY_NAME", "TEST_PROJECT_ID")
489
+
490
+ expect(endpoint).to have_been_requested
491
+ end
492
+ end
493
+
475
494
  describe 'subscribing to a topic' do
476
495
  # TODO
477
496
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fcm
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 1.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kashif Rasul
@@ -9,22 +9,22 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2018-11-05 00:00:00.000000000 Z
12
+ date: 2020-07-09 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
- name: httparty
15
+ name: faraday
16
16
  requirement: !ruby/object:Gem::Requirement
17
17
  requirements:
18
18
  - - "~>"
19
19
  - !ruby/object:Gem::Version
20
- version: '0.1'
20
+ version: 1.0.0
21
21
  type: :runtime
22
22
  prerelease: false
23
23
  version_requirements: !ruby/object:Gem::Requirement
24
24
  requirements:
25
25
  - - "~>"
26
26
  - !ruby/object:Gem::Version
27
- version: '0.1'
27
+ version: 1.0.0
28
28
  description: fcm provides ruby bindings to Firebase Cloud Messaging (FCM) a cross-platform
29
29
  messaging solution that lets you reliably deliver messages and notifications at
30
30
  no cost to Android, iOS or Web browsers.
@@ -58,15 +58,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
58
58
  requirements:
59
59
  - - ">="
60
60
  - !ruby/object:Gem::Version
61
- version: 2.0.0
61
+ version: 2.4.0
62
62
  required_rubygems_version: !ruby/object:Gem::Requirement
63
63
  requirements:
64
64
  - - ">="
65
65
  - !ruby/object:Gem::Version
66
66
  version: '0'
67
67
  requirements: []
68
- rubyforge_project: fcm
69
- rubygems_version: 2.6.13
68
+ rubygems_version: 3.1.2
70
69
  signing_key:
71
70
  specification_version: 4
72
71
  summary: Reliably deliver messages and notifications via FCM