Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to connect to MessageReceived signal from C++? #71

Open
TheSDTM opened this issue Apr 22, 2018 · 7 comments
Open

How to connect to MessageReceived signal from C++? #71

TheSDTM opened this issue Apr 22, 2018 · 7 comments

Comments

@TheSDTM
Copy link

TheSDTM commented Apr 22, 2018

Hello. How can I receive message (and token) inside my application? For now I do like this:

auto firebase = QtFirebaseMessaging::instance(); Notification handler; QObject::connect(firebase, &QtFirebaseMessaging::messageReceived, &handler, &Notification::messageReceived);

Where Notification is my class. But I do not receive any signal. Also, no notification when App is opened.

@isipisi89
Copy link
Collaborator

You want to connect from outside?

@TheSDTM
Copy link
Author

TheSDTM commented Apr 24, 2018

Yes

@larpon
Copy link
Owner

larpon commented May 1, 2018

If you can connect via QML it should indeed get the same results as when connecting in C++.

But I'm not sure any Qt or related code will be triggered if your app is in the background. If it's not running I don't think your code will run per design. If your app is in the background you'll get a notification that open your app.

If you're doing this from iOS please cruise issue #49 for a messy checklist of what should be ensured before messages come through

@adolby
Copy link
Contributor

adolby commented May 2, 2018

If you want to receive a token from C++ you can connect to the tokenChanged signal just as you've done above, but since the signal doesn't emit the token value as a parameter, you'll need to have access to the QtFirebase instance from inside Notification::messageReceived, so you can call the token accessor.

The messageReceived is emitted from QtFirebaseMessaging::setData, which currently only updates when the data has changed. This prevents you from sending more than one message with the same data at a time. In your case if you're trying to send the same message over and over, it will be per app run.

Since I didn't want to break the QML API and didn't have a better alternative solution ready, my solution to this problem was to comment out the if (_data != data) { block in QtFirebaseMessaging::setData. You'll also need the QtFirebase instance to access the data accessor since the signal doesn't emit the data as a parameter.

@larpon
Copy link
Owner

larpon commented May 2, 2018

@adolby - I can see why you'd need to be able to send out the same message more than once in the same app run (although it might be user bombardment, if not for testing purposes). We could add a bool for the emit upon data received. Something like alwaysEmitDataChanged with a default to false?

So we'd check: if (_data != data || _alwaysEmitDataChanged) {

It also makes sense to pass the token to the signal as a parameter - it's useful - also from QML

@adolby
Copy link
Contributor

adolby commented May 11, 2018

Yeah, the flag for always emitting would be helpful for me-- the use case is that the message is a command from a server to tell the app to synchronize data, which really doesn't need any more data then the command itself.

It's also used for sending data only and not in messages shown to the user-- I agree that that would probably be bad to continue showing the same notification message to users.

We could have just as easily added a timestamp to the message data to have avoided this issue, though, which might be a better way to have solved the problem.

@TLCoetzer
Copy link

When not running from QML but straight from c++, you need to have the following line as well because this is where QtFirebaseMessaging does some of its own connections for messages and tokens:
firebase ->componentComplete();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants