I managed to do it in iOS by sending a GET
request to https://iid.googleapis.com/iid/info
:
Models for decoding response:
struct AppInstance: Codable {
let applicationVersion: String
let gmiRegistrationId: String
let application: String
let scope: String
let authorizedEntity: String
let rel: Rel
let platform: String
}
struct Rel: Codable {
let topics: [String: AddDate]
enum CodingKeys: String, CodingKey {
case topics
}
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
topics = try container.decode([String: AddDate].self, forKey: .topics)
}
}
struct AddDate: Codable {
let addDate: String
}
Then called the following function:
func getSubscribedTopics() async -> [String]? {
let fcmToken = "YOUR_FCM_TOKEN" // received inside didReceiveRegistrationToken callback
guard var urlComponents = URLComponents(string: "https://iid.googleapis.com/iid/info/\(fcmToken)")
else { return nil }
let parameter = URLQueryItem(name: "details", value: "true")
urlComponents.queryItems = [parameter]
guard let url = urlComponents.url else { return nil }
let serverKey = "YOUR_SERVER_KEY" // from firebase console
var request = URLRequest(url: url)
request.httpMethod = "GET"
request.addValue("Bearer \(serverKey)", forHTTPHeaderField: "Authorization") // this will be deprecated
let (data, _) = try! await URLSession.shared.data(for: request)
let decoder = JSONDecoder()
let appInstance = try! decoder.decode(AppInstance.self, from: data)
let topics = appInstance.rel.topics.keys.map { $0 as String }
return topics
}
This is currently working but using the server key for authorization will be deprecated in June 2024. I should replace the server key with a token but this didn't work with me and the server responded with "Unauthorized".