larpon / QtFirebase

An effort to bring Google's Firebase C++ API to Qt + QML
MIT License
284 stars 83 forks source link

Cloud messaging and Subscriptions to "Topics" #129

Closed daljit97 closed 5 years ago

daljit97 commented 5 years ago

Firebase Cloud Messaging allows the user to subscribe to "Topics" as mentioned here. Currently there doesn't seem to be support in QtFirebase to subscribe to Topics. I was wondering if it was possible to implement this. Looking here it seems this is just a matter of calling:

::firebase::messaging::Subscribe("example");

However, I am not familiar with the Firebase C++ APIs so I am not sure if this is the case. Currently as a workaround, I am using the Instance ID APIs but would be nice to able to do this using QtFirebase directly.

daljit97 commented 5 years ago

Ok so I tried to implement this in C++ and this works:

void QtFirebaseMessaging::subscribe(const QString &topic)
{
    auto result = messaging::Subscribe(topic.toUtf8());

    result.OnCompletion([this, topic](const firebase::FutureBase &completed_future){
        if(completed_future.error() != messaging::kErrorNone){
            emit subscribeSuccess(topic);
        }
        else{
            emit subscribeFailed(topic);
        }
    });
}

void QtFirebaseMessaging::unsubscribe(const QString &topic)
{
    auto result = messaging::Unsubscribe(topic.toUtf8());

    result.OnCompletion([this, topic](const firebase::FutureBase &completed_future){
        if(completed_future.error() != messaging::kErrorNone){
            emit unsubscribeSuccess(topic);
        }
        else{
            emit unsubscribeFailed(topic);
        }
    });
}

I think 4 signals fo subscribing/unsubscribing might be too much but it does the job.

larpon commented 5 years ago

@daljit97 - awesome! Good thing you found a workaround. I'll see if I can get it merged soon!

larpon commented 5 years ago

Implemented in v1.4.3