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

🐛 [Cloud Messaging] onBackGroundMessage callback #3905

Closed
swapnens opened this issue Oct 19, 2020 · 16 comments · Fixed by #4012
Closed

🐛 [Cloud Messaging] onBackGroundMessage callback #3905

swapnens opened this issue Oct 19, 2020 · 16 comments · Fixed by #4012
Labels
plugin: messaging type: bug Something isn't working type: documentation Improvements or additions to documentation

Comments

@swapnens
Copy link

swapnens commented Oct 19, 2020

Bug report

Describe the bug
Hi
I cannot figure out what can be used in the onBackGroundMessage callback. The other callbacks work fine. I have tried and followed the documentation properly (I think - I double checked). Could you please add this call back to your example - as in do something significant over there.

Steps to reproduce

Steps to reproduce the behavior:

I have tried to print stuff to the console when the app is minimized but it did not work.
I have tried to play a ringtone using the [flutter_ringtone_player] package (which is static) but that does not work either.

Expected behavior

A clear and concise description of what you expected to happen.

Additional context

Add any other context about the problem here.

I want to understand what is supposed to happen and what are the possibilities. Please add that to the example app.

Flutter doctor

Run flutter doctor and paste the output below:

Click To Expand
avey> flutter doctor
Doctor summary (to see all details, run flutter doctor -v):
[√] Flutter (Channel stable, 1.22.2, on Microsoft Windows [Version 10.0.19041.572], locale en-US)
 
[√] Android toolchain - develop for Android devices (Android SDK version 30.0.1)
[!] Android Studio (version 4.1.0)
    X Flutter plugin not installed; this adds Flutter specific functionality.
    X Dart plugin not installed; this adds Dart specific functionality.
[√] VS Code (version 1.50.1)
[√] Connected device (2 available)

! Doctor found issues in 1 category.

Flutter dependencies

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

Click To Expand
avey> flutter pub deps -- --style=compact
Dart SDK 2.10.2
Flutter SDK 1.22.2
avey 2.0.0+7

dependencies:
- agora_rtc_engine 3.1.3 [flutter json_annotation]
- appcenter_analytics 0.2.1 [flutter]
- assets_audio_player 2.0.5+2 [flutter rxdart uuid dio path_provider assets_audio_player_web]        
- cloud_firestore 0.14.1+3 [flutter meta quiver firebase_core firebase_core_platform_interface cloud_firestore_platform_interface cloud_firestore_web]
- connectivity 0.4.9+5 [flutter meta connectivity_platform_interface connectivity_macos connectivity_for_web]
- cupertino_icons 1.0.0
- dartz 0.9.2
- dots_indicator 1.2.0 [flutter]
- email_validator 1.0.6
- equatable 1.2.5 [collection meta]
- fading_edge_scrollview 1.1.4 [flutter]
- firebase_analytics 6.0.2 [meta flutter firebase_core firebase_analytics_web firebase_analytics_platform_interface]
- firebase_auth 0.18.1+2 [meta firebase_core firebase_core_platform_interface firebase_auth_platform_interface firebase_auth_web flutter]
- firebase_core 0.5.0+1 [firebase_core_platform_interface flutter quiver meta firebase_core_web]     
- firebase_crashlytics 0.2.1+1 [flutter stack_trace firebase_core firebase_core_platform_interface firebase_crashlytics_platform_interface]
- firebase_messaging 7.0.3 [meta flutter firebase_core]
- firebase_performance 0.4.0+2 [flutter firebase_core]
- flutter 0.0.0 [characters collection meta typed_data vector_math sky_engine]
- flutter_appavailability 0.0.21 [flutter]
- flutter_appcenter_bundle 3.2.0+2 [flutter]
- flutter_bloc 6.0.6 [flutter bloc provider]
- flutter_cache_manager 2.0.0 [flutter path_provider uuid http path sqflite pedantic clock file rxdart]
- flutter_cupertino_date_picker 1.0.12 [flutter assets_audio_player]
- flutter_email_sender 3.0.1 [flutter]
- flutter_gifimage 1.0.1 [flutter]
- flutter_launcher_icons 0.8.1 [image args yaml path]
- flutter_launcher_name 0.0.1 [flutter yaml]
- flutter_local_notifications 2.0.1+1 [flutter platform flutter_local_notifications_platform_interface timezone]
- flutter_native_timezone 1.0.4 [flutter]
- flutter_rating_bar 3.0.1+1 [flutter]
- flutter_ringtone_player 2.0.0 [flutter]
- get_it 5.0.1 [async meta]
- google_fonts 1.1.1 [flutter http path_provider crypto pedantic]
- http 0.12.2 [http_parser path pedantic]
- intent 1.4.0 [flutter]
- intl 0.16.1 [path]
- keyboard_visibility 0.5.6 [flutter]
- maps_launcher 1.2.2+1 [flutter flutter_web_plugins url_launcher]
- modal_progress_hud 0.1.3 [flutter]
- package_info 0.4.3 [flutter]
- path_provider 1.6.21 [flutter path_provider_platform_interface path_provider_macos path_provider_linux path_provider_windows]
- percent_indicator 2.1.7+4 [flutter]
- permission_handler 5.0.1+1 [flutter meta permission_handler_platform_interface]
- shared_preferences 0.5.12+2 [meta flutter shared_preferences_platform_interface shared_preferences_linux shared_preferences_macos shared_preferences_web shared_preferences_windows]
- speech_bubble 0.0.6 [flutter]
- url_launcher 5.7.5 [flutter url_launcher_platform_interface url_launcher_web url_launcher_linux url_launcher_macos url_launcher_windows]
- video_player 0.11.1+2 [meta video_player_platform_interface video_player_web flutter]
- wakelock 0.2.0+1 [flutter meta wakelock_platform_interface wakelock_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]
- mockito 4.1.2 [analyzer build code_builder collection dart_style matcher meta source_gen test_api] 

transitive dependencies:
- _fe_analyzer_shared 7.0.0 [meta]
- analyzer 0.39.17 [_fe_analyzer_shared args charcode cli_util collection convert crypto glob html meta package_config path pub_semver source_span watcher yaml]
- archive 2.0.13 [crypto args path]
- args 1.6.0
- assets_audio_player_web 2.0.9+2 [flutter flutter_web_plugins]
- async 2.5.0-nullsafety.1 [collection]
- bloc 6.1.0 [meta]
- 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
- cloud_firestore_platform_interface 2.1.2 [flutter meta collection firebase_core plugin_platform_interface]
- cloud_firestore_web 0.2.0+4 [flutter flutter_web_plugins firebase http_parser meta firebase_core cloud_firestore_platform_interface js]
- code_builder 3.5.0 [built_collection built_value collection matcher meta]
- collection 1.15.0-nullsafety.3
- connectivity_for_web 0.3.1+2 [connectivity_platform_interface flutter_web_plugins flutter]
- connectivity_macos 0.1.0+5 [flutter]
- connectivity_platform_interface 1.0.6 [flutter meta plugin_platform_interface]
- convert 2.1.1 [charcode typed_data]
- crypto 2.1.5 [collection convert typed_data]
- csslib 0.16.2 [source_span]
- dart_style 1.3.6 [analyzer args path source_span]
- dio 3.0.10 [http_parser path]
- fake_async 1.2.0-nullsafety.1 [clock collection]
- ffi 0.1.3
- file 5.2.1 [intl meta path]
- firebase 7.3.2 [http http_parser js]
- firebase_analytics_platform_interface 1.0.3 [flutter meta]
- firebase_analytics_web 0.1.1 [flutter flutter_web_plugins firebase firebase_analytics_platform_interface meta]
- firebase_auth_platform_interface 2.1.1 [flutter meta firebase_core plugin_platform_interface]      
- firebase_auth_web 0.3.1+1 [flutter flutter_web_plugins firebase meta http_parser intl firebase_core firebase_auth_platform_interface js]
- firebase_core_platform_interface 2.0.0 [flutter meta plugin_platform_interface quiver]
- firebase_core_web 0.2.0 [firebase firebase_core_platform_interface flutter flutter_web_plugins meta js]
- firebase_crashlytics_platform_interface 1.1.1 [flutter meta collection firebase_core plugin_platform_interface]
- fixnum 0.10.11
- flutter_local_notifications_platform_interface 2.0.0 [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]
- html 0.14.0+4 [csslib source_span]
- http_parser 3.1.4 [charcode collection source_span string_scanner typed_data]
- image 2.1.18 [archive xml meta]
- import_js_library 1.0.1 [flutter html flutter_web_plugins js]
- js 0.6.2
- json_annotation 3.1.0
- logging 0.11.4
- matcher 0.12.10-nullsafety.1 [stack_trace]
- meta 1.3.0-nullsafety.3
- nested 0.0.4 [flutter]
- node_interop 1.1.1 [js]
- node_io 1.1.1 [node_interop path]
- package_config 1.9.3 [path charcode]
- path 1.8.0-nullsafety.1
- path_provider_linux 0.0.1+2 [path xdg_directories path_provider_platform_interface flutter]        
- path_provider_macos 0.0.4+4 [flutter]
- path_provider_platform_interface 1.0.3 [flutter meta platform plugin_platform_interface]
- path_provider_windows 0.0.4+1 [path_provider_platform_interface meta path flutter ffi win32]       
- pedantic 1.9.2 [meta]
- permission_handler_platform_interface 2.0.1 [flutter meta plugin_platform_interface]
- petitparser 3.1.0 [meta]
- platform 2.2.1
- plugin_platform_interface 1.0.3 [meta]
- process 3.0.13 [file intl meta path platform]
- provider 4.3.2+2 [flutter nested collection]
- pub_semver 1.4.4 [collection]
- quiver 2.1.3 [matcher meta]
- rxdart 0.24.1
- shared_preferences_linux 0.0.2+2 [file flutter meta path path_provider_linux shared_preferences_platform_interface]
- 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]
- shared_preferences_windows 0.0.1+1 [shared_preferences_platform_interface flutter ffi file meta path path_provider_platform_interface path_provider_windows]
- sky_engine 0.0.99
- source_gen 0.9.7+1 [analyzer async build dart_style glob meta path pedantic source_span]
- source_span 1.8.0-nullsafety.2 [charcode collection path term_glyph]
- sqflite 1.3.1+1 [flutter sqflite_common path]
- 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+2
- 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]
- timezone 0.5.7 [path]
- typed_data 1.3.0-nullsafety.3 [collection]
- url_launcher_linux 0.0.1+1 [flutter]
- url_launcher_macos 0.0.1+8 [flutter]
- url_launcher_platform_interface 1.0.8 [flutter meta plugin_platform_interface]
- url_launcher_web 0.1.4+1 [url_launcher_platform_interface flutter flutter_web_plugins meta]        
- url_launcher_windows 0.0.1+1 [flutter]
- uuid 2.2.2 [crypto convert]
- vector_math 2.1.0-nullsafety.3
- video_player_platform_interface 2.2.0 [flutter meta]
- video_player_web 0.1.4 [flutter flutter_web_plugins meta video_player_platform_interface]
- wakelock_platform_interface 0.1.0 [flutter meta]
- wakelock_web 0.1.0+2 [flutter flutter_web_plugins import_js_library js wakelock_platform_interface]- watcher 0.9.7+15 [async path pedantic]
- win32 1.7.3 [ffi]
- xdg_directories 0.1.2 [meta path process]
- xml 4.5.1 [collection convert meta petitparser]
- yaml 2.2.1 [charcode collection string_scanner source_span]
avey>

@swapnens swapnens added Needs Attention This issue needs maintainer attention. type: bug Something isn't working labels Oct 19, 2020
@markusaksli-nc markusaksli-nc added plugin: messaging type: documentation Improvements or additions to documentation and removed Needs Attention This issue needs maintainer attention. labels Oct 19, 2020
@devjeff
Copy link

devjeff commented Oct 21, 2020

Could you paste the part of your code where you register your callback and the code of the callback itself?

@dackers86
Copy link
Member

Possibly related? #3819 (comment)

@swapnens
Copy link
Author

swapnens commented Oct 21, 2020

Could you paste the part of your code where you register your callback and the code of the callback itself?

Hi, here you go.

From my main, I run a Material app with InitialPage as it's a child. The following code is from the init of that widget's state class.

_firebaseMessaging.configure(
      onMessage: (Map<String, dynamic> message) =>
          _handleNotificationForeground(message),
      onLaunch: (Map<String, dynamic> message) =>
          _handleNotificationBackground(message),
      onResume: (Map<String, dynamic> message) =>
          _handleNotificationBackground(message),
      onBackgroundMessage: backGroundHandler,
    );

    _firebaseMessaging.requestNotificationPermissions(
      const IosNotificationSettings(
        sound: true,
        badge: true,
        alert: true,
        provisional: true,
      ),
    );

Here is my background handler, defined outside the class.

Future<dynamic> backGroundHandler(Map<String, dynamic> message) async {
  print("onBackgroundMessage: $message");
  // await FlutterRingtonePlayer.play(
  //   android: AndroidSounds.ringtone,
  //   ios: IosSounds.glass,
  //   looping: true, // Android only - API >= 28
  //   volume: 1, // Android only - API >= 28
  //   asAlarm: true, // Android only - all APIs
  // );
  Future<void>.value();
}

@devjeff
Copy link

devjeff commented Oct 21, 2020

Ahh. I think, I have an idea. Apparently, you are testing this on iOS. If you read the docs of the firebase messaging plugin, you will notice that the background message callback is described in the Android section. The reason is that this method is only used on android, as the background process is different there. Therefore, it will never be called on iOS. On iOS the onMessage callback will be used, no matter if the app is in background or not.

@swapnens
Copy link
Author

Ahh. I think, I have an idea. Apparently, you are testing this on iOS. If you read the docs of the firebase messaging plugin, you will notice that the background message callback is described in the Android section. The reason is that this method is only used on android, as the background process is different there. Therefore, it will never be called on iOS. On iOS the onMessage callback will be used, no matter if the app is in background or not.

I think there is some confusion. Indeed I am developing an app for both iOS and Android. I have not tested it on iOS yet. I'm facing this problem on Android 11.

@devjeff
Copy link

devjeff commented Oct 21, 2020

All right then. I'm pretty sure that the print output should appear in the console. Can you try the following?

  1. Open the project's android sub folder in Android Studio
  2. Set a breakpoint in the class "FlutterFirebaseMessagingService" in the method "onMessageReceived"
  3. Attach Debugger to the device or emulator while the app is running

If the firebase plugin configuration is correctly set up, The app should wait at this breakpoint.

@dackers86
Copy link
Member

Messaging rework was announced yesterday https://twitter.com/mikediarmid/status/1319298281290203139. Could this be a potential answer?

@devjeff
Copy link

devjeff commented Oct 26, 2020

I rather think that the problem is related to this post:
https://stackoverflow.com/questions/59446933/pluginregistry-cannot-be-converted-to-flutterengine/59490722#59490722

At the moment, the firebase messaging plugin doesn't fully support the "new" Flutter embedding classes (introduced in version 1.12) and still relies on the "PluginRegistry.PluginRegistrantCallback". Currently I've also implemeted the solution mentioned in this stackoverflow post. Hopefully, this won't be necessary after the messaging rework.

