fcm 1.0.5 → 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/README.md +145 -51
- data/fcm.gemspec +14 -13
- data/lib/fcm.rb +63 -91
- data/spec/fcm_spec.rb +331 -394
- metadata +28 -8
- 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/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/).
|
|
@@ -22,6 +22,38 @@ For Android you will need a device running 2.3 (or newer) that also have the Goo
|
|
|
22
22
|
A version of supported Ruby, currently:
|
|
23
23
|
`ruby >= 2.4`
|
|
24
24
|
|
|
25
|
+
## Getting Started
|
|
26
|
+
To use this gem, you need to instantiate a client with your firebase credentials:
|
|
27
|
+
|
|
28
|
+
```ruby
|
|
29
|
+
fcm = FCM.new(
|
|
30
|
+
GOOGLE_APPLICATION_CREDENTIALS_PATH,
|
|
31
|
+
FIREBASE_PROJECT_ID
|
|
32
|
+
)
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
## About the `GOOGLE_APPLICATION_CREDENTIALS_PATH`
|
|
36
|
+
The `GOOGLE_APPLICATION_CREDENTIALS_PATH` is meant to contain your firebase credentials.
|
|
37
|
+
|
|
38
|
+
The easiest way to provide them is to pass here an absolute path to a file with your credentials:
|
|
39
|
+
|
|
40
|
+
```ruby
|
|
41
|
+
fcm = FCM.new(
|
|
42
|
+
'/path/to/credentials.json',
|
|
43
|
+
FIREBASE_PROJECT_ID
|
|
44
|
+
)
|
|
45
|
+
```
|
|
46
|
+
|
|
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:
|
|
48
|
+
|
|
49
|
+
```ruby
|
|
50
|
+
fcm = FCM.new(
|
|
51
|
+
StringIO.new(ENV.fetch('FIREBASE_CREDENTIALS')),
|
|
52
|
+
FIREBASE_PROJECT_ID
|
|
53
|
+
)
|
|
54
|
+
|
|
55
|
+
```
|
|
56
|
+
|
|
25
57
|
## Usage
|
|
26
58
|
|
|
27
59
|
## HTTP v1 API
|
|
@@ -30,13 +62,13 @@ To migrate to HTTP v1 see: https://firebase.google.com/docs/cloud-messaging/migr
|
|
|
30
62
|
|
|
31
63
|
```ruby
|
|
32
64
|
fcm = FCM.new(
|
|
33
|
-
API_TOKEN,
|
|
34
65
|
GOOGLE_APPLICATION_CREDENTIALS_PATH,
|
|
35
66
|
FIREBASE_PROJECT_ID
|
|
36
67
|
)
|
|
37
68
|
message = {
|
|
38
|
-
'
|
|
39
|
-
# '
|
|
69
|
+
'token': "000iddqd", # send to a specific device
|
|
70
|
+
# 'topic': "yourTopic",
|
|
71
|
+
# 'condition': "'TopicA' in topics && ('TopicB' in topics || 'TopicC' in topics)",
|
|
40
72
|
'data': {
|
|
41
73
|
payload: {
|
|
42
74
|
data: {
|
|
@@ -62,58 +94,42 @@ message = {
|
|
|
62
94
|
}
|
|
63
95
|
}
|
|
64
96
|
|
|
65
|
-
fcm.send_v1(message)
|
|
97
|
+
fcm.send_v1(message) # or fcm.send_notification_v1(message)
|
|
66
98
|
```
|
|
67
99
|
|
|
68
|
-
## HTTP Legacy Version
|
|
69
|
-
|
|
70
|
-
To migrate to HTTP v1 see: https://firebase.google.com/docs/cloud-messaging/migrate-v1
|
|
71
|
-
|
|
72
|
-
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`.
|
|
73
|
-
|
|
74
|
-
Example sending notifications:
|
|
75
|
-
|
|
76
|
-
```ruby
|
|
77
|
-
require 'fcm'
|
|
78
|
-
|
|
79
|
-
fcm = FCM.new("my_server_key")
|
|
80
|
-
|
|
81
|
-
registration_ids= ["12", "13"] # an array of one or more client registration tokens
|
|
82
|
-
|
|
83
|
-
# See https://firebase.google.com/docs/cloud-messaging/http-server-ref for all available options.
|
|
84
|
-
options = { "notification": {
|
|
85
|
-
"title": "Portugal vs. Denmark",
|
|
86
|
-
"body": "5 to 1"
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
response = fcm.send(registration_ids, options)
|
|
90
|
-
```
|
|
91
|
-
|
|
92
|
-
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.
|
|
93
|
-
|
|
94
100
|
## Device Group Messaging
|
|
95
101
|
|
|
96
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.
|
|
97
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
|
+
|
|
98
107
|
### Generate a Notification Key for device group
|
|
99
108
|
|
|
100
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:
|
|
101
110
|
|
|
111
|
+
`project_id` is the SENDER_ID in your cloud settings.
|
|
112
|
+
https://firebase.google.com/docs/cloud-messaging/concept-options#senderid
|
|
113
|
+
|
|
102
114
|
```ruby
|
|
103
|
-
params = {key_name: "appUser-Chris",
|
|
115
|
+
params = { key_name: "appUser-Chris",
|
|
104
116
|
project_id: "my_project_id",
|
|
105
|
-
registration_ids: ["4", "8", "15", "16", "23", "42"]}
|
|
117
|
+
registration_ids: ["4", "8", "15", "16", "23", "42"] }
|
|
106
118
|
response = fcm.create(*params.values)
|
|
107
119
|
```
|
|
108
120
|
|
|
109
|
-
### Send to Notification
|
|
121
|
+
### Send to Notification device group
|
|
110
122
|
|
|
111
|
-
|
|
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
|
|
112
125
|
|
|
113
126
|
```ruby
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
127
|
+
message = {
|
|
128
|
+
'token': "NOTIFICATION_KEY", # send to a device group
|
|
129
|
+
# ...data
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
fcm.send_v1(message)
|
|
117
133
|
```
|
|
118
134
|
|
|
119
135
|
### Add/Remove Registration Tokens
|
|
@@ -136,23 +152,51 @@ response = fcm.remove(*params.values)
|
|
|
136
152
|
|
|
137
153
|
## Send Messages to Topics
|
|
138
154
|
|
|
139
|
-
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-_.~%]+"`:
|
|
140
156
|
|
|
141
157
|
```ruby
|
|
142
|
-
|
|
143
|
-
|
|
158
|
+
message = {
|
|
159
|
+
'topic': "yourTopic", # send to a device group
|
|
160
|
+
# ...data
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
fcm.send_v1(message)
|
|
144
164
|
```
|
|
145
165
|
|
|
146
|
-
Or you can use the helper:
|
|
166
|
+
Or you can use the `fcm.send_to_topic` helper:
|
|
147
167
|
|
|
148
168
|
```ruby
|
|
149
169
|
response = fcm.send_to_topic("yourTopic",
|
|
150
|
-
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
|
+
)
|
|
151
195
|
```
|
|
152
196
|
|
|
153
197
|
### Sending to Multiple Topics
|
|
154
198
|
|
|
155
|
-
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_:
|
|
156
200
|
|
|
157
201
|
```
|
|
158
202
|
'TopicA' in topics && ('TopicB' in topics || 'TopicC' in topics)
|
|
@@ -188,18 +232,38 @@ Given a registration token and a topic name, you can add the token to the topic
|
|
|
188
232
|
|
|
189
233
|
```ruby
|
|
190
234
|
topic = "YourTopic"
|
|
191
|
-
|
|
192
|
-
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)
|
|
193
239
|
```
|
|
194
240
|
|
|
195
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)
|
|
196
242
|
|
|
197
243
|
```ruby
|
|
198
244
|
topic = "YourTopic"
|
|
199
|
-
|
|
200
|
-
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)
|
|
201
247
|
# or unsubscription
|
|
202
|
-
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)
|
|
203
267
|
```
|
|
204
268
|
|
|
205
269
|
## Mobile Clients
|
|
@@ -210,9 +274,38 @@ The guide to set up an iOS app to get notifications is here: [Setting up a FCM C
|
|
|
210
274
|
|
|
211
275
|
## ChangeLog
|
|
212
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
|
+
|
|
213
306
|
### 1.0.3
|
|
214
307
|
|
|
215
|
-
- Fix overly strict faraday
|
|
308
|
+
- Fix overly strict faraday dependency
|
|
216
309
|
|
|
217
310
|
### 1.0.2
|
|
218
311
|
|
|
@@ -232,6 +325,7 @@ The guide to set up an iOS app to get notifications is here: [Setting up a FCM C
|
|
|
232
325
|
- Fixed group messaging url.
|
|
233
326
|
- Added API to `recover_notification_key`.
|
|
234
327
|
|
|
328
|
+
|
|
235
329
|
### 0.0.1
|
|
236
330
|
|
|
237
331
|
- Initial version.
|
|
@@ -250,7 +344,7 @@ Update version in `fcm.gemspec` with `VERSION` and update `README.md` `## Change
|
|
|
250
344
|
|
|
251
345
|
```bash
|
|
252
346
|
# set the version
|
|
253
|
-
# VERSION="1.0.
|
|
347
|
+
# VERSION="1.0.7"
|
|
254
348
|
gem build fcm.gemspec
|
|
255
349
|
git tag -a v${VERSION} -m "Releasing version v${VERSION}"
|
|
256
350
|
git push origin --tags
|
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
|
data/lib/fcm.rb
CHANGED
|
@@ -7,20 +7,15 @@ class FCM
|
|
|
7
7
|
BASE_URI = "https://fcm.googleapis.com"
|
|
8
8
|
BASE_URI_V1 = "https://fcm.googleapis.com/v1/projects/"
|
|
9
9
|
DEFAULT_TIMEOUT = 30
|
|
10
|
-
FORMAT = :json
|
|
11
10
|
|
|
12
|
-
# constants
|
|
13
11
|
GROUP_NOTIFICATION_BASE_URI = "https://android.googleapis.com"
|
|
14
12
|
INSTANCE_ID_API = "https://iid.googleapis.com"
|
|
15
13
|
TOPIC_REGEX = /[a-zA-Z0-9\-_.~%]+/
|
|
16
14
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
def initialize(api_key, json_key_path = "", project_name = "", client_options = {})
|
|
20
|
-
@api_key = api_key
|
|
21
|
-
@client_options = client_options
|
|
15
|
+
def initialize(json_key_path = "", project_name = "", http_options = {})
|
|
22
16
|
@json_key_path = json_key_path
|
|
23
|
-
@
|
|
17
|
+
@project_name = project_name
|
|
18
|
+
@http_options = http_options
|
|
24
19
|
end
|
|
25
20
|
|
|
26
21
|
# See https://firebase.google.com/docs/cloud-messaging/send-message
|
|
@@ -47,48 +42,24 @@ class FCM
|
|
|
47
42
|
# }
|
|
48
43
|
# }
|
|
49
44
|
# }
|
|
50
|
-
# fcm = FCM.new(
|
|
51
|
-
# fcm.
|
|
45
|
+
# fcm = FCM.new(json_key_path, project_name)
|
|
46
|
+
# fcm.send_v1(
|
|
52
47
|
# { "token": "4sdsx",, "to" : "notification": {}.. }
|
|
53
48
|
# )
|
|
54
49
|
def send_notification_v1(message)
|
|
55
|
-
return if @
|
|
50
|
+
return if @project_name.empty?
|
|
56
51
|
|
|
57
52
|
post_body = { 'message': message }
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
53
|
+
for_uri(BASE_URI_V1) do |connection|
|
|
54
|
+
response = connection.post(
|
|
55
|
+
"#{@project_name}/messages:send", post_body.to_json
|
|
56
|
+
)
|
|
57
|
+
build_response(response)
|
|
63
58
|
end
|
|
64
|
-
build_response(response)
|
|
65
59
|
end
|
|
66
60
|
|
|
67
61
|
alias send_v1 send_notification_v1
|
|
68
62
|
|
|
69
|
-
# See https://developers.google.com/cloud-messaging/http for more details.
|
|
70
|
-
# { "notification": {
|
|
71
|
-
# "title": "Portugal vs. Denmark",
|
|
72
|
-
# "text": "5 to 1"
|
|
73
|
-
# },
|
|
74
|
-
# "to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1..."
|
|
75
|
-
# }
|
|
76
|
-
# fcm = FCM.new("API_KEY")
|
|
77
|
-
# fcm.send(
|
|
78
|
-
# ["4sdsx", "8sdsd"], # registration_ids
|
|
79
|
-
# { "notification": { "title": "Portugal vs. Denmark", "text": "5 to 1" }, "to" : "bk3RNwTe3HdFQ3P1..." }
|
|
80
|
-
# )
|
|
81
|
-
def send_notification(registration_ids, options = {})
|
|
82
|
-
post_body = build_post_body(registration_ids, options)
|
|
83
|
-
|
|
84
|
-
for_uri(BASE_URI) do |connection|
|
|
85
|
-
response = connection.post("/fcm/send", post_body.to_json)
|
|
86
|
-
build_response(response, registration_ids)
|
|
87
|
-
end
|
|
88
|
-
end
|
|
89
|
-
|
|
90
|
-
alias send send_notification
|
|
91
|
-
|
|
92
63
|
def create_notification_key(key_name, project_id, registration_ids = [])
|
|
93
64
|
post_body = build_post_body(registration_ids, operation: "create",
|
|
94
65
|
notification_key_name: key_name)
|
|
@@ -152,28 +123,29 @@ class FCM
|
|
|
152
123
|
end
|
|
153
124
|
end
|
|
154
125
|
|
|
155
|
-
def
|
|
156
|
-
body = { to: notification_key }.merge(options)
|
|
157
|
-
execute_notification(body)
|
|
158
|
-
end
|
|
159
|
-
|
|
160
|
-
def topic_subscription(topic, registration_id)
|
|
126
|
+
def topic_subscription(topic, registration_token)
|
|
161
127
|
for_uri(INSTANCE_ID_API) do |connection|
|
|
162
|
-
response = connection.post(
|
|
128
|
+
response = connection.post(
|
|
129
|
+
"/iid/v1/#{registration_token}/rel/topics/#{topic}"
|
|
130
|
+
)
|
|
163
131
|
build_response(response)
|
|
164
132
|
end
|
|
165
133
|
end
|
|
166
134
|
|
|
167
|
-
def
|
|
168
|
-
|
|
135
|
+
def topic_unsubscription(topic, registration_token)
|
|
136
|
+
batch_topic_unsubscription(topic, [registration_token])
|
|
137
|
+
end
|
|
138
|
+
|
|
139
|
+
def batch_topic_subscription(topic, registration_tokens)
|
|
140
|
+
manage_topics_relationship(topic, registration_tokens, 'Add')
|
|
169
141
|
end
|
|
170
142
|
|
|
171
|
-
def batch_topic_unsubscription(topic,
|
|
172
|
-
manage_topics_relationship(topic,
|
|
143
|
+
def batch_topic_unsubscription(topic, registration_tokens)
|
|
144
|
+
manage_topics_relationship(topic, registration_tokens, 'Remove')
|
|
173
145
|
end
|
|
174
146
|
|
|
175
|
-
def manage_topics_relationship(topic,
|
|
176
|
-
body = { to: "/topics/#{topic}", registration_tokens:
|
|
147
|
+
def manage_topics_relationship(topic, registration_tokens, action)
|
|
148
|
+
body = { to: "/topics/#{topic}", registration_tokens: registration_tokens }
|
|
177
149
|
|
|
178
150
|
for_uri(INSTANCE_ID_API) do |connection|
|
|
179
151
|
response = connection.post("/iid/v1:batch#{action}", body.to_json)
|
|
@@ -181,53 +153,52 @@ class FCM
|
|
|
181
153
|
end
|
|
182
154
|
end
|
|
183
155
|
|
|
184
|
-
def send_to_topic(topic, options = {})
|
|
185
|
-
if topic.gsub(TOPIC_REGEX, "").length == 0
|
|
186
|
-
send_with_notification_key("/topics/" + topic, options)
|
|
187
|
-
end
|
|
188
|
-
end
|
|
189
|
-
|
|
190
156
|
def get_instance_id_info(iid_token, options = {})
|
|
191
|
-
params =
|
|
192
|
-
query: options,
|
|
193
|
-
}
|
|
157
|
+
params = options
|
|
194
158
|
|
|
195
159
|
for_uri(INSTANCE_ID_API) do |connection|
|
|
196
|
-
response = connection.get("/iid/info
|
|
160
|
+
response = connection.get("/iid/info/#{iid_token}", params)
|
|
197
161
|
build_response(response)
|
|
198
162
|
end
|
|
199
163
|
end
|
|
200
164
|
|
|
201
|
-
def
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
def batch_unsubscribe_instance_ids_from_topic(instance_ids, topic_name)
|
|
214
|
-
manage_topics_relationship(topic_name, instance_ids, "Remove")
|
|
165
|
+
def send_to_topic(topic, options = {})
|
|
166
|
+
if topic.gsub(TOPIC_REGEX, '').length.zero?
|
|
167
|
+
body = { 'message': { 'topic': topic }.merge(options) }
|
|
168
|
+
|
|
169
|
+
for_uri(BASE_URI_V1) do |connection|
|
|
170
|
+
response = connection.post(
|
|
171
|
+
"#{@project_name}/messages:send", body.to_json
|
|
172
|
+
)
|
|
173
|
+
build_response(response)
|
|
174
|
+
end
|
|
175
|
+
end
|
|
215
176
|
end
|
|
216
177
|
|
|
217
178
|
def send_to_topic_condition(condition, options = {})
|
|
218
179
|
if validate_condition?(condition)
|
|
219
|
-
body = { condition: condition }.merge(options)
|
|
220
|
-
|
|
180
|
+
body = { 'message': { 'condition': condition }.merge(options) }
|
|
181
|
+
|
|
182
|
+
for_uri(BASE_URI_V1) do |connection|
|
|
183
|
+
response = connection.post(
|
|
184
|
+
"#{@project_name}/messages:send", body.to_json
|
|
185
|
+
)
|
|
186
|
+
build_response(response)
|
|
187
|
+
end
|
|
221
188
|
end
|
|
222
189
|
end
|
|
223
190
|
|
|
224
191
|
private
|
|
225
192
|
|
|
226
193
|
def for_uri(uri, extra_headers = {})
|
|
227
|
-
connection = ::Faraday.new(
|
|
194
|
+
connection = ::Faraday.new(
|
|
195
|
+
url: uri,
|
|
196
|
+
request: { timeout: @http_options.fetch(:timeout, DEFAULT_TIMEOUT) }
|
|
197
|
+
) do |faraday|
|
|
228
198
|
faraday.adapter Faraday.default_adapter
|
|
229
199
|
faraday.headers["Content-Type"] = "application/json"
|
|
230
|
-
faraday.headers["Authorization"] = "
|
|
200
|
+
faraday.headers["Authorization"] = "Bearer #{jwt_token}"
|
|
201
|
+
faraday.headers["access_token_auth"]= "true"
|
|
231
202
|
extra_headers.each do |key, value|
|
|
232
203
|
faraday.headers[key] = value
|
|
233
204
|
end
|
|
@@ -285,13 +256,6 @@ class FCM
|
|
|
285
256
|
not_registered_ids
|
|
286
257
|
end
|
|
287
258
|
|
|
288
|
-
def execute_notification(body)
|
|
289
|
-
for_uri(BASE_URI) do |connection|
|
|
290
|
-
response = connection.post("/fcm/send", body.to_json)
|
|
291
|
-
build_response(response)
|
|
292
|
-
end
|
|
293
|
-
end
|
|
294
|
-
|
|
295
259
|
def has_canonical_id?(result)
|
|
296
260
|
!result["registration_id"].nil?
|
|
297
261
|
end
|
|
@@ -319,11 +283,19 @@ class FCM
|
|
|
319
283
|
|
|
320
284
|
def jwt_token
|
|
321
285
|
scope = "https://www.googleapis.com/auth/firebase.messaging"
|
|
322
|
-
authorizer
|
|
323
|
-
json_key_io:
|
|
286
|
+
@authorizer ||= Google::Auth::ServiceAccountCredentials.make_creds(
|
|
287
|
+
json_key_io: json_key,
|
|
324
288
|
scope: scope,
|
|
325
289
|
)
|
|
326
|
-
token = authorizer.fetch_access_token!
|
|
290
|
+
token = @authorizer.fetch_access_token!
|
|
327
291
|
token["access_token"]
|
|
328
292
|
end
|
|
293
|
+
|
|
294
|
+
def json_key
|
|
295
|
+
@json_key ||= if @json_key_path.respond_to?(:read)
|
|
296
|
+
@json_key_path
|
|
297
|
+
else
|
|
298
|
+
File.open(@json_key_path)
|
|
299
|
+
end
|
|
300
|
+
end
|
|
329
301
|
end
|