0
# Token Management
1
2
FCM token management for device registration and authentication with Firebase Cloud Messaging servers. Tokens are unique identifiers for each app installation and are required for sending targeted messages.
3
4
## Capabilities
5
6
### Get FCM Token
7
8
Retrieves the current FCM registration token for the device. This token should be sent to your server for targeting push notifications.
9
10
```typescript { .api }
11
/**
12
* Returns an FCM token for this device
13
* @param options - Optional configuration for token retrieval
14
* @returns Promise resolving to the FCM token string
15
*/
16
function getToken(options?: GetTokenOptions & NativeTokenOptions): Promise<string>;
17
18
interface GetTokenOptions {
19
/** VAPID key for web push (web only) */
20
vapidKey?: string;
21
/** Service worker registration for web push (web only) */
22
serviceWorkerRegistration?: ServiceWorkerRegistration;
23
}
24
25
interface NativeTokenOptions {
26
/** Firebase app name (Android only) */
27
appName?: string;
28
/** Sender ID for push notifications (iOS only) */
29
senderId?: string;
30
}
31
```
32
33
**Usage Examples:**
34
35
```typescript
36
import messaging from '@react-native-firebase/messaging';
37
38
// Get default token
39
const token = await messaging().getToken();
40
console.log('FCM Token:', token);
41
42
// Get token with custom sender ID (iOS)
43
const tokenWithSender = await messaging().getToken({
44
senderId: 'your-sender-id'
45
});
46
47
// Modular API
48
import { getMessaging, getToken } from '@react-native-firebase/messaging';
49
const messagingInstance = getMessaging();
50
const token = await getToken(messagingInstance);
51
```
52
53
### Delete FCM Token
54
55
Removes access to an FCM token, causing messages sent to this token to fail. Useful for logout scenarios or when uninstalling push notifications.
56
57
```typescript { .api }
58
/**
59
* Removes access to an FCM token previously authorized
60
* @param options - Optional configuration for token deletion
61
* @returns Promise that resolves when token is deleted
62
*/
63
function deleteToken(options?: NativeTokenOptions): Promise<void>;
64
```
65
66
**Usage Examples:**
67
68
```typescript
69
import messaging from '@react-native-firebase/messaging';
70
71
// Delete default token
72
await messaging().deleteToken();
73
74
// Delete token with specific options
75
await messaging().deleteToken({
76
appName: 'secondary-app', // Android
77
senderId: 'your-sender-id' // iOS
78
});
79
80
// Modular API
81
import { getMessaging, deleteToken } from '@react-native-firebase/messaging';
82
const messagingInstance = getMessaging();
83
await deleteToken(messagingInstance);
84
```
85
86
### Token Refresh Listener
87
88
Monitors token refresh events. Tokens can be refreshed by Firebase when they expire or when the app is restored on a new device.
89
90
```typescript { .api }
91
/**
92
* Called when a new registration token is generated for the device
93
* @param listener - Callback function receiving the new token
94
* @returns Function to unsubscribe from token refresh events
95
*/
96
function onTokenRefresh(listener: (token: string) => any): () => void;
97
```
98
99
**Usage Examples:**
100
101
```typescript
102
import messaging from '@react-native-firebase/messaging';
103
104
// Listen for token refresh
105
const unsubscribe = messaging().onTokenRefresh(token => {
106
console.log('New token:', token);
107
// Send new token to your server
108
updateTokenOnServer(token);
109
});
110
111
// Stop listening
112
unsubscribe();
113
114
// Modular API
115
import { getMessaging, onTokenRefresh } from '@react-native-firebase/messaging';
116
const messagingInstance = getMessaging();
117
const unsubscribe = onTokenRefresh(messagingInstance, token => {
118
console.log('Token refreshed:', token);
119
});
120
```
121
122
### Configuration Properties
123
124
Properties for controlling token behavior and checking current status.
125
126
```typescript { .api }
127
/** Whether messaging auto initialization is enabled */
128
readonly isAutoInitEnabled: boolean;
129
130
/**
131
* Sets whether auto initialization for messaging is enabled or disabled
132
* @param enabled - Boolean to enable or disable auto initialization
133
* @returns Promise that resolves when setting is updated
134
*/
135
function setAutoInitEnabled(enabled: boolean): Promise<void>;
136
```
137
138
**Usage Examples:**
139
140
```typescript
141
import messaging from '@react-native-firebase/messaging';
142
143
// Check if auto init is enabled
144
const autoInitEnabled = messaging().isAutoInitEnabled;
145
console.log('Auto init enabled:', autoInitEnabled);
146
147
// Disable auto initialization (useful for GDPR compliance)
148
await messaging().setAutoInitEnabled(false);
149
150
// Modular API
151
import { getMessaging, isAutoInitEnabled, setAutoInitEnabled } from '@react-native-firebase/messaging';
152
const messagingInstance = getMessaging();
153
154
const enabled = isAutoInitEnabled(messagingInstance);
155
await setAutoInitEnabled(messagingInstance, false);
156
```
157
158
## Token Lifecycle
159
160
1. **Initial Token**: Generated when app first requests messaging permissions
161
2. **Token Refresh**: Automatic refresh when tokens expire or app is restored
162
3. **Token Deletion**: Manual deletion for logout or app uninstall scenarios
163
4. **Server Sync**: Tokens should be synchronized with your backend server
164
165
## Error Handling
166
167
Common token-related errors:
168
169
- **Unregistered**: Device not registered for remote notifications (iOS)
170
- **Network Error**: Unable to connect to Firebase servers
171
- **Invalid Configuration**: Missing or incorrect Firebase configuration
172
- **Permission Denied**: User has denied notification permissions