KOTIOS
KOTIOS

Reputation: 11194

How to list all topics of deviceToken (ID service firebase deprecated)

IID api from fcm - https://iid.googleapis.com/iid/info/IIDtoken allows to look for all topics subscribed by devicetoken. Currently since it deprecated its responding with - "Authentication using server key is deprecated. Please use OAuth2token instead."

Post generating valid OAuth2 token as well it says request Unauthorized.

Can someone please suggest whats the alternative. I have tried firebase admin sdk apis mentioned here - https://firebase.google.com/docs/cloud-messaging/manage-topics.

Firebase SDK does not have alternative api to list all topics from user deviceToken.

I have also tried to execute - pubsub.projects.topics.list api as mentioned here - https://www.any-api.com/googleapis_com/pubsub/docs/projects/pubsub_projects_topics_list. This as well didnt seems to work.

Please suggest of listing topic from user deviceToken is been fully removed post IID service is depricated.

Upvotes: 2

Views: 521

Answers (3)

smistry
smistry

Reputation: 1126

I managed to get it working like this:


        [HttpGet]
        [Route("/subscriptions")]
        public async Task<IActionResult> GetSubscriptions(
            [FromHeader(Name = "x-fcm-token")][RequiredAttribute]
            string fcmToken, [FromServices] HttpClient httpClient,
            [FromServices] IConfiguration config
        )
        {
            var token = await GetAccessTokenAsync();
            var request = new HttpRequestMessage();
            request.RequestUri = new Uri($"https://iid.googleapis.com/iid/info/{fcmToken}?details=true");
            request.Headers.TryAddWithoutValidation("Authorization", "Bearer " + token);
            request.Headers.TryAddWithoutValidation("access_token_auth", "true");
            var response = await httpClient.SendAsync(request);
            var responseContent = await response.Content.ReadAsStringAsync();
            var responseJson = JsonSerializer.Deserialize<JsonElement>(responseContent);


            try
            {
                var topics = responseJson.GetProperty("rel").GetProperty("topics").EnumerateObject().Select(e => e.Name);

                
                return new JsonResult(topics);
            }
            catch (KeyNotFoundException)
            {
                return new JsonResult(new string[] { });
            }
        }


        public static async Task<string> GetAccessTokenAsync()
        {
            try
            {
                // Load the service account key from the JSON file
                GoogleCredential credential;
                using (var stream = new FileStream("google-creds.json", FileMode.Open, FileAccess.Read))
                {
                    credential = GoogleCredential.FromStream(stream)
                        .CreateScoped("https://www.googleapis.com/auth/firebase.messaging");
                }

                // Create a JWT token for authentication
                var token = await credential.UnderlyingCredential.GetAccessTokenForRequestAsync();

                return token;
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error obtaining access token: " + ex.Message);
                throw;
            }
        }

Upvotes: 0

Mohsin Ashfaq
Mohsin Ashfaq

Reputation: 1

Set this parameter in the header. access_token_auth: true.

details: set this query parameter to true to get FCM topic subscription information (if any) associated with this token. https://iid.googleapis.com/iid/info/IIDtoken?details=true

Upvotes: 0

anish adhikari
anish adhikari

Reputation: 1

You would need to manage topic subscriptions on the server side by keeping track of which topics a device token is subscribed to in your own database. always manage and track form server side.

Upvotes: -1

Related Questions