Skip to content

Commit 66a9570

Browse files
authored
Preserve Messaging token until a Listener is set (#1634)
* Preserve Messaging token until a Listener is set * Format file
1 parent dd81f2c commit 66a9570

File tree

2 files changed

+27
-6
lines changed

2 files changed

+27
-6
lines changed

messaging/src/common.cc

+22-6
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ static Listener* g_listener = nullptr;
4848

4949
// Keep track of the most recent token received.
5050
static std::string* g_prev_token_received = nullptr;
51+
// Keep track if that token was receieved without a listener set.
52+
staticbool g_has_pending_token = false;
5153

5254
namespaceinternal {
5355

@@ -91,11 +93,18 @@ Listener* SetListener(Listener* listener) {
9193
g_prev_token_received = new std::string;
9294
}
9395
g_listener = listener;
96+
// If we have a pending token, send it before notifying other systems about
97+
// the listener.
98+
if (g_listener && g_has_pending_token && g_prev_token_received) {
99+
g_listener->OnTokenReceived(g_prev_token_received->c_str());
100+
g_has_pending_token = false;
101+
}
94102
NotifyListenerSet(listener);
95103
if (!listener && g_prev_token_received) {
96104
std::string* ptr = g_prev_token_received;
97105
g_prev_token_received = nullptr;
98106
delete ptr;
107+
g_has_pending_token = false;
99108
}
100109
return previous_listener;
101110
}
@@ -119,13 +128,20 @@ void NotifyListenerOnTokenReceived(const char* token) {
119128
MutexLock lock(g_listener_lock);
120129
// If we have a previous token that we've notified any listener about, check
121130
// to ensure no repeat.
122-
if (g_prev_token_received) {
123-
if (*g_prev_token_received == token) {
124-
return;
125-
}
126-
*g_prev_token_received = token;
131+
if (g_prev_token_received && *g_prev_token_received == token) {
132+
return;
133+
}
134+
135+
if (!g_prev_token_received) g_prev_token_received = new std::string;
136+
*g_prev_token_received = token;
137+
138+
if (g_listener) {
139+
g_listener->OnTokenReceived(token);
140+
} else {
141+
// Set so that if a listener is set in the future, it will be sent
142+
// the token, since the underlying platform won't send it again.
143+
g_has_pending_token = true;
127144
}
128-
if (g_listener) g_listener->OnTokenReceived(token);
129145
}
130146

131147
classPollableListenerImpl {

release_build_files/readme.md

+5
Original file line numberDiff line numberDiff line change
@@ -631,6 +631,11 @@ workflow use only during the development of your app, not for publicly shipping
631631
code.
632632

633633
## Release Notes
634+
### Upcoming Release
635+
- Changes
636+
- Messaging: Changed SetListener to send the last token received
637+
before the listener was set.
638+
634639
### 12.2.0
635640
- Changes
636641
- General (iOS): Update to Firebase Cocoapods version 11.0.0.

0 commit comments

Comments
 (0)
close