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 +4 -4
- data/.github/workflows/ci.yml +30 -0
- data/.gitignore +1 -0
- data/Gemfile +1 -0
- data/README.md +180 -42
- data/fcm.gemspec +14 -13
- data/lib/fcm.rb +143 -110
- data/spec/fcm_spec.rb +335 -394
- metadata +32 -12
- data/.travis.yml +0 -7
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 49ddf83437f7c8df20298ffa3f334473e0032032e7f8e88d5021185f448f4606
|
|
4
|
+
data.tar.gz: fc79d5369ee5b22b854f49889a69de1612c4c7e2a37ec17683b8a428a0326549
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
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
data/Gemfile
CHANGED
data/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# Firebase Cloud Messaging (FCM) for Android and iOS
|
|
2
2
|
|
|
3
|
-
[](http://badge.fury.io/rb/fcm) [](http://badge.fury.io/rb/fcm) [](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
|
-
|
|
28
|
+
```ruby
|
|
29
|
+
fcm = FCM.new(
|
|
30
|
+
GOOGLE_APPLICATION_CREDENTIALS_PATH,
|
|
31
|
+
FIREBASE_PROJECT_ID
|
|
32
|
+
)
|
|
33
|
+
```
|
|
28
34
|
|
|
29
|
-
|
|
35
|
+
## About the `GOOGLE_APPLICATION_CREDENTIALS_PATH`
|
|
36
|
+
The `GOOGLE_APPLICATION_CREDENTIALS_PATH` is meant to contain your firebase credentials.
|
|
30
37
|
|
|
31
|
-
|
|
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
|
-
|
|
41
|
+
fcm = FCM.new(
|
|
42
|
+
'/path/to/credentials.json',
|
|
43
|
+
FIREBASE_PROJECT_ID
|
|
44
|
+
)
|
|
45
|
+
```
|
|
35
46
|
|
|
36
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
|
121
|
+
### Send to Notification device group
|
|
71
122
|
|
|
72
|
-
|
|
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
|
-
|
|
76
|
-
|
|
77
|
-
|
|
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,
|
|
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
|
-
|
|
104
|
-
|
|
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,
|
|
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
|
-
|
|
153
|
-
response = fcm.topic_subscription(topic,
|
|
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
|
-
|
|
161
|
-
response = fcm.batch_topic_subscription(topic,
|
|
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,
|
|
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.
|
|
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
|
|
6
|
-
s.version
|
|
7
|
-
s.platform
|
|
8
|
-
s.authors
|
|
9
|
-
s.email
|
|
10
|
-
s.homepage
|
|
11
|
-
s.summary
|
|
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
|
|
13
|
+
s.license = "MIT"
|
|
14
14
|
|
|
15
|
-
s.required_ruby_version
|
|
15
|
+
s.required_ruby_version = ">= 2.4.0"
|
|
16
16
|
|
|
17
|
-
s.files
|
|
18
|
-
s.test_files
|
|
19
|
-
s.executables
|
|
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(
|
|
22
|
+
s.add_runtime_dependency("faraday", ">= 1.0.0", "< 3.0")
|
|
23
|
+
s.add_runtime_dependency("googleauth", "~> 1")
|
|
23
24
|
end
|