diff --git a/messaging/src/common.cc b/messaging/src/common.cc index 43eabe1aa2..8d805c1098 100644 --- a/messaging/src/common.cc +++ b/messaging/src/common.cc @@ -48,6 +48,8 @@ static Listener* g_listener = nullptr; // Keep track of the most recent token received. static std::string* g_prev_token_received = nullptr; +// Keep track if that token was receieved without a listener set. +static bool g_has_pending_token = false; namespace internal { @@ -91,11 +93,18 @@ Listener* SetListener(Listener* listener) { g_prev_token_received = new std::string; } g_listener = listener; + // If we have a pending token, send it before notifying other systems about + // the listener. + if (g_listener && g_has_pending_token && g_prev_token_received) { + g_listener->OnTokenReceived(g_prev_token_received->c_str()); + g_has_pending_token = false; + } NotifyListenerSet(listener); if (!listener && g_prev_token_received) { std::string* ptr = g_prev_token_received; g_prev_token_received = nullptr; delete ptr; + g_has_pending_token = false; } return previous_listener; } @@ -119,13 +128,20 @@ void NotifyListenerOnTokenReceived(const char* token) { MutexLock lock(g_listener_lock); // If we have a previous token that we've notified any listener about, check // to ensure no repeat. - if (g_prev_token_received) { - if (*g_prev_token_received == token) { - return; - } - *g_prev_token_received = token; + if (g_prev_token_received && *g_prev_token_received == token) { + return; + } + + if (!g_prev_token_received) g_prev_token_received = new std::string; + *g_prev_token_received = token; + + if (g_listener) { + g_listener->OnTokenReceived(token); + } else { + // Set so that if a listener is set in the future, it will be sent + // the token, since the underlying platform won't send it again. + g_has_pending_token = true; } - if (g_listener) g_listener->OnTokenReceived(token); } class PollableListenerImpl { diff --git a/release_build_files/readme.md b/release_build_files/readme.md index cb667514f9..35970bce23 100644 --- a/release_build_files/readme.md +++ b/release_build_files/readme.md @@ -631,6 +631,11 @@ workflow use only during the development of your app, not for publicly shipping code. ## Release Notes +### Upcoming Release +- Changes + - Messaging: Changed SetListener to send the last token received + before the listener was set. + ### 12.2.0 - Changes - General (iOS): Update to Firebase Cocoapods version 11.0.0.