@@ -724,6 +724,11 @@ static void InstallationsGetToken() {
724724
725725 result.OnCompletion (
726726 [](const Future<std::string>& result, void * voidptr) {
727+ if (g_registration_token_mutex) {
728+ MutexLock lock (*g_registration_token_mutex);
729+ g_registration_token_received = true ;
730+ HandlePendingSubscriptions ();
731+ }
727732 NotifyListenerOnTokenReceived (result.result ()->c_str ());
728733 },
729734 nullptr );
@@ -868,6 +873,9 @@ static const char kErrorMessageNoRegistrationToken[] =
868873 " Cannot update subscription when SetTokenRegistrationOnInitEnabled is set "
869874 " to false." ;
870875
876+ static const char kErrorMessageSubscriptionUnknown [] =
877+ " Cannot update subscription for unknown reason." ;
878+
871879Future<void > Subscribe (const char * topic) {
872880 FIREBASE_ASSERT_MESSAGE_RETURN (Future<void >(), internal::IsInitialized (),
873881 kMessagingNotInitializedError );
@@ -882,6 +890,11 @@ Future<void> Subscribe(const char* topic) {
882890 kErrorMessageNoRegistrationToken );
883891 } else if (g_pending_subscriptions) {
884892 g_pending_subscriptions->push_back (PendingTopic (topic, handle));
893+ } else {
894+ // This shouldn't happen, since g_pending_subscriptions should be valid if
895+ // here, but handle it to prevent abandoning the Future in case something
896+ // happens.
897+ api->Complete (handle, kErrorUnknown , kErrorMessageSubscriptionUnknown );
885898 }
886899 return MakeFuture (api, handle);
887900}
@@ -907,6 +920,11 @@ Future<void> Unsubscribe(const char* topic) {
907920 kErrorMessageNoRegistrationToken );
908921 } else if (g_pending_unsubscriptions) {
909922 g_pending_unsubscriptions->push_back (PendingTopic (topic, handle));
923+ } else {
924+ // This shouldn't happen, since g_pending_unsubscriptions should be valid if
925+ // here, but handle it to prevent abandoning the Future in case something
926+ // happens.
927+ api->Complete (handle, kErrorUnknown , kErrorMessageSubscriptionUnknown );
910928 }
911929 return MakeFuture (api, handle);
912930}
0 commit comments