HomeProjectsBlog

How to get all subscribed topics from firebase cloud messaging

September 14, 2024

To get information about an app instance ID e.g topics subscribed , platform

const iidToken = "YOUR_IID_TOKEN"; // Replace with your actual Instance ID token
const accessToken = "YOUR_ACCESS_TOKEN"; // Replace with the OAuth2 access token

// API endpoint for FCM token info
const url = `https://iid.googleapis.com/iid/info/${iidToken}`;

const headers = {
  Authorization: `Bearer ${accessToken}`,
  "Content-Type": "application/json",
  // This indicates that OAuth2 authentication is being used
  access_token_auth: "true",
};

// Set the optional query parameter to get details (e.g., FCM topic subscription info)
const queryParams = new URLSearchParams({
  details: "true", // Set to 'true' to get additional details, or omit to get default info
}).toString();

// Make the fetch request to the FCM Instance ID API
fetch(`${url}?${queryParams}`, {
  method: "GET",
  headers: headers,
})
  .then((response) => {
    if (!response.ok) {
      throw new Error("Something went wrong");
    }
    return response.json(); // Parse JSON response
  })
  .then((data) => {
    console.log("FCM Token Info:", data);
  })
  .catch((error) => {
    console.error("Error fetching FCM token info");
  });

Happy coding.