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

🐛 Messaging - dev.14 build - Data message delayed when in Android doze #4718

Closed
pete-cth opened this issue Jan 19, 2021 · 19 comments · Fixed by #6730
Closed

🐛 Messaging - dev.14 build - Data message delayed when in Android doze #4718

pete-cth opened this issue Jan 19, 2021 · 19 comments · Fixed by #6730

Comments

@pete-cth
Copy link

Bug report

I'm using the latest dev build 8.0.0-dev.14. Almost everything is working perfectly!

When I send a high priority FCM data message to the device, my background message handler is not given the message while the device is in doze (idle) state. As soon as the device comes out of doze the background handler is called.

  • The following log message is observed in LogCat at the correct time the message should be handled (during doze): D/FLTFireMsgReceiver: broadcast received for message

  • The message I send is set as high priority. I have tried sending from both the legacy HTTP api, and from the Node firebase-admin client

  • There is no notification section in the message being sent, just a data section.

  • I have tried setting a ttl of 0 for the message. The message is still delivered, but again only after the device comes out of doze.

  • I am testing on a Pixel Xl Android 10

  • I have primarily been testing the doze (idle) state by forcing it using adb (see below) however I have also tested by leaving the device to naturally go into a doze state, with the same effect.

Steps to reproduce

Steps to reproduce the behavior:

  1. Setup a simple project with a background message handler, with logging for when a background message is received
  2. Install the app on a device and turn off the screen (it doesn't matter whether you close the app or not)
  3. Put the device in doze: adb shell dumpsys deviceidle force-idle
  4. Send a high priority data only message to the device via an FCM client
  5. Observe that the message is not handled and does not produce the logging from the custom handler (only the FLTFireMsgReceiver log message instead)
  6. Take the device out of doze with: adb shell dumpsys deviceidle unforce
  7. Observe that the message that was previously sent is now handled and the expected logging is produced

Expected behavior

I expect that the high priority message should still be passed to the background message handler in a timely manner even if the device is in doze

It looks like this almost works as the FLTFireMsgReceiver log message is produced at the correct time, and there is also evidence that the Flutter app's UI does wake up a bit and is given network access for a brief period, but the background message handler is not called until later


Flutter doctor

Run flutter doctor and paste the output below:

Click To Expand
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 1.22.3, on Linux, locale en_GB.UTF-8)
 
[✓] Android toolchain - develop for Android devices (Android SDK version 30.0.2)
[!] Android Studio
    ✗ Flutter plugin not installed; this adds Flutter specific functionality.
    ✗ Dart plugin not installed; this adds Dart specific functionality.
[✓] VS Code (version 1.52.1)
[✓] Connected device (1 available)

Flutter dependencies

Run flutter pub deps -- --style=compact and paste the output below:

Click To Expand
Dart SDK 2.10.3
Flutter SDK 1.22.3
the_app_flutter 1.0.0+1

dependencies:
- android_alarm_manager 0.4.5+17 [flutter]
- cupertino_icons 0.1.3
- firebase_core 0.7.0 [firebase_core_platform_interface flutter quiver meta firebase_core_web]
- firebase_messaging 8.0.0-dev.14 [meta flutter firebase_core firebase_core_platform_interface firebase_messaging_platform_interface firebase_messaging_web]
- flutter 0.0.0 [characters collection meta typed_data vector_math sky_engine]
- flutter_datetime_picker 1.4.0 [flutter]
- flutter_local_notifications 3.0.3 [flutter platform flutter_local_notifications_platform_interface timezone]
- flutter_ringtone_player 2.0.0 [flutter]
- http 0.12.2 [http_parser path pedantic]
- intl 0.16.1 [path]
- logger 0.9.4
- mockito 4.1.3 [analyzer build code_builder collection dart_style matcher meta source_gen test_api]
- path 1.8.0-nullsafety.1
- shared_preferences 0.5.7+3 [meta flutter shared_preferences_platform_interface shared_preferences_macos shared_preferences_web]
- socket_io_client 0.9.11 [logging socket_io_common]
- sqflite 1.3.2+1 [flutter sqflite_common path]
- test 1.16.0-nullsafety.5 [analyzer async boolean_selector coverage http http_multi_server io js node_preamble package_config path pedantic pool shelf shelf_packages_handler shelf_static shelf_web_socket source_span stack_trace stream_channel typed_data web_socket_channel webkit_inspection_protocol yaml test_api test_core]
- timezone 0.5.9 [path]
- uuid 2.2.2 [crypto convert]
- vibration 1.7.3 [flutter vibration_web]

