Skip to content

Commit f0f400a

Browse files
authored
Merge pull request #123 from OLIOEX/v1-OAuth2-migration-update
Use OAuth2 token instead of server key for v1
2 parents 40eb6ef + 1abcd2c commit f0f400a

File tree

5 files changed

+449
-618
lines changed

5 files changed

+449
-618
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ jobs:
1313
runs-on: ubuntu-latest
1414
strategy:
1515
matrix:
16-
ruby: ['2.7', '3.0', '3.1']
16+
ruby: ['2.7', '3.0', '3.1', '3.3']
1717

1818
steps:
1919
- uses: actions/checkout@master

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,4 @@ Gemfile.lock
4949
.rvmrc
5050
spec/reports
5151
*.gem
52+
.env

README.md

Lines changed: 94 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ To use this gem, you need to instantiate a client with your firebase credentials
2727

2828
```ruby
2929
fcm = FCM.new(
30-
API_TOKEN,
3130
GOOGLE_APPLICATION_CREDENTIALS_PATH,
3231
FIREBASE_PROJECT_ID
3332
)
@@ -40,7 +39,6 @@ The easiest way to provide them is to pass here an absolute path to a file with
4039

4140
```ruby
4241
fcm = FCM.new(
43-
API_TOKEN,
4442
'/path/to/credentials.json',
4543
FIREBASE_PROJECT_ID
4644
)
@@ -50,7 +48,6 @@ As per their secret nature, you might not want to have them in your repository.
5048

5149
```ruby
5250
fcm = FCM.new(
53-
API_TOKEN,
5451
StringIO.new(ENV.fetch('FIREBASE_CREDENTIALS')),
5552
FIREBASE_PROJECT_ID
5653
)
@@ -65,13 +62,13 @@ To migrate to HTTP v1 see: https://firebase.google.com/docs/cloud-messaging/migr
6562

6663
```ruby
6764
fcm = FCM.new(
68-
API_TOKEN,
6965
GOOGLE_APPLICATION_CREDENTIALS_PATH,
7066
FIREBASE_PROJECT_ID
7167
)
7268
message = {
73-
'topic': "89023", # OR token if you want to send to a specific device
74-
# 'token': "000iddqd",
69+
'token': "000iddqd", # send to a specific device
70+
# 'topic': "yourTopic",
71+
# 'condition': "'TopicA' in topics && ('TopicB' in topics || 'TopicC' in topics)",
7572
'data': {
7673
payload: {
7774
data: {
@@ -97,58 +94,42 @@ message = {
9794
}
9895
}
9996

100-
fcm.send_v1(message)
101-
```
102-
103-
## HTTP Legacy Version
104-
105-
To migrate to HTTP v1 see: https://firebase.google.com/docs/cloud-messaging/migrate-v1
106-
107-
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`.
108-
109-
Example sending notifications:
110-
111-
```ruby
112-
require 'fcm'
113-
114-
fcm = FCM.new("my_server_key")
115-
116-
registration_ids= ["12", "13"] # an array of one or more client registration tokens
117-
118-
# See https://firebase.google.com/docs/cloud-messaging/http-server-ref for all available options.
119-
options = { "notification": {
120-
"title": "Portugal vs. Denmark",
121-
"body": "5 to 1"
122-
}
123-
}
124-
response = fcm.send(registration_ids, options)
97+
fcm.send_v1(message) # or fcm.send_notification_v1(message)
12598
```
12699

127-
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.
128-
129100
## Device Group Messaging
130101

131102
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.
132103

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+
133107
### Generate a Notification Key for device group
134108

135109
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:
136110

111+
`project_id` is the SENDER_ID in your cloud settings.
112+
https://firebase.google.com/docs/cloud-messaging/concept-options#senderid
113+
137114
```ruby
138-
params = {key_name: "appUser-Chris",
115+
params = { key_name: "appUser-Chris",
139116
project_id: "my_project_id",
140-
registration_ids: ["4", "8", "15", "16", "23", "42"]}
117+
registration_ids: ["4", "8", "15", "16", "23", "42"] }
141118
response = fcm.create(*params.values)
142119
```
143120

144-
### Send to Notification Key
121+
### Send to Notification device group
145122

146-
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
147125

148126
```ruby
149-
response = fcm.send_with_notification_key("notification_key",
150-
data: {score: "3x1"},
151-
collapse_key: "updated_score")
127+
message = {
128+
'token': "NOTIFICATION_KEY", # send to a device group
129+
# ...data
130+
}
131+
132+
fcm.send_v1(message)
152133
```
153134

154135
### Add/Remove Registration Tokens
@@ -171,23 +152,51 @@ response = fcm.remove(*params.values)
171152

172153
## Send Messages to Topics
173154

174-
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-_.~%]+"`:
175156

176157
```ruby
177-
response = fcm.send_with_notification_key("/topics/yourTopic",
178-
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)
179164
```
180165

181-
Or you can use the helper:
166+
Or you can use the `fcm.send_to_topic` helper:
182167

183168
```ruby
184169
response = fcm.send_to_topic("yourTopic",
185-
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+
)
186195
```
187196

188197
### Sending to Multiple Topics
189198

190-
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_:
191200

192201
```
193202
'TopicA' in topics && ('TopicB' in topics || 'TopicC' in topics)
@@ -223,18 +232,38 @@ Given a registration token and a topic name, you can add the token to the topic
223232

224233
```ruby
225234
topic = "YourTopic"
226-
registration_id= "12" # a client registration tokens
227-
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)
228239
```
229240

230241
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)
231242

232243
```ruby
233244
topic = "YourTopic"
234-
registration_ids= ["4", "8", "15", "16", "23", "42"] # an array of one or more client registration tokens
235-
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)
236247
# or unsubscription
237-
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)
238267
```
239268

240269
## Mobile Clients
@@ -245,6 +274,20 @@ The guide to set up an iOS app to get notifications is here: [Setting up a FCM C
245274

246275
## ChangeLog
247276

277+
### 2.0.0
278+
#### Breaking Changes
279+
- Remove deprecated `API_KEY`
280+
- Remove deprecated `send` method
281+
- Remove deprecated `send_with_notification_key` method
282+
- Remove `subscribe_instance_id_to_topic` method
283+
- Remove `unsubscribe_instance_id_from_topic` method
284+
- Remove `batch_subscribe_instance_ids_to_topic` method
285+
- Remove `batch_unsubscribe_instance_ids_from_topic` method
286+
287+
#### Supported Features
288+
- Add HTTP v1 API support for `send_to_topic_condition` method
289+
- Add HTTP v1 API support for `send_to_topic` method
290+
248291
### 1.0.8
249292
- caches calls to `Google::Auth::ServiceAccountCredentials` #103
250293
- Allow `faraday` versions from 1 up to 2 #101

0 commit comments

Comments
 (0)