Skip to content

Commit 012356e

Browse files
authored
docs(push): unsubscribe of push token was not called properly (#1786)
* fix(docs): unsubscribe of push token was not called properly * fix(docs-v1): unsubscribe of push token was not called properly
1 parent 0efadaf commit 012356e

File tree

4 files changed

+18
-10
lines changed

4 files changed

+18
-10
lines changed

docusaurus/docs/reactnative/guides/push_notifications_v1.mdx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,11 +118,13 @@ const requestPermission = async () => {
118118

119119
const App = () => {
120120
const [isReady, setIsReady] = useState(false);
121+
const unsubscribeTokenRefreshListenerRef = useRef<() => void>();
121122

122123
useEffect(() => {
123-
let unsubscribeTokenRefreshListener;
124124
// Register FCM token with stream chat server.
125125
const registerPushToken = async () => {
126+
// unsubscribe any previous listener
127+
unsubscribeTokenRefreshListenerRef.current?.();
126128
const token = await messaging().getToken();
127129
await client.addDevice(token, 'firebase');
128130

@@ -144,7 +146,7 @@ const App = () => {
144146

145147
return async () => {
146148
await client?.disconnectUser();
147-
unsubscribeTokenRefreshListener?.();
149+
unsubscribeTokenRefreshListenerRef.current?.();
148150
};
149151
}, []);
150152

docusaurus/docs/reactnative/guides/push_notifications_v2.mdx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,11 +138,13 @@ const requestPermission = async () => {
138138

139139
const App = () => {
140140
const [isReady, setIsReady] = useState(false);
141+
const unsubscribeTokenRefreshListenerRef = useRef<() => void>();
141142

142143
useEffect(() => {
143-
let unsubscribeTokenRefreshListener;
144144
// Register FCM token with stream chat server.
145145
const registerPushToken = async () => {
146+
// unsubscribe any previous listener
147+
unsubscribeTokenRefreshListenerRef.current?.();
146148
const token = await messaging().getToken();
147149
const push_provider = 'firebase';
148150
const push_provider_name = 'MyRNAppFirebasePush'; // name an alias for your push provider (optional)
@@ -161,7 +163,7 @@ const App = () => {
161163
}
162164
};
163165

164-
unsubscribeTokenRefreshListener = messaging().onTokenRefresh(async newToken => {
166+
unsubscribeTokenRefreshListenerRef.current = messaging().onTokenRefresh(async newToken => {
165167
await Promise.all([
166168
removeOldToken(),
167169
client.addDevice(newToken, push_provider, USER_ID, push_provider_name),
@@ -182,7 +184,7 @@ const App = () => {
182184

183185
return async () => {
184186
await client?.disconnectUser();
185-
unsubscribeTokenRefreshListener?.();
187+
unsubscribeTokenRefreshListenerRef.current?.();
186188
};
187189
}, []);
188190

docusaurus/reactnative_versioned_docs/version-4.x.x/guides/push_notifications_v1.mdx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,11 +118,13 @@ const requestPermission = async () => {
118118

119119
const App = () => {
120120
const [isReady, setIsReady] = useState(false);
121+
const unsubscribeTokenRefreshListenerRef = useRef<() => void>();
121122

122123
useEffect(() => {
123-
let unsubscribeTokenRefreshListener;
124124
// Register FCM token with stream chat server.
125125
const registerPushToken = async () => {
126+
// unsubscribe any previous listener
127+
unsubscribeTokenRefreshListenerRef.current?.();
126128
const token = await messaging().getToken();
127129
await client.addDevice(token, 'firebase');
128130

@@ -144,7 +146,7 @@ const App = () => {
144146

145147
return async () => {
146148
await client?.disconnectUser();
147-
unsubscribeTokenRefreshListener?.();
149+
unsubscribeTokenRefreshListenerRef.current?.();
148150
};
149151
}, []);
150152

docusaurus/reactnative_versioned_docs/version-4.x.x/guides/push_notifications_v2.mdx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,11 +133,13 @@ const requestPermission = async () => {
133133

134134
const App = () => {
135135
const [isReady, setIsReady] = useState(false);
136+
const unsubscribeTokenRefreshListenerRef = useRef<() => void>();
136137

137138
useEffect(() => {
138-
let unsubscribeTokenRefreshListener;
139139
// Register FCM token with stream chat server.
140140
const registerPushToken = async () => {
141+
// unsubscribe any previous listener
142+
unsubscribeTokenRefreshListenerRef.current?.();
141143
const token = await messaging().getToken();
142144
const push_provider = 'firebase';
143145
const push_provider_name = 'MyRNAppFirebasePush'; // name an alias for your push provider (optional)
@@ -156,7 +158,7 @@ const App = () => {
156158
}
157159
};
158160

159-
unsubscribeTokenRefreshListener = messaging().onTokenRefresh(async newToken => {
161+
unsubscribeTokenRefreshListenerRef.current = messaging().onTokenRefresh(async newToken => {
160162
await Promise.all([
161163
removeOldToken(),
162164
client.addDevice(newToken, push_provider, USER_ID, push_provider_name),
@@ -177,7 +179,7 @@ const App = () => {
177179

178180
return async () => {
179181
await client?.disconnectUser();
180-
unsubscribeTokenRefreshListener?.();
182+
unsubscribeTokenRefreshListenerRef.current?.();
181183
};
182184
}, []);
183185

0 commit comments

Comments
 (0)