Skip to content

[Messaging] Fix subscription not completing sometimes #1718

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

Merged
merged 2 commits into from
May 29, 2025
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
18 changes: 18 additions & 0 deletions messaging/src/android/cpp/messaging.cc
Original file line number Diff line number Diff line change
Expand Up @@ -724,6 +724,11 @@ static void InstallationsGetToken() {

result.OnCompletion(
[](const Future<std::string>& result, void* voidptr) {
if (g_registration_token_mutex) {
MutexLock lock(*g_registration_token_mutex);
g_registration_token_received = true;
HandlePendingSubscriptions();
}
NotifyListenerOnTokenReceived(result.result()->c_str());
},
nullptr);
Expand Down Expand Up @@ -868,6 +873,9 @@ static const char kErrorMessageNoRegistrationToken[] =
"Cannot update subscription when SetTokenRegistrationOnInitEnabled is set "
"to false.";

static const char kErrorMessageSubscriptionUnknown[] =
"Cannot update subscription for unknown reason.";

Future<void> Subscribe(const char* topic) {
FIREBASE_ASSERT_MESSAGE_RETURN(Future<void>(), internal::IsInitialized(),
kMessagingNotInitializedError);
Expand All @@ -882,6 +890,11 @@ Future<void> Subscribe(const char* topic) {
kErrorMessageNoRegistrationToken);
} else if (g_pending_subscriptions) {
g_pending_subscriptions->push_back(PendingTopic(topic, handle));
} else {
// This shouldn't happen, since g_pending_subscriptions should be valid if
// here, but handle it to prevent abandoning the Future in case something
// happens.
api->Complete(handle, kErrorUnknown, kErrorMessageSubscriptionUnknown);
}
return MakeFuture(api, handle);
}
Expand All @@ -907,6 +920,11 @@ Future<void> Unsubscribe(const char* topic) {
kErrorMessageNoRegistrationToken);
} else if (g_pending_unsubscriptions) {
g_pending_unsubscriptions->push_back(PendingTopic(topic, handle));
} else {
// This shouldn't happen, since g_pending_unsubscriptions should be valid if
// here, but handle it to prevent abandoning the Future in case something
// happens.
api->Complete(handle, kErrorUnknown, kErrorMessageSubscriptionUnknown);
}
return MakeFuture(api, handle);
}
Expand Down
2 changes: 2 additions & 0 deletions release_build_files/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -664,6 +664,8 @@ code.
library and to the firebase::ump namespace. The version in the
GMA library (in firebase::gma::ump) has been deprecated and will
be removed soon.
- Messaging (Android): Fix issue with the Subscribe Future not completing
when a cached token is available.

### 12.7.0
- Changes
Expand Down
Loading