Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preserve Messaging token until a Listener is set #1634

Merged
merged 2 commits into from
Aug 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading