Home > Net >  how to run dart code when receive notification on Terminated state?
how to run dart code when receive notification on Terminated state?

Time:09-17

I have to post some data to Firebase when user receive notification when app is on killed/Terminated state, I tried flutter_background_service but that not solved my problem. I don't want to run my app on background, I just want to run my app on background when I receive notification and after sends data to Firebase close the background thread.

CodePudding user response:

I faced this problem a long time ago. I solved it by using Firebase native instead of Firebase flutter, and pass all my data to/from native using native bridge.

You might be able to use both (flutter for most of the stuff, native for the background stuff), but be prepared for dependency hell and some bizarre behavior on init etc.

CodePudding user response:

Basically when your app is in the background or not running then if firebase notification triggers then the FirebaseMessaging.onBackgroundMessage() method gets called.

So if you want to do something then you can put your code like

/// this method will be called when you receive a message from firebase
/// while your app is not running

/// put this line on your main method
FirebaseMessaging.onBackgroundMessage(_messageHandler);


/// this method should be a top level function
Future<void> _messageHandler(RemoteMessage message) async {
    // do what you want
}

  • Related