Skip to content

Commit

Permalink
Preserve Messaging token until a Listener is set (#1634)
Browse files Browse the repository at this point in the history
* Preserve Messaging token until a Listener is set

* Format file
  • Loading branch information
a-maurice authored Aug 20, 2024
1 parent dd81f2c commit 66a9570
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
28 changes: 22 additions & 6 deletions messaging/src/common.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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;
}
Expand All @@ -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 {
Expand Down
5 changes: 5 additions & 0 deletions release_build_files/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 66a9570

Please sign in to comment.