I am using flutter_local_notifications: ^15.1.0+1 to show notification using FCM. i was able to handle notification click on foreground and background clicks , But i couldn't find a function which is invoked on notification tap while the app is in terminated state. Any help will be welcomed .

onDidReceiveNotificationResponse is invoked while app is in background or foreground, Using custom notification with local_notification package, below is my FCM content

 { "message":{
      "token":"token_1",
      "data":{
        "title":"FCM Message",
        "body":"This is an FCM notification message!",
      }
   }
1

There are 1 best solutions below

1
pmatatias On

flutter_local_notifications will handle incoming message in the foreground notification. because the Firebase Android SDK will block displaying any FCM notification no matter what Notification Channel has been set.

read here for detail: https://firebase.flutter.dev/docs/messaging/notifications#application-in-foreground


while on teminated state, no need to use local notification. if you still use flutter_local_notification to handle background state, you will receive 2 notifications at the same time.

for handling received message read this documentation: https://firebase.google.com/docs/cloud-messaging/flutter/receive


  • invoke method everytime revice notification

you can call your method here, it will automatically invoked if the notification incomming in the terminated state

@pragma('vm:entry-point')
Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
  // If you're going to use other Firebase services in the background, such as Firestore,
  // make sure you call `initializeApp` before using other Firebase services.
  await Firebase.initializeApp();
 /// call you method here <<<<<<<<<<<<<<<<<<<<<<

  print("Handling a background message: ${message.messageId}");
}

void main() {
  FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);
  runApp(MyApp());
}

but if you need interaction, for example, you want to navigate specific screen when user click the notification in the `terminated state, then read below:

  • interaction

by default, when we click the notification, it will trigger to open the app. based on my project, I use invoke my method on the splash screen before navigate to home screen.

splash_screen.dart

class SplashScreen extends StatefulWidget {
  const SplashScreen({Key? key}) : super(key: key);

  @override
  _SplashScreenState createState() => _SplashScreenState();
}

class _SplashScreenState extends State<SplashScreen> {

 @override
  void initState() {
    super.initState();
    setupInteractedMessage();
  }


 Future<void> setupInteractedMessage() async {
    // Get any messages which caused the application to open from
    // a terminated state.
    RemoteMessage? initialMessage =
        await FirebaseMessaging.instance.getInitialMessage();

    // If the message also contains a data property with a "type" of "chat",
    // navigate to a chat screen
    if (initialMessage != null) {
      _handleMessage(initialMessage);
    }

    // Also handle any interaction when the app is in the background via a
    // Stream listener
    FirebaseMessaging.onMessageOpenedApp.listen(_handleMessage);
  }

 void _handleMessage(RemoteMessage message) {
    /// Navigate to detail
    String notifType = message.data['type'] ?? '';
      if (notifType == "chatiii"){
          // navigate to specific screen
          Navigator.of(context).pushAndRemoveUntil(
            MaterialPageRoute(
                builder: (context) => const ChatScreen()),
            (Route<dynamic> route) => true);
   }
 }
...

}