QtFirebase icon indicating copy to clipboard operation
QtFirebase copied to clipboard

How to connect to MessageReceived signal from C++?

Open TheSDTM opened this issue 7 years ago • 7 comments

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.

TheSDTM avatar Apr 22 '18 10:04 TheSDTM

You want to connect from outside?

isipisi89 avatar Apr 24 '18 09:04 isipisi89

Yes

TheSDTM avatar Apr 24 '18 10:04 TheSDTM

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

larpon avatar May 01 '18 08:05 larpon

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.

adolby avatar May 02 '18 20:05 adolby

@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

larpon avatar May 02 '18 20:05 larpon

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.

adolby avatar May 11 '18 16:05 adolby

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();

TLCoetzer avatar May 01 '20 07:05 TLCoetzer