'How to start specific screen when notification appear in flutter

I am using firebase_messaging for notification, everyting is work fine. I have a video calling screen I want to start this screen on notification when app in background. It's working fine when application in foreground.

here is my firebase integration code:

final FirebaseMessaging _firebaseMessaging = FirebaseMessaging();
_firebaseMessaging.configure(
      onMessage: (Map<String, dynamic> message) async {
        Map data = message["data"];
        if (data.isNotEmpty){
          Fcm.startRinging(data);
        } else {
          showNotification(title: "Message", body: "Telemedicine");
        }
      },
      onLaunch: (Map<String, dynamic> message) async {
        Map data = message["data"];
        print("OnLaunch::: $message");
        showNotification(title: "Message", body: "Telemedicine Call");
      },
      onBackgroundMessage: Fcm.myBackgroundMessageHandler,
      onResume: (Map<String, dynamic> message) async {
        Map data = message["data"];
        print("onResume::: $message");

      },
    );

Here I handle payload notification when application in background:

class Fcm{
  static Future<dynamic> myBackgroundMessageHandler(Map<String, dynamic> message) async {
    print("hello"+message.toString());

    if (message.containsKey('data')) {
      // Handle data message
      final dynamic data = message['data'];
      await startRinging(data);
      print(data);
    }

    if (message.containsKey('notification')) {
      // Handle notification message
      final dynamic notification = message['notification'];
      print(notification);
    }

    return Future<void>.value();
    // Or do other work.
  }


  static void startRinging(Map<dynamic, dynamic> data) async {
    print("start");
    String docName = data["DoctorName"];
    String docImage = data["DoctorImage"];
    String room = data["RoomName"];
    String token = data["PatientToken"];
    Route route = MaterialPageRoute(builder: (_)=> CallResponse(
        docName: docName,
        docImage: docImage,
        token: token,
        roomName: room
    ));
    <Here I am trying to start next route but i don't have BuildContext>
//    await Navigator.push(context, route);
  }
}


Solution 1:[1]

The notification received from firebase_messaging while the app is in background will be received in the notification tray and the message can be handled on onBackgroundMessage. You can run functions in onBackgroundMessage(), but this can't launch the app. The user needs to interact with the notification received to launch the app. The VOIP protocol that you're handling should be the one managing the app launch when an incoming call is received.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Omatt