larpon / QtFirebase

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

Push notifications while in foreground #158

Closed carlonluca closed 3 years ago

carlonluca commented 3 years ago

Hello! I'm using the test app on iOS and Android. I noticed that, while notifications always arrive when the app is in the background or completely closed, I'm not getting any signal when the app is in the foreground in Android.

On iOS, it seems a notification arrives in QML while the app is in the foreground, but on Android I'm not getting any signal: I'd expect a signal here https://github.com/Larpon/QtFirebaseExample/blob/master/App/qml/pages/Messaging.qml#L95 or here https://github.com/Larpon/QtFirebaseExample/blob/master/App/qml/pages/Messaging.qml#L92, but I can get nothing.

Is this expected? Am I the only one with this problem? Is some broadcast receiver or similar needed to get that notification? Thank you.

larpon commented 3 years ago

As far as I have been able to figure out - it's expected and a limitation of firebase - others may have experienced different behaviour on different hardware 🤷‍♂️

carlonluca commented 3 years ago

Thank you for your answer. In fact, I do not see any call to OnMessage from firebase. But this is considerably weird, isn't it? On iOS this seems to work properly. Do you happen to have a reference about the limitation? I suspected something like this but I couldn't find a confirmation.

So, do you think it is impossible to know that the notification arrived and the data it contains while the app is running? Or do you happen to know if there is a way around this somehow?

Thank you very much for your quick answer. At least I won't waste my time trying to find something that is not there :-)

larpon commented 3 years ago

It should work with OnMessageRecieved in the normal (non cpp) firebase - I guess you should be able to handle the callback then - I'm not sure why it's missing from QtFirebase currently. I haven't been active with this project in a long time now - so I'm not very up-to-date on how everything runs 😬

carlonluca commented 3 years ago

But is the non-cpp firebase included in QtFirebase? So would it be possible to add it to QtFirebase immediately somehow or you mean that it would be necessary to add the firebase Java library? Thanks.

larpon commented 3 years ago

No I meant you'll probably have to try and find out if firebase-cpp has this feature and then implement it yourself - we have some code for this so maybe it's worth looking into at that call.

OTOH other users seem to have it working

carlonluca commented 3 years ago

The OnMessage callback seems not to be called in any way in this situation. At this point, this should probably be asked in the firebase repo. Thanks for your help!

larpon commented 3 years ago

Happy to help 🙂

I'm uncertain if OnMessage and OnMessadeReceived is the same functionality between the cpp/java versions - but yes it's probably a good idea to find out if it's supposed to work in the firebase-cpp version 🙂

carlonluca commented 3 years ago

Hello, it turned out that receiving notifications when the app is in foreground is possible and it should work in the current QtFirebase example. The problem that led to this issue is that the current version of Qt Creator happily removes any intent filter defined inside messaging services as soon as the manifest is opened... without telling anyone... The missing intent filter results in the problem described above. If the manifest remains just like in the git version of the QtFirebase example, everything works.

On the other hand, while everything works while the app is dead or in the foreground, I still could not get data from the opened notification when the app was running in the background. It is possible that this is the intended behavior though: https://stackoverflow.com/questions/39529317/firebase-notification-is-not-working-when-activity-is-in-background-or-not/45292455#45292455. When both "notification" and "data" keys are in the notification, then data is not received by the app. The only quick fix I could find is to reinit QtFirebaseMessaging when the app is back from the background. This is a bad solution though, not sure if it makes sense to send a pull request.

I guess this issue does not actually exist, so I think I can close it. Thanks for your assistance.

larpon commented 3 years ago

@carlonluca thanks for investigating and getting back with your results!

I'm not sure how to solve the data issue - we shouldn't reinit as a part of a solution since we'd just introduce confusion - just like Qt Creator did in this case.

Thanks for getting back and supporting the community!