dev dependencies:
- flutter_test 0.0.0 [flutter test_api path fake_async clock stack_trace vector_math async boolean_selector characters charcode collection matcher meta source_span stream_channel string_scanner term_glyph typed_data]

transitive dependencies:
- _fe_analyzer_shared 12.0.0 [meta]
- analyzer 0.40.6 [_fe_analyzer_shared args cli_util collection convert crypto glob meta package_config path pub_semver source_span watcher yaml]
- args 1.6.0
- async 2.5.0-nullsafety.1 [collection]
- boolean_selector 2.1.0-nullsafety.1 [source_span string_scanner]
- build 1.5.0 [analyzer async convert crypto logging meta path glob]
- built_collection 4.3.2 [collection quiver]
- built_value 7.1.0 [built_collection collection fixnum quiver]
- characters 1.1.0-nullsafety.3
- charcode 1.2.0-nullsafety.1
- cli_util 0.2.0 [path]
- clock 1.1.0-nullsafety.1
- code_builder 3.5.0 [built_collection built_value collection matcher meta]
- collection 1.15.0-nullsafety.3
- convert 2.1.1 [charcode typed_data]
- coverage 0.13.11 [args logging package_config path source_maps stack_trace vm_service]
- crypto 2.1.4 [collection convert typed_data]
- dart_style 1.3.9 [analyzer args path source_span]
- fake_async 1.2.0-nullsafety.1 [clock collection]
- firebase_core_platform_interface 3.0.1 [flutter meta plugin_platform_interface quiver]
- firebase_core_web 0.2.1+3 [firebase_core_platform_interface flutter flutter_web_plugins meta js]
- firebase_messaging_platform_interface 1.0.0-dev.10 [flutter meta firebase_core plugin_platform_interface]
- firebase_messaging_web 0.1.0-dev.5 [flutter flutter_web_plugins meta firebase_core firebase_core_web firebase_messaging_platform_interface js service_worker]
- fixnum 0.10.11
- flutter_local_notifications_platform_interface 2.0.0+1 [flutter plugin_platform_interface]
- flutter_web_plugins 0.0.0 [flutter characters collection meta typed_data vector_math]
- glob 1.2.0 [async collection node_io path pedantic string_scanner]
- http_multi_server 2.2.0 [async]
- http_parser 3.1.4 [charcode collection source_span string_scanner typed_data]
- io 0.3.4 [charcode meta path string_scanner]
- js 0.6.3-nullsafety.2
- logging 0.11.4
- matcher 0.12.10-nullsafety.1 [stack_trace]
- meta 1.3.0-nullsafety.3
- mime 0.9.6+3
- node_interop 1.1.1 [js]
- node_io 1.1.1 [node_interop path]
- node_preamble 1.4.12
- package_config 1.9.3 [path charcode]
- pedantic 1.10.0-nullsafety.2
- platform 2.2.1
- plugin_platform_interface 1.0.2 [meta]
- pool 1.5.0-nullsafety.2 [async stack_trace]
- pub_semver 1.4.4 [collection]
- quiver 2.1.4+1 [matcher meta]
- service_worker 0.2.4 [js]
- shared_preferences_macos 0.0.1+10 [shared_preferences_platform_interface flutter]
- shared_preferences_platform_interface 1.0.4 [meta flutter]
- shared_preferences_web 0.1.2+7 [shared_preferences_platform_interface flutter flutter_web_plugins meta]
- shelf 0.7.5 [async collection http_parser path stack_trace stream_channel]
- shelf_packages_handler 2.0.0 [path shelf shelf_static]
- shelf_static 0.2.8 [convert http_parser mime path shelf]
- shelf_web_socket 0.2.3 [shelf web_socket_channel stream_channel]
- sky_engine 0.0.99
- socket_io_common 0.9.2 [logging]
- source_gen 0.9.8 [analyzer async build dart_style glob meta path pedantic source_span]
- source_map_stack_trace 2.1.0-nullsafety.3 [path stack_trace source_maps]
- source_maps 0.10.10-nullsafety.2 [source_span]
- source_span 1.8.0-nullsafety.2 [charcode collection path term_glyph]
- sqflite_common 1.0.2+1 [synchronized path meta]
- stack_trace 1.10.0-nullsafety.1 [path]
- stream_channel 2.1.0-nullsafety.1 [async]
- string_scanner 1.1.0-nullsafety.1 [charcode source_span]
- synchronized 2.2.0+1
- term_glyph 1.2.0-nullsafety.1
- test_api 0.2.19-nullsafety.2 [async boolean_selector collection meta path source_span stack_trace stream_channel string_scanner term_glyph matcher]
- test_core 0.3.12-nullsafety.5 [analyzer async args boolean_selector collection coverage glob io meta package_config path pedantic pool source_map_stack_trace source_maps source_span stack_trace stream_channel vm_service yaml matcher test_api]
- typed_data 1.3.0-nullsafety.3 [collection]
- vector_math 2.1.0-nullsafety.3
- vibration_web 1.6.2 [flutter flutter_web_plugins]
- vm_service 4.1.0 [meta]
- watcher 0.9.7+15 [async path pedantic]
- web_socket_channel 1.1.0 [async crypto stream_channel]
- webkit_inspection_protocol 0.7.3 [logging]
- yaml 2.2.1 [charcode collection string_scanner source_span]

