UnifiedPush / flutter-connector

Mirror of https://codeberg.org/UnifiedPush/flutter-connector
Apache License 2.0
31 stars 11 forks source link

Problems with receiving messages #80

Closed provokateurin closed 2 years ago

provokateurin commented 2 years ago

Receiving messages works right after initialization.
When restarting the app and receiving a message after that it doesn't work anymore. In the logs I can see the Android code is definitely called.
When the app is in background or closed, receiving a message also doesn't work (same as above with receiving on the Android side).

Maybe these are actually the same problem, I suspect the callback is just never called for some reason.
I tried the embedded FOSS FCM and nfty distributors, both have the same problem and from the logs I collected I'm very confident this problem is distributor independent.

I will try to investigate it a little bit and see if adapting stuff from https://github.com/firebase/flutterfire/blob/master/packages/firebase_messaging/firebase_messaging/android/src/main/java/io/flutter/plugins/firebase/messaging/FlutterFirebaseMessagingBackgroundService.java helps.

karmanyaahm commented 2 years ago

Hey👋

I'm really excited to see a Nextcloud w/ UP app. I will try to look at what might be going wrong today/tmrw.

(I'm sure @p1gp1g's android expertise will be helpful if they have time to look too)

provokateurin commented 2 years ago

I'm pretty sure something like https://github.com/OneSignal/OneSignal-Flutter-SDK/pull/464 is needed for this to work. Afaict most of that code is copied from https://github.com/firebase/flutterfire/tree/master/packages/firebase_messaging/firebase_messaging/android/src/main/java/io/flutter/plugins/firebase/messaging.

So how can we coordinate this? I can put some time into implementing this, but I don't want to do the work if someone else is already working on it. If you want to fix it then do it, because then I can continue working on other stuff for the notifications in the app.

provokateurin commented 2 years ago

I did some small refactoring and now at least the notifications work on subsequent runs. Background still not working.

provokateurin commented 2 years ago

FluffyChat also uses UP, so maybe I can check what they are doing, because I guess that works.

p1gp1g commented 2 years ago

What tests did you do ? Because the example (and fluffychat) definitely work in background and/or closed ?

provokateurin commented 2 years ago

What do you mean with tests? My implementation just doesn't work. I didn't try the examples yet, because as I said in the second comment I don't want to duplicate efforts if someone is already working on it. I will check the examples now.

provokateurin commented 2 years ago

LOL example works in background. Then I'm pretty sure I know what is wrong. The documentation just doesn't help a lot. I'll make a PR towards it to fix it.

p1gp1g commented 2 years ago

I though you did some tests with the example and you had trouble to receive message with the app in background or closed.

PR to improve our doc are very welcomed :)

provokateurin commented 2 years ago

Somehow I now don't receive the messages at all already on the Android side (looking at the logs). Message definitely arrives at the distributor.

provokateurin commented 2 years ago

I just always get GCM : broadcast intent callback: result=CANCELLED forIntent { act=com.google.android.c2dm.intent.RECEIVE pkg=de.provokateurin.harbour (has extras) } in the logs. That it is cancelled seems to be the problem.

p1gp1g commented 2 years ago

Are you trying to use foss_embedded_fcm_distributor ? If so, we should close this issue and continue there. And remind that the foss embedded fcm distrib is still in beta :)

provokateurin commented 2 years ago

Yes I'm trying the foss distributor. I can temporarily switch to the non-foss, but I need to use the foss one to publish on f-droid. I don't think the non-foss will help though, because I have the same problem when using nfty. I'll try anyway.

provokateurin commented 2 years ago

Tried the same with the non-foss distributor. I don't think any of this is related to the distributor that is used.

p1gp1g commented 2 years ago

You should try with ntfy then. I don't know if the GCM message is related to your problem but it is definitely dependent on Google Services.

Do not forget to wipe data of your app sometime, there might be using introduced in development, not wiped after code changes.

I can give a look at your code if it can help. You must receive notifications in background :)

provokateurin commented 2 years ago

ntfy also doesn't work in the background

provokateurin commented 2 years ago

at this point i kinda suspect another plugin is making problems

provokateurin commented 2 years ago

i'll just add all the plugins with native android code to the example app and see if it breaks.

provokateurin commented 2 years ago

adding the plugins didn't break it.

with nfty i get this log and nothing after that:

