Skip to content

Commit a9ce7cc

Browse files
committed
Preserve Messaging token until a Listener is set
1 parent dd81f2c commit a9ce7cc

File tree

2 files changed

+26
-6
lines changed

2 files changed

+26
-6
lines changed

messaging/src/common.cc

Lines changed: 21 additions & 6 deletions
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+
static bool g_has_pending_token = false;
5153

5254
namespace internal {
5355

@@ -91,11 +93,17 @@ 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 the listener.
97+
if (g_listener && g_has_pending_token && g_prev_token_received) {
98+
g_listener->OnTokenReceived(g_prev_token_received->c_str());
99+
g_has_pending_token = false;
100+
}
94101
NotifyListenerSet(listener);
95102
if (!listener && g_prev_token_received) {
96103
std::string* ptr = g_prev_token_received;
97104
g_prev_token_received = nullptr;
98105
delete ptr;
106+
g_has_pending_token = false;
99107
}
100108
return previous_listener;
101109
}
@@ -119,13 +127,20 @@ void NotifyListenerOnTokenReceived(const char* token) {
119127
MutexLock lock(g_listener_lock);
120128
// If we have a previous token that we've notified any listener about, check
121129
// 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;
130+
if (g_prev_token_received && *g_prev_token_received == token) {
131+
return;
132+
}
133+
134+
if (!g_prev_token_received) g_prev_token_received = new std::string;
135+
*g_prev_token_received = token;
136+
137+
if (g_listener) {
138+
g_listener->OnTokenReceived(token);
139+
} else {
140+
// Set so that if a listener is set in the future, it will be sent
141+
// the token, since the underlying platform won't send it again.
142+
g_has_pending_token = true;
127143
}
128-
if (g_listener) g_listener->OnTokenReceived(token);
129144
}
130145

131146
class PollableListenerImpl {

release_build_files/readme.md

Lines changed: 5 additions & 0 deletions
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)