Flutterfire: [firebase_messaging] Another exception was thrown: NoSuchMethodError: The method 'toRawHandle' was called on null

Created on 8 Oct 2019  路  6Comments  路  Source: FirebaseExtended/flutterfire

Firebase messaging not working in background for android.
it shows this error, Another exception was thrown: NoSuchMethodError: The method 'toRawHandle' was called on null.

crowd messaging bug

Most helpful comment

write it in your main.dart, below is my code snippet

 Future<dynamic> myBackgroundMessageHandler(Map<String, dynamic> message) async{
      print(message);
}

main() async {
 final FirebaseMessaging _firebaseMessaging = FirebaseMessaging();
  _firebaseMessaging.configure(
    onMessage: (Map<String, dynamic> message) async {
      print("onMessage: $message");
},
    onBackgroundMessage: myBackgroundMessageHandler,
    onLaunch: (Map<String, dynamic> message) async {
      print("onLaunch: $message");
 },
    onResume: (Map<String, dynamic> message) async {
      print("onResume: $message");
},);

runApp(App());

 });

All 6 comments

write it in your main.dart, below is my code snippet

 Future<dynamic> myBackgroundMessageHandler(Map<String, dynamic> message) async{
      print(message);
}

main() async {
 final FirebaseMessaging _firebaseMessaging = FirebaseMessaging();
  _firebaseMessaging.configure(
    onMessage: (Map<String, dynamic> message) async {
      print("onMessage: $message");
},
    onBackgroundMessage: myBackgroundMessageHandler,
    onLaunch: (Map<String, dynamic> message) async {
      print("onLaunch: $message");
 },
    onResume: (Map<String, dynamic> message) async {
      print("onResume: $message");
},);

runApp(App());

 });

In order to attach onBackgroundMessage, the handler function needs to be a top level method as kept by @mudi256. This is also described in optionally-handle-background-messages

i am getting data messages even app is background..

also i can show notification by using local_notification plugin. I just want to ask how can i navigate a page by using data message..

For example if data message contains 'You have a new message', i want to navigate messagesPage when user clicks the notification..

onBackgroundMessage should be static or top-level function but if we make it static we can not get context :(

Please help

@emrealtunbilek : As per receiving-message, notifications messages are delivered to the app or system tray depending upon whether the app is in foreground or background respectively.

Data messages, on the other hand, are to process the data on the client side when the app is in background. For such messages there is no provision to launch the app.

So, if you want to process the data along with displaying some message withing the app, leverage notification message. Do take care of following:

  • Set click_action: FLUTTER_NOTIFICATION_CLICK so that notification can be delivered to system tray when app is in background.
  • Configure onResume and onLaunch callbacks to handle message when user taps the notification. Also add onMessage that will be called when app is in foreground.
  • Pass additional data with notification message you need to process on client side.

@emrealtunbilek : As per receiving-message, notifications messages are delivered to the app or system tray depending upon whether the app is in foreground or background respectively.

Data messages, on the other hand, are to process the data on the client side when the app is in background. For such messages there is no provision to launch the app.

So, if you want to process the data along with displaying some message withing the app, leverage notification message. Do take care of following:

  • Set click_action: FLUTTER_NOTIFICATION_CLICK so that notification can be delivered to system tray when app is in background.
  • Configure onResume and onLaunch callbacks to handle message when user taps the notification. Also add onMessage that will be called when app is in foreground.
  • Pass additional data with notification message you need to process on client side.

Thanks for reply.. But can you tell me how can i use local_notification package for custom notication, not auto notification

I want to show user profile picture in notification , if i use notification messages, system created auto notification in system tray and i can not edit them as far as i know..

Because of that i am using data messages...

Is there a way to do that with notification messages not data messages ?

Hey all 馃憢

As part of our roadmap (#2582) we've just shipped a complete rework of the firebase_messaging plugin that aims to solve this and many other issues.

If you can, please try out the dev release (see the migration guide for upgrading and for changes) and if you have any feedback then join in the discussion here.

Given the scope of the rework I'm going to go ahead and close this issue in favor of trying out the latest plugin.

Thanks everyone 馃

Was this page helpful?
0 / 5 - 0 ratings