You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+94-51Lines changed: 94 additions & 51 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -27,7 +27,6 @@ To use this gem, you need to instantiate a client with your firebase credentials
27
27
28
28
```ruby
29
29
fcm =FCM.new(
30
-
API_TOKEN,
31
30
GOOGLE_APPLICATION_CREDENTIALS_PATH,
32
31
FIREBASE_PROJECT_ID
33
32
)
@@ -40,7 +39,6 @@ The easiest way to provide them is to pass here an absolute path to a file with
40
39
41
40
```ruby
42
41
fcm =FCM.new(
43
-
API_TOKEN,
44
42
'/path/to/credentials.json',
45
43
FIREBASE_PROJECT_ID
46
44
)
@@ -50,7 +48,6 @@ As per their secret nature, you might not want to have them in your repository.
50
48
51
49
```ruby
52
50
fcm =FCM.new(
53
-
API_TOKEN,
54
51
StringIO.new(ENV.fetch('FIREBASE_CREDENTIALS')),
55
52
FIREBASE_PROJECT_ID
56
53
)
@@ -65,13 +62,13 @@ To migrate to HTTP v1 see: https://firebase.google.com/docs/cloud-messaging/migr
65
62
66
63
```ruby
67
64
fcm =FCM.new(
68
-
API_TOKEN,
69
65
GOOGLE_APPLICATION_CREDENTIALS_PATH,
70
66
FIREBASE_PROJECT_ID
71
67
)
72
68
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)",
75
72
'data': {
76
73
payload: {
77
74
data: {
@@ -97,58 +94,42 @@ message = {
97
94
}
98
95
}
99
96
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)
125
98
```
126
99
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
-
129
100
## Device Group Messaging
130
101
131
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.
132
103
104
+
The maximum number of members allowed for a notification key is 20.
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:
136
110
111
+
`project_id` is the SENDER_ID in your cloud settings.
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
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-_.~%]+"`:
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)
179
164
```
180
165
181
-
Or you can use the helper:
166
+
Or you can use the `fcm.send_to_topic`helper:
182
167
183
168
```ruby
184
169
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
+
)
186
195
```
187
196
188
197
### Sending to Multiple Topics
189
198
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_:
191
200
192
201
```
193
202
'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
223
232
224
233
```ruby
225
234
topic ="YourTopic"
226
-
registration_id="12"# a client registration tokens
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)
231
242
232
243
```ruby
233
244
topic ="YourTopic"
234
-
registration_ids= ["4", "8", "15", "16", "23", "42"] # an array of one or more client registration tokens
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
0 commit comments