fcm 1.0.2 → 2.0.1

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
2
  SHA256:
3
- metadata.gz: 2e620131ea8e4a53c7a3927da1c48f4893fbeffbbcce88c9306a41614c3e5768
4
- data.tar.gz: 9dae9eb3980e57fe84344afb22f68b29d874dbde580dc28ccd6cf3028cc9e16a
3
+ metadata.gz: 49ddf83437f7c8df20298ffa3f334473e0032032e7f8e88d5021185f448f4606
4
+ data.tar.gz: fc79d5369ee5b22b854f49889a69de1612c4c7e2a37ec17683b8a428a0326549
5
5
  SHA512:
6
- metadata.gz: 63ee034d8813fce59b32673c846b1c86d4dddfc3b1136caa3e6d25fdcc0ca2dc00bfc7a8aa6ef4871bdcfd66790bbf28321c70312171ae994463aac2ce18b6e8
7
- data.tar.gz: 45886b059e48c8497767a2747842178ae2db282a0dde6438cc67fedc54c5264de2f0707c52863dd5015bc0389e0fdc93c23ca8268238210809299fe29d834364
6
+ metadata.gz: 9b37b2f37b95cc0816547c973872c8c17c3905602fe8162bff2e25e3a52c7997f7dae11c472a399c8d947e9bfdde668b3223f793b912d274f4bcd204ba827325
7
+ data.tar.gz: 06f380df29d1bcc8683df25f45c099c8615609081308de79c0966c52ea9f4f887155ee77591b65ad479260d736b5e2b98f682e4616bce19cfc52c937995015ad
@@ -0,0 +1,30 @@
1
+ name: Tests
2
+
3
+ on:
4
+ pull_request:
5
+ branches:
6
+ - '*'
7
+ push:
8
+ branches:
9
+ - master
10
+
11
+ jobs:
12
+ tests:
13
+ runs-on: ubuntu-latest
14
+ strategy:
15
+ matrix:
16
+ ruby: ['2.7', '3.0', '3.1', '3.3']
17
+
18
+ steps:
19
+ - uses: actions/checkout@master
20
+
21
+ - name: Set up Ruby
22
+ uses: ruby/setup-ruby@v1
23
+ with:
24
+ ruby-version: ${{ matrix.ruby }}
25
+ bundler: default
26
+ bundler-cache: true
27
+
28
+ - name: Run tests
29
+ run: |
30
+ bundle exec rspec
data/.gitignore CHANGED
@@ -49,3 +49,4 @@ Gemfile.lock
49
49
  .rvmrc
50
50
  spec/reports
51
51
  *.gem
52
+ .env
data/Gemfile CHANGED
@@ -5,3 +5,4 @@ gem 'rake'
5
5
  gem 'rspec'
6
6
  gem 'webmock'
7
7
  gem 'ci_reporter_rspec'
8
+ gem 'googleauth'
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.svg?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://github.com/decision-labs/fcm/workflows/Tests/badge.svg)](https://github.com/decision-labs/fcm/actions)
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,62 +19,117 @@ 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
-
23
- A version of supported Ruby, currently:
22
+ A version of supported Ruby, currently:
24
23
  `ruby >= 2.4`
25
24
 
25
+ ## Getting Started
26
+ To use this gem, you need to instantiate a client with your firebase credentials:
26
27
 
27
- ## Usage
28
+ ```ruby
29
+ fcm = FCM.new(
30
+ GOOGLE_APPLICATION_CREDENTIALS_PATH,
31
+ FIREBASE_PROJECT_ID
32
+ )
33
+ ```
28
34
 