@swapnens
Copy link
Author

All right then. I'm pretty sure that the print output should appear in the console. Can you try the following?

  1. Open the project's android sub folder in Android Studio
  2. Set a breakpoint in the class "FlutterFirebaseMessagingService" in the method "onMessageReceived"
  3. Attach Debugger to the device or emulator while the app is running

If the firebase plugin configuration is correctly set up, The app should wait at this breakpoint.

Where do I find this?

@swapnens
Copy link
Author

Messaging rework was announced yesterday https://twitter.com/mikediarmid/status/1319298281290203139. Could this be a potential answer?

Maybe this works. We would have to wait till it is actually launched right?

@swapnens
Copy link
Author

I rather think that the problem is related to this post:
https://stackoverflow.com/questions/59446933/pluginregistry-cannot-be-converted-to-flutterengine/59490722#59490722

At the moment, the firebase messaging plugin doesn't fully support the "new" Flutter embedding classes (introduced in version 1.12) and still relies on the "PluginRegistry.PluginRegistrantCallback". Currently I've also implemeted the solution mentioned in this stackoverflow post. Hopefully, this won't be necessary after the messaging rework.

So does it mean that we cannot use the onBackGroundMessage handler with the latest flutter version?

@abhishek199-dhn
Copy link

I'm also facing exactly the same issue and behaviour. Not sure how to resolve this :(

@devjeff
Copy link

devjeff commented Oct 27, 2020

You can use the background message handling, but you need to implement a custom "FirebaseCloudMessagingPluginRegistrant" as a workaround as described in the stackoverflow answer that I've linked above. If you have problems while applying this solution, you can ask them here.

@swapnens
Copy link
Author

swapnens commented Oct 28, 2020

You can use the background message handling, but you need to implement a custom "FirebaseCloudMessagingPluginRegistrant" as a workaround as described in the stackoverflow answer that I've linked above. If you have problems while applying this solution, you can ask them here.

I am using Kotlin for this part. I found the code on some StackOverflow page. Could that be the issue? Also, how would I modify the fixed Java code to Kotlin?

import io.flutter.app.FlutterApplication
import io.flutter.plugin.common.PluginRegistry
import io.flutter.plugin.common.PluginRegistry.PluginRegistrantCallback
import io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin
import io.flutter.plugins.firebasemessaging.FlutterFirebaseMessagingService

class Application : FlutterApplication(), PluginRegistrantCallback {
    override fun onCreate() {
        super.onCreate()
        FlutterFirebaseMessagingService.setPluginRegistrant(this)
    }

    override fun registerWith(registry: PluginRegistry?) {
        FirebaseMessagingPlugin.registerWith(registry?.registrarFor("io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin"))
    }
}

This is my MainActivity.kt

import androidx.annotation.NonNull;
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugins.GeneratedPluginRegistrant

class MainActivity: FlutterActivity() {}

And this is my relevant part of Android manifest
<application android:name=".Application"
android:label=

@devjeff
Copy link

devjeff commented Oct 28, 2020

This should work. The pasted code is already in Kotlin, so what do you mean by the "fixed java code"? However, there is a built-in menu entry in intellij to transform java code to Kotlin:

Main Menu -> Code -> Convert Java File to Kotlin File

@swapnens
Copy link
Author

This should work. The pasted code is already in Kotlin, so what do you mean by the "fixed java code"? However, there is a built-in menu entry in intellij to transform java code to Kotlin:

Main Menu -> Code -> Convert Java File to Kotlin File

By "fixed java code", I was referring to the code from the StackOverflow page. I will try to use the Android Studio tool and see if that helps.
Thanks

  • to

@firebase firebase locked and limited conversation to collaborators Dec 4, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
plugin: messaging type: bug Something isn't working type: documentation Improvements or additions to documentation
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants