Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there anyway to access the Firebase Topics a user is subscribed to?

I'm currently trying to migrate my app to firebase and I'm looking for the Firebase equivalent of Parse Installations and Channels.

What I've found is that we are supposed to use topics however in my app "subscribing" and "unsubscribing" to topics is common yet there is no way (that I have found) to see what topics a user is subscribed to. Any ideas?

I've looked through the Firebase documentation but I'm new to Firebase so maybe someone with more experience would know: https://firebase.google.com/docs/cloud-messaging/android/topic-messaging#managing_topic_subscriptions_from_the_server

Thanks in advance for any help!

like image 957
Kevin R Avatar asked Aug 15 '16 02:08

Kevin R


Video Answer


2 Answers

FCM topic subscriptions are based on an application's Instance ID, so when you subscribe or unsubscribe to or from a topic the IID is used.

You can use the Instance ID API to get information about a particular IID, this information includes the topics that the IID is currently subscribed to. See the reference

like image 198
Arthur Thompson Avatar answered Oct 11 '22 17:10

Arthur Thompson


You have to make a request to the Instance ID API. I created an extension

https://gist.github.com/eduardo22i/0ee8960c36659b17fbc538964e929cac

extension Messaging {

static private let accessToken = "" //Server Web Key

struct Topic : Decodable {
    var name : String?
    var addDate : String?
}

struct Rel : Decodable {
    var topics = [Topic]()

    init(from decoder: Decoder) throws {
        let container = try decoder.container(keyedBy: CodingKeys.self)
        let relContainer = try container.nestedContainer(keyedBy: CodingKeys.self, forKey: .rel)
        let topicsContainer = try relContainer.nestedContainer(keyedBy: CodingKeys.self, forKey: .topics )

        for key in topicsContainer.allKeys {
            var topic = Topic()
            topic.name = key.stringValue

            let topicContainer = try? topicsContainer.nestedContainer(keyedBy: CodingKeys.self, forKey: key)
            topic.addDate = try! topicContainer?.decode(String.self, forKey: .addDate)

            self.topics.append(topic)
        }
    }

    struct CodingKeys : CodingKey {
        var stringValue: String
        init?(stringValue: String) {
            self.stringValue = stringValue
        }
        var intValue: Int? { return nil }
        init?(intValue: Int) { return nil }

        static let rel = CodingKeys(stringValue: "rel")!
        static let topics = CodingKeys(stringValue: "topics")!
        static let addDate = CodingKeys(stringValue: "addDate")!
    }
}

func loadTopics(block : @escaping (_ topics: [Messaging.Topic]?, _ error: Error?) -> Void ) {
    if let token = InstanceID.instanceID().token() {
        let url = URL(string: "https://iid.googleapis.com/iid/info/\(token)?details=true")!
        var request = URLRequest(url: url)
        request.addValue("key=\(Messaging.accessToken)", forHTTPHeaderField: "Authorization")
        let dataTask = URLSession.shared.dataTask(with: request) { (data, response, error) in
        DataManager.shared.make(request: urlRequest, block: { (data, error) in
            if let data = data {
                let decoder = JSONDecoder()
                let rel = try? decoder.decode(Rel.self, from: data)
                block(rel?.topics, error)
            } else {
                block(nil, error)
            }
        }

        dataTask.resume()
    }
}
}

// Usage

Messaging.messaging().loadTopics { (topics, error) in
    topics?.forEach({ (topic) in
        print(topic.name)
    })
}
like image 39
Eduardo Irias Avatar answered Oct 11 '22 17:10

Eduardo Irias