29
- For your server to send a message to one or more devices, you must first initialise a new `FCM` class with your Firebase Cloud Messaging server key, and then call the `send` method on this and give it 1 or more (up to 1000) registration tokens as an array of strings. You can also optionally send further [HTTP message parameters](https://firebase.google.com/docs/cloud-messaging/http-server-ref) like `data` or `time_to_live` etc. as a hash via the second optional argument to `send`.
35
+ ## About the `GOOGLE_APPLICATION_CREDENTIALS_PATH`
36
+ The `GOOGLE_APPLICATION_CREDENTIALS_PATH` is meant to contain your firebase credentials.
30
37
 
31
- Example sending notifications:
38
+ The easiest way to provide them is to pass here an absolute path to a file with your credentials:
32
39
 
33
40
  ```ruby
34
- require 'fcm'
41
+ fcm = FCM.new(
42
+ '/path/to/credentials.json',
43
+ FIREBASE_PROJECT_ID
44
+ )
45
+ ```
35
46
 
36
- fcm = FCM.new("my_server_key")
37
- # you can set option parameters in here
38
- # - all options are pass to HTTParty method arguments
39
- # - ref: https://github.com/jnunemaker/httparty/blob/master/lib/httparty.rb#L29-L60
40
- # fcm = FCM.new("my_server_key", timeout: 3)
47
+ As per their secret nature, you might not want to have them in your repository. In that case, another supported solution is to pass a `StringIO` that contains your credentials:
41
48
 
42
- registration_ids= ["12", "13"] # an array of one or more client registration tokens
49
+ ```ruby
50
+ fcm = FCM.new(
51
+ StringIO.new(ENV.fetch('FIREBASE_CREDENTIALS')),
52
+ FIREBASE_PROJECT_ID
53
+ )
43
54
 
44
- # See https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages for all available options.
45
- options = { "notification": {
46
- "title": "Portugal vs. Denmark",
47
- "body": "5 to 1"
48
- }
49
- }
50
- response = fcm.send(registration_ids, options)
51
55
  ```
52
56
 
53
- Currently `response` is just a hash containing the response `body`, `headers` and `status_code`. Check [here](https://firebase.google.com/docs/cloud-messaging/server#response) to see how to interpret the responses.
57
+ ## Usage
58
+
59
+ ## HTTP v1 API
60
+
61
+ To migrate to HTTP v1 see: https://firebase.google.com/docs/cloud-messaging/migrate-v1
62
+
63
+ ```ruby
64
+ fcm = FCM.new(
65
+ GOOGLE_APPLICATION_CREDENTIALS_PATH,
66
+ FIREBASE_PROJECT_ID
67
+ )
68
+ message = {
69
+ 'token': "000iddqd", # send to a specific device
70
+ # 'topic': "yourTopic",
71
+ # 'condition': "'TopicA' in topics && ('TopicB' in topics || 'TopicC' in topics)",
72
+ 'data': {
73
+ payload: {
74
+ data: {
75
+ id: 1
76
+ }
77
+ }.to_json
78
+ },
79
+ 'notification': {
80
+ title: notification.title_th,
81
+ body: notification.body_th,
82
+ },
83
+ 'android': {},
84
+ 'apns': {
85
+ payload: {
86
+ aps: {
87
+ sound: "default",
88
+ category: "#{Time.zone.now.to_i}"
89
+ }
90
+ }
91
+ },
92
+ 'fcm_options': {
93
+ analytics_label: 'Label'
94
+ }
95
+ }
96
+
97
+ fcm.send_v1(message) # or fcm.send_notification_v1(message)
98
+ ```
54
99
 
55
100
  ## Device Group Messaging
56
101
 
57
102
  With [device group messaging](https://firebase.google.com/docs/cloud-messaging/notifications), you can send a single message to multiple instance of an app running on devices belonging to a group. Typically, "group" refers a set of different devices that belong to a single user. However, a group could also represent a set of devices where the app instance functions in a highly correlated manner. To use this feature, you will first need an initialised `FCM` class.
58
103
 
104
+ The maximum number of members allowed for a notification key is 20.
105
+ https://firebase.google.com/docs/cloud-messaging/android/device-group#managing_device_groups
106
+
59
107
  ### Generate a Notification Key for device group
60
108
 
61
109
  Then you will need a notification key which you can create for a particular `key_name` which needs to be uniquely named per app in case you have multiple apps for the same `project_id`. This ensures that notifications only go to the intended target app. The `create` method will do this and return the token `notification_key`, that represents the device group, in the response:
62
110
 
111
+ `project_id` is the SENDER_ID in your cloud settings.
112
+ https://firebase.google.com/docs/cloud-messaging/concept-options#senderid
113
+
63
114
  ```ruby
64
- params = {key_name: "appUser-Chris",
115
+ params = { key_name: "appUser-Chris",
65
116
  project_id: "my_project_id",
66
- registration_ids: ["4", "8", "15", "16", "23", "42"]}
117
+ registration_ids: ["4", "8", "15", "16", "23", "42"] }
67
118
  response = fcm.create(*params.values)
68
119
  ```
69
120
 
70
- ### Send to Notification Key
121
+ ### Send to Notification device group
71
122
 
72
- Now you can send a message to a particular `notification_key` via the `send_with_notification_key` method. This allows the server to send a single [data](https://firebase.google.com/docs/cloud-messaging/concept-options#data_messages) payload or/and [notification](https://firebase.google.com/docs/cloud-messaging/concept-options#notifications) payload to multiple app instances (typically on multiple devices) owned by a single user (instead of sending to some registration tokens). Note: the maximum number of members allowed for a `notification_key` is 20.
123
+ To send messages to device groups, use the HTTP v1 API,
124
+ Sending messages to a device group is very similar to sending messages to an individual device, using the same method to authorize send requests. Set the token field to the group notification key
73
125
 
74
126
  ```ruby
75
- response = fcm.send_with_notification_key("notification_key",
76
- data: {score: "3x1"},
77
- collapse_key: "updated_score")
127
+ message = {
128
+ 'token': "NOTIFICATION_KEY", # send to a device group
129
+ # ...data
130
+ }
131
+
132
+ fcm.send_v1(message)
78
133
  ```
79
134
 
80
135
  ### Add/Remove Registration Tokens
@@ -97,23 +152,51 @@ response = fcm.remove(*params.values)
97
152
 
98
153
  ## Send Messages to Topics
99
154
 
100
- FCM [topic messaging](https://firebase.google.com/docs/cloud-messaging/topic-messaging) allows your app server to send a message to multiple devices that have opted in to a particular topic. Based on the publish/subscribe model, topic messaging supports unlimited subscriptions per app. Sending to a topic is very similar to sending to an individual device or to a user group, in the sense that you can use the `fcm.send_with_notification_key()` method where the `notification_key` matches the regular expression `"/topics/[a-zA-Z0-9-_.~%]+"`:
155
+ FCM [topic messaging](https://firebase.google.com/docs/cloud-messaging/topic-messaging) allows your app server to send a message to multiple devices that have opted in to a particular topic. Based on the publish/subscribe model, one app instance can be subscribed to no more than 2000 topics. Sending to a topic is very similar to sending to an individual device or to a user group, in the sense that you can use the `fcm.send_v1` method where the `topic` matches the regular expression `"/topics/[a-zA-Z0-9-_.~%]+"`:
101
156
 
102
157
  ```ruby
103
- response = fcm.send_with_notification_key("/topics/yourTopic",
104
- notification: {body: "This is a FCM Topic Message!"})
158
+ message = {
159
+ 'topic': "yourTopic", # send to a device group
160
+ # ...data
161
+ }
162
+
163
+ fcm.send_v1(message)
105
164
  ```
106
165
 
107
- Or you can use the helper:
166
+ Or you can use the `fcm.send_to_topic` helper:
108
167
 
109
168
  ```ruby
110
169
  response = fcm.send_to_topic("yourTopic",
111
- notification: {body: "This is a FCM Topic Message!"})
170
+ notification: { body: "This is a FCM Topic Message!"} )
171
+ ```
172
+
173
+ ## Send Messages to Topics with Conditions
174
+
175
+ FCM [topic condition messaging](https://firebase.google.com/docs/cloud-messaging/android/topic-messaging#build_send_requests) to send a message to a combination of topics, specify a condition, which is a boolean expression that specifies the target topics.
176
+
177
+ ```ruby
178
+ message = {
179
+ 'condition': "'TopicA' in topics && ('TopicB' in topics || 'TopicC' in topics)", # send to topic condition
180
+ # ...data
181
+ }
182
+
183
+ fcm.send_v1(message)
184
+ ```
185
+
186
+ Or you can use the `fcm.send_to_topic_condition` helper:
187
+
188
+ ```ruby
189
+ response = fcm.send_to_topic_condition(
190
+ "'TopicA' in topics && ('TopicB' in topics || 'TopicC' in topics)",
191
+ notification: {
192
+ body: "This is an FCM Topic Message sent to a condition!"
193
+ }
194
+ )
112
195
  ```
113
196
 
114
197
  ### Sending to Multiple Topics
115
198
 
116
- To send to combinations of multiple topics, the FCM [docs](https://firebase.google.com/docs/cloud-messaging/send-message#send_messages_to_topics_2) require that you set a **condition** key (instead of the `to:` key) to a boolean condition that specifies the target topics. For example, to send messages to devices that subscribed to _TopicA_ and either _TopicB_ or _TopicC_:
199
+ To send to combinations of multiple topics, require that you set a **condition** key to a boolean condition that specifies the target topics. For example, to send messages to devices that subscribed to _TopicA_ and either _TopicB_ or _TopicC_:
117
200
 
118
201
  ```
119
202
  'TopicA' in topics && ('TopicB' in topics || 'TopicC' in topics)
@@ -149,18 +232,38 @@ Given a registration token and a topic name, you can add the token to the topic
149
232
 
150
233
  ```ruby
151
234
  topic = "YourTopic"
152
- registration_id= "12" # a client registration tokens
153
- response = fcm.topic_subscription(topic, registration_id)
235
+ registration_token= "12" # a client registration token
236
+ response = fcm.topic_subscription(topic, registration_token)
237
+ # or unsubscription
238
+ response = fcm.topic_unsubscription(topic, registration_token)
154
239
  ```
155
240
 
156
241
  Or you can manage relationship maps for multiple app instances [Google Instance ID server API. Manage relationship](https://developers.google.com/instance-id/reference/server#manage_relationship_maps_for_multiple_app_instances)
157
242
 
158
243
  ```ruby
159
244
  topic = "YourTopic"
160
- registration_ids= ["4", "8", "15", "16", "23", "42"] # an array of one or more client registration tokens
161
- response = fcm.batch_topic_subscription(topic, registration_ids)
245
+ registration_tokens= ["4", "8", "15", "16", "23", "42"] # an array of one or more client registration tokens
246
+ response = fcm.batch_topic_subscription(topic, registration_tokens)
162
247
  # or unsubscription
163
- response = fcm.batch_topic_unsubscription(topic, registration_ids)
248
+ response = fcm.batch_topic_unsubscription(topic, registration_tokens)
249
+ ```
250
+
251
+ ## Get Information about the Instance ID
252
+
253
+ Given a registration token, you can retrieve information about the token using the [Google Instance ID server API](https://developers.google.com/instance-id/reference/server).
254
+
255
+ ```ruby
256
+ registration_token= "12" # a client registration token
257
+ response = fcm.get_instance_id_info(registration_token)
258
+ ```
259
+
260
+ To get detailed information about the instance ID, you can pass an optional
261
+ `options` hash to the `get_instance_id_info` method:
262
+
263
+ ```ruby
264
+ registration_token= "12" # a client registration token
265
+ options = { "details" => true }
266
+ response = fcm.get_instance_id_info(registration_token, options)
164
267
  ```
165
268
 
166
269
  ## Mobile Clients
@@ -170,6 +273,40 @@ You can find a guide to implement an Android Client app to receive notifications
170
273
  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).
171
274
 
172
275
  ## ChangeLog
276
+
277
+ ### 2.0.1
278
+ - Add `http_options` to `initialize` method and whitelist `timeout` option
279
+
280
+ ### 2.0.0
281
+ #### Breaking Changes
282
+ - Remove deprecated `API_KEY`
283
+ - Remove deprecated `send` method
284
+ - Remove deprecated `send_with_notification_key` method
285
+ - Remove `subscribe_instance_id_to_topic` method
286
+ - Remove `unsubscribe_instance_id_from_topic` method
287
+ - Remove `batch_subscribe_instance_ids_to_topic` method
288
+ - Remove `batch_unsubscribe_instance_ids_from_topic` method
289
+
290
+ #### Supported Features
291
+ - Add HTTP v1 API support for `send_to_topic_condition` method
292
+ - Add HTTP v1 API support for `send_to_topic` method
293
+
294
+ ### 1.0.8
295
+ - caches calls to `Google::Auth::ServiceAccountCredentials` #103
296
+ - Allow `faraday` versions from 1 up to 2 #101
297
+
298
+ ### 1.0.7
299
+
300
+ - Fix passing `DEFAULT_TIMEOUT` to `faraday` [#96](https://github.com/decision-labs/fcm/pull/96)
301
+ - Fix issue with `get_instance_id_info` option params [#98](https://github.com/decision-labs/fcm/pull/98)
302
+ - Accept any IO object for credentials [#95](https://github.com/decision-labs/fcm/pull/94)
303
+
304
+ Huge thanks to @excid3 @jsparling @jensljungblad
305
+
306
+ ### 1.0.3
307
+
308
+ - Fix overly strict faraday dependency
309
+
173
310
  ### 1.0.2
174
311
 
175
312
  - Bug fix: retrieve notification key" params: https://github.com/spacialdb/fcm/commit/b328a75c11d779a06d0ceda83527e26aa0495774
@@ -188,6 +325,7 @@ The guide to set up an iOS app to get notifications is here: [Setting up a FCM C
188
325
  - Fixed group messaging url.
189
326
  - Added API to `recover_notification_key`.
190
327
 
328
+
191
329
  ### 0.0.1
192
330
 
193
331
  - Initial version.
@@ -206,9 +344,9 @@ Update version in `fcm.gemspec` with `VERSION` and update `README.md` `## Change
206
344
 
207
345
  ```bash
208
346
  # set the version
209
- # VERSION="1.0.2"
347
+ # VERSION="1.0.7"
210
348
  gem build fcm.gemspec
211
- gem push fcm-${VERSION}.gem
212
349
  git tag -a v${VERSION} -m "Releasing version v${VERSION}"
213
350
  git push origin --tags
214
- ```
351
+ gem push fcm-${VERSION}.gem
352
+ ```
data/fcm.gemspec CHANGED
@@ -2,22 +2,23 @@
2
2
  $:.push File.expand_path("../lib", __FILE__)
3
3
 
4
4
  Gem::Specification.new do |s|
5
- s.name = "fcm"
6
- s.version = "1.0.2"
7
- s.platform = Gem::Platform::RUBY
8
- s.authors = ["Kashif Rasul", "Shoaib Burq"]
9
- s.email = ["kashif@spacialdb.com", "shoaib@spacialdb.com"]
10
- s.homepage = "https://github.com/spacialdb/fcm"
11
- s.summary = %q{Reliably deliver messages and notifications via FCM}
5
+ s.name = "fcm"
6
+ s.version = "2.0.1"
7
+ s.platform = Gem::Platform::RUBY
8
+ s.authors = ["Kashif Rasul", "Shoaib Burq"]
9
+ s.email = ["kashif@decision-labs.com", "shoaib@decision-labs.com"]
10
+ s.homepage = "https://github.com/decision-labs/fcm"
11
+ s.summary = %q{Reliably deliver messages and notifications via FCM}
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
- s.license = "MIT"
13
+ s.license = "MIT"
14
14
 
15
- s.required_ruby_version = '>= 2.4.0'
15
+ s.required_ruby_version = ">= 2.4.0"
16
16
 
17
- s.files = `git ls-files`.split("\n")
18
- s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
- s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map { |f| File.basename(f) }
20
20
  s.require_paths = ["lib"]
21
21
 
22
- s.add_runtime_dependency('faraday', '~> 1.0.0')
22
+ s.add_runtime_dependency("faraday", ">= 1.0.0", "< 3.0")
23
+ s.add_runtime_dependency("googleauth", "~> 1")
23
24
  end