@pete-cth pete-cth added Needs Attention This issue needs maintainer attention. type: bug Something isn't working labels Jan 19, 2021
@pete-cth
Copy link
Author

I have also now also tested this with

Dart SDK 2.10.4
Flutter SDK 1.22.5

and with the same outcome

@markusaksli-nc
Copy link
Contributor

Reproducible on the latest master 1.26.0-2.0.pre.402 with firebase_messaging: ^8.0.0-dev.14.

flutter doctor -v
[√] Flutter (Channel master, 1.26.0-2.0.pre.402, on Microsoft Windows [Version 10.0.19041.746], locale et-EE)
    • Flutter version 1.26.0-2.0.pre.402 at C:\Development\flutter_master
    • Framework revision 2a188eeca3 (34 hours ago), 2021-01-17 19:27:00 -0800
    • Engine revision 609036f2bf
    • Dart version 2.12.0 (build 2.12.0-236.0.dev)

[√] Android toolchain - develop for Android devices (Android SDK version 30.0.2)
    • Android SDK at C:\Users\marku\AppData\Local\Android\sdk
    • Platform android-30, build-tools 30.0.2
    • Java binary at: C:\Users\marku\AppData\Local\JetBrains\Toolbox\apps\AndroidStudio\ch-0\201.6953283\jre\bin\java
    • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b01)
    • All Android licenses accepted.

[√] Chrome - develop for the web
    • Chrome at C:\Program Files\Google\Chrome\Application\chrome.exe

[√] Visual Studio - develop for Windows (Visual Studio Community 2019 16.7.7)
    • Visual Studio at C:\Program Files (x86)\Microsoft Visual Studio\2019\Community
    • Visual Studio Community 2019 version 16.7.30621.155
    • Windows 10 SDK version 10.0.18362.0

[√] Android Studio (version 4.1.0)
    • Android Studio at C:\Users\marku\AppData\Local\JetBrains\Toolbox\apps\AndroidStudio\ch-0\201.6953283
    • Flutter plugin can be installed from:
       https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
       https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b01)

[√] Connected device (4 available)
    • SM G950F (mobile) • ce12171c51cc001c03 • android-arm64  • Android 9 (API 28)
    • Windows (desktop) • windows            • windows-x64    • Microsoft Windows [Version 10.0.19041.746]
    • Chrome (web)      • chrome             • web-javascript • Google Chrome 87.0.4280.141
    • Edge (web)        • edge               • web-javascript • Microsoft Edge 87.0.664.75

• No issues found!

@markusaksli-nc markusaksli-nc added plugin: messaging and removed Needs Attention This issue needs maintainer attention. labels Jan 19, 2021
@russellwheatley
Copy link
Member

Hey @pete-cth, we're now on firebase_messaging v10, can you confirm this is still an issue?

@russellwheatley russellwheatley added the blocked: customer-response Waiting for customer response, e.g. more information was requested. label May 25, 2021
@cturn-dev
Copy link

Hello,
I have the exact same behavior.
Flutter (Channel stable, 2.2.0, on Linux, locale fr_FR.utf8)
firebase_messaging: ^10.0.0

@google-oss-bot
Copy link

Hey @pete-cth. We need more information to resolve this issue but there hasn't been an update in 7 weekdays. I'm marking the issue as stale and if there are no new updates in the next 7 days I will close it automatically.

If you have more information that will help us get to the bottom of this, just add a comment!

@google-oss-bot google-oss-bot added the Stale Issue with no recent activity label Jun 4, 2021
@pete-cth
Copy link
Author

pete-cth commented Jun 8, 2021

I haven't had a chance to retest this yet as I have been on another project just recently. I will retry as soon as I can.

However I see that @cturn-dev has just noted that it is still reproducible against v10. I suggest it is kept open for now.

@google-oss-bot google-oss-bot added Needs Attention This issue needs maintainer attention. and removed blocked: customer-response Waiting for customer response, e.g. more information was requested. Stale Issue with no recent activity labels Jun 8, 2021
@cturn-dev
Copy link

Same problem with flutter 2.2.1 and firebase messaging 10.0.1
I have tested sending high priority data only message with HTTPv1 and legacy HTTP protocols.

@cturn-dev
Copy link

In the file firebase/messaging/FlutterFirebaseMessagingBackgroundService.java, the class FlutterFirebaseMessagingBackgroundService extends JobIntentService.

The JobIntentService documentation says:
" When running as a Job, it will be subject to standard JobScheduler policies for a Job with a JobInfo.Builder.setOverrideDeadline(long) of 0: the job will not run while the device is dozing, it may get delayed more than a service if the device is under strong memory pressure with lots of demand to run jobs."

Could this be the problem?

@ManuelFMartinho
Copy link

Any News on this?

Same problem with flutter 1.22.6 and firebase messaging 8.0.0-dev.14
I have tested sending high priority data only message with HTTPv1

@makstheimba
Copy link

makstheimba commented Jul 15, 2021

Issue still persists.

Flutter SDK 2.2.2
firebase_messaging: 10.0.3

I can see that message is received by firebase_messaging via this log:
D/FLTFireMsgReceiver(12942): broadcast received for message

But FirebaseMessaging.onBackgroundMessage doesn't get called until phone exits doze mode.

We are building a calling app and this issue is actually blocking us from release - our app won't wake on incoming call when phone is in doze mode :(

@makstheimba
Copy link

@russellwheatley @markusaksli-nc
Hi! Guys, can we provide more useful information for you so this issue is fixed?

@boredcity
Copy link

got the same problem

  sdk: '>=2.13.0 <3.0.0'
  flutter: ">=2.2.0"

  firebase_messaging: ^10.0.3

@russellwheatley
Copy link
Member

Hey @pete-cth, I did quite a bit of digging on this. I was able to confirm the behaviour you & others outlined. The solution for me was to change the battery optimisation settings for my app. My Samsung device was stopping the background task from executing. Only when I updated the "App power management" settings to ensure the app wasn't put to sleep was I able to receive the background message.

I highly recommend using don't kill my app to help remove any battery saving blockers your device might be using, and test if your device is able to receive the message.

This is a problem with allowing vendors to implement their own battery saving optimisations on top of the android battery saving optimisations which makes it a big problem for us developers.

@russellwheatley russellwheatley removed the type: bug Something isn't working label Jul 28, 2021
@makstheimba
Copy link

makstheimba commented Jul 28, 2021

@russellwheatley You are right about battery optimization. However I've checked previous version of our app that was native and it awakes from doze mode when we receive a high priority push. And this same push doesn't awake flutter app.

This is not a problem with the device, this is a bug inside of this implementation of firebase_messaging. I think message handling should not be passed to JobIntentService cause in it's description it clearly says that JobIntentService will not handle messages while device is in Doze mode (except for the maintenance window)

@makstheimba
Copy link

makstheimba commented Jul 28, 2021

@russellwheatley I've tested your solution and it works indeed - all pushes are shown immediately if I turn off battery saving optimizations. However there is no way we can force all our users to do that and without push handling in Doze mode our calling app is useless.

It also contradicts the information here, which states that only high priority data push messages will be handled right away in Doze. With your suggestion all messages are now handled right away and I think it might affect battery drain.

I think we should look more into this issue and try to make high priority data messages work in Doze mode by default. I'm trying to fix it myself but unfortunately I have very limited knowledge of Java and Android cause I used to be a web dev and have only recently switched to Flutter. We really need help from maintainers to fix this 🙏

@cturn-dev
Copy link

I solved the problem by using a modified version of JobIntentService, as telegram and onesignal do.
Here is the version of onesignal .

@makstheimba
Copy link

makstheimba commented Jul 29, 2021

@cturn-dev Thank you so much! 🤗 It is actually working now. The only changes I had to make are:

  • get rid of import androidx.core.app.JobIntentService;
  • add boolean true as last argument to enqueueWork call inside FlutterFirebaseMessagingBackgroundService 🎉

@russellwheatley may be we could use similar JobIntentService inside this package? There is even an explanation of why it is working in Doze mode.

@Salakar
Copy link
Member

Salakar commented Jul 29, 2021

@russellwheatley may be we could use similar JobIntentService inside this package? There is even an explanation of why it is working in Doze mode.

It's being looked into at the moment

@cturn-dev
Copy link

@makstheimba You're welcome!

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

Successfully merging a pull request may close this issue.

9 participants