Skip to content

Commit 0e26fca

Browse files
ahron1mikehardy
andauthored
docs(messaging): add token management info to server integration doc (#5427)
Added additional section about handling tokens while logging out users, and about tokens being associated with the app installation instance. As discussed in the discussions #5423 Co-authored-by: Mike Hardy <[email protected]>
1 parent e212c3f commit 0e26fca

File tree

1 file changed

+20
-7
lines changed

1 file changed

+20
-7
lines changed

docs/messaging/server-integration.md

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ Firebase provides a number of SDKs in different languages such as [Node.JS](http
1717
[HTTP](https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages). These methods allow you to send messages
1818
directly to your user's devices via the FCM servers.
1919

20-
# Device tokens
20+
## Device tokens
2121

2222
To send a message to a device, you must access its unique token. A token is automatically generated by the device and
2323
can be accessed using the Cloud Messaging module. The token should be saved inside of your systems data-store and should
@@ -163,7 +163,20 @@ function App() {
163163
Your application code can then handle messages as you see fit; updating local cache, displaying a [notification](/messaging/notifications)
164164
or updating UI. The possibilities are endless!
165165

166-
# Send messages to topics
166+
## Signing out users
167+
168+
Firebase Cloud Messaging tokens are associated with the instance of the installed app. By default, only token expiration or uninstalling/reinstalling the app will generate a fresh token.
169+
170+
This means that by default, if your app has users and you allow them to log out and log in on the same app on the same device, the same FCM token will be used for both users. Usually this is not what you want, so you must take care to cycle the FCM token at the same time you handle user logout/login.
171+
172+
How and when you invalidate a token and generate a new one will be specific to your project, but a common pattern is to delete the FCM token during logout and update your back end to remove ti, then to fetch the FCM token during login and update your back end systems to associate the new token with the logged in user.
173+
174+
<https://rnfirebase.io/reference/messaging#deleteToken>
175+
<https://rnfirebase.io/reference/messaging#getToken>
176+
177+
Note that when a token is deleted by calling the `deleteToken` method, it is immediately and permanently invalid.
178+
179+
## Send messages to topics
167180

168181
When devices [subscribe to topics](/messaging/usage#topics), you can send messages without specifying/storing any device
169182
tokens.
@@ -198,8 +211,8 @@ To send a message to a combination of topics, specify a condition, which is a bo
198211
topics. For example, the following condition will send messages to devices that are subscribed to `weather` and either `news`
199212
or `traffic`:
200213

201-
```
202-
'weather' in topics && ('news' in topics || 'traffic' in topics)
214+
```json
215+
condition: "'weather' in topics && ('news' in topics || 'traffic' in topics)"
203216
```
204217

205218
To send a message to this condition, replace the `topic` key with `condition`:
@@ -225,11 +238,11 @@ admin
225238
});
226239
```
227240

228-
# Send messages with image
241+
## Send messages with image
229242

230243
Both the Notifications composer and the FCM API support image links in the message payload.
231244

232-
## iOS
245+
### iOS
233246

234247
To successfully send an image using the Admin SDK it's important that the `ApnsConfig` options are set:
235248

@@ -254,7 +267,7 @@ const payload = {
254267

255268
> Check out the [official Firebase documentation](https://firebase.google.com/docs/cloud-messaging/ios/send-image) to see the list of available configuration for iOS.
256269
257-
## Android
270+
### Android
258271

259272
Similarly to iOS, some configurations specific to Android are needed:
260273

0 commit comments

Comments
 (0)