07-18 20:51:19.759  5631  5653 D NtfyUpDistributor: Sending MESSAGE to de.provokateurin.harbour (token=836ca154-e396-4785-8a36-02e99a793ed0): {"subject":"ppOeSO5z5ok6/44urMYGRue1ZbwgbnAGkGqIM4S0LpWRSpXCYmFEfvx6+qirYAuBvffos9h9paZXIxO0vbEG22f3ec/T1tY7ndZQh7D+Zqq81Kc/YI2diAzGffXX7Q9hpFlaF51rgHzzYP4lsQwFGkFdh+pUEBIgf9OydBiEokt+kWX3LzABIOECVL1jeR2vLiaf8chDbl/EBw0idGhvO2567pDMkU8URUAPCCvzR1gLLDuEKk06bqWFIaoVDfxnhYULntXsIkGGIXRDc01AF6Au6mGfga1RcAVsZ35rJiiicDjnkZrtN48vU6jGYbpYlXbRRkvOgh7Xxg8chAaENw==","signature":"Lj3+J6OtbGjRIRaQHisWMAZz3k7dTkWHD04bVZpeGZMf6X5vNGbK/tJvZKVjf2ZNdFZsCOEdnszQd+Km73dQxonC6ChMVfb8+Cgci3hEDnKR29LN4XG8R+ilL6fsSI74Pp2WWQAXVAC4eehWLATnuM925se4RvMP5cOoEQUUvDvTlM+heXONrYHRAkzmoCPp4CThD2wUkX2bdbhQnWtvLsxrkwObf8LdKu6WjKcza0LbabIvpg3vDhx7zSBwnUN82Q10OW0EN/iwiTb0aNtunIryAdBje5+mzj/D0NpHlykeC3I3WbmN41LQnuzJAzjOd5Ie8gOB0y5ngDyg0Wz5GA==","priority":"normal","type":"alert"} (752 bytes)}
provokateurin commented 2 years ago

so i think the broadcast intent is never received. idk why this happens now and used to work before.

p1gp1g commented 2 years ago

Can you set a breakpoint around there https://github.com/UnifiedPush/android-connector/blob/main/connector/src/main/java/org/unifiedpush/android/connector/MessagingReceiver.kt#L19 ? And/or check your shared pref

provokateurin commented 2 years ago

I rebuilt the example app exactly following the docs and it just works. There has to be something weird about my app.

provokateurin commented 2 years ago

I copied the android folder to my app and it doesn't work. So that part is fine. I really think it has to be one of the plugins

provokateurin commented 2 years ago

removing all plugins from my app also doesn't fix it.

i'm kind of out of ideas what it could be tbh.

p1gp1g commented 2 years ago

I don't know, I haven't seen your code. Maybe you could share it so I can check that. Did you do any change android side ?

provokateurin commented 2 years ago

The code is in the MR linked at the top. I have some local changes. I'll try on last time and then you can take a look.

provokateurin commented 2 years ago

I pushed the code. Hopefully you can find something. I just recreated the android folder from scratch and added my changes on top, but also no luck.

provokateurin commented 2 years ago

You don't need to look at all the changes. packages/harbour/ is the app. All the other changes are related to forwarding push messages from Nextcloud and so on.

provokateurin commented 2 years ago

Can you set a breakpoint around there https://github.com/UnifiedPush/android-connector/blob/main/connector/src/main/java/org/unifiedpush/android/connector/MessagingReceiver.kt#L19 ? And/or check your shared pref

How can I do that? I don't even know how to include the connector from local sources.

provokateurin commented 2 years ago

sorry for being so annoying, this is probably the worst bug i ever had because it doesn't make any sense :see_no_evil:

p1gp1g commented 2 years ago

No problem, I'll try your code to check that soon ! :)

provokateurin commented 2 years ago

i tried adding the android-connector from local sources, but just can't get it working :(

provokateurin commented 2 years ago

my time of developing native android apps and fiddeling with java and kotlin is just too long ago. i need help from someone with more expertise in that field

provokateurin commented 2 years ago

@p1gp1g did you have a chance to look at my code? i tried some other stuff, but nothing helped at all

provokateurin commented 2 years ago

So I gradually add the code again and I got it working now. Really no clue what was wrong. I'll push my code later when I got it all working.

provokateurin commented 2 years ago

It works pretty well now that it's reliable :D

provokateurin commented 2 years ago

wow it's broken again. what i noticed is that i can close the app in release mode and the notifications still arrive as long as i don't stop the flutter run command. in the app settings i can see the app is not running in the background as it should: Screenshot_20220723_150342 when i now open the app again and close it, i receive the notifications and also can press the force stop button. so is suspect this is something with the broadcast receiver getting killed for some reason.

provokateurin commented 2 years ago

so i think flutter run just kills the app completely instead of only closing the visible window. that would explain why the app isn't even running in the background. that would mean there is no issue with UP at all, just bad DX from flutter.

karmanyaahm commented 2 years ago

when i now open the app again and close it, i receive the notifications and also can press the force stop button. so is suspect this is something with the broadcast receiver getting killed for some reason. that would mean there is no issue with UP at all, just bad DX from flutter.

That's great to hear!

We should document this somewhere. I'll test this with other apps and create an FAQ section on the Flutter page on the website if this keeps on happening (weird that I never noticed it, but I guess when testing I leave the app open longer than I keep flutter run running usually)

provokateurin commented 2 years ago

i've fully integrated the notifications in my app now. no more issues so far other than the one i discovered. expect the feature to land in the app very soon!