0

I need to open the app automatically on receiving a notification message. Is it possible in flutter?

Below is the code for handling the background messages and it works.

Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
  await Firebase.initializeApp();
  print('Handling a background message ${message.messageId}');
}

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();
  FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);
  ......
  runApp(
    MaterialApp(
      debugShowCheckedModeBanner: false,
      home: MyApp()
    )
  );

What I need is I have a separate page that needs to popup when that particular function is executed (When the app is in background). Can anyone help me out with this! Thanks in advance

Nagulan S
  • 617
  • 5
  • 11
  • Did you find any solution? I am facing the same issue. – wahab sohail Sep 29 '21 at 10:49
  • From what I have researched, android does not allow apps to automatically open without any user interaction. If you have the latest android u would notice, even phone calls only pop a notification, and only when clicked the app opens. – Nagulan S Sep 30 '21 at 11:46
  • When user click the notification that's possible to take user to a specific screen when application is terminated ? I am using firebase_messaging: ^10.0.8 . – wahab sohail Sep 30 '21 at 13:34
  • I that case this answer would help you! https://stackoverflow.com/a/48405551/12131806 – Nagulan S Sep 30 '21 at 15:44
  • Thank you . But in this answer link they are using old version of firebase_messaging. – wahab sohail Oct 01 '21 at 06:20
  • Yeah but you can use that GlobalKey method in that background handler function. That will work – Nagulan S Oct 01 '21 at 07:54
  • yes. I use that `final GlobalKey navigatorKey = GlobalKey(); ` but did not work when user click on the notification just Application open not take the user to specific screen. ` navigatorKey.currentState.push(PageRouteBuilder( opaque: false, pageBuilder: (BuildContext context, _, __) => ProfilePage( userId: int.parse(message.data["userId"].toString()), ), )); ` – wahab sohail Oct 01 '21 at 08:13
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/237688/discussion-between-wahab-sohail-and-nagulan-s). – wahab sohail Oct 01 '21 at 08:15

1 Answers1

0
    await _firebaseMessaging.subscribeToTopic('topic name');
    _firebaseMessaging.configure
    (
        
        // The onMessage function triggers when the notification is 
        received while we are running the app.
        onMessage: (message) async
        {
            setState(()
            {
                messageTitle = message["notification"]["title"];
                messageDescription = message["notification"]["description"];
                notificationAlert = "New Notification Alert";
            });

        },
        // The onResume function triggers when we receive the notification alert in the device notification bar and opens the app through the push notification itself. In this case, the app can be running in the background or not running at all.
        onResume: (message) async
        {
            print("ON RESUME");

            setState(() 
            {
                messageTitle = message["data"]["title"];
                messageDescription = message["notification"]["description"];
                notificationAlert = "Application opened from Notification";
            });
        },
        onLaunch: (Map<String, dynamic> message) async // Called when app is terminated
        {
            print("onLaunch: $message");

            var data = message["data"];

            print(data);

            // Navigator.pushNamed(context, "details");
        }
    );

In this code the onResume function will help you run the app from background so you can code inside onResume and navigate to your specified screen.

Amitha Mohanan
  • 301
  • 4
  • 9
  • 2
    If I am not wrong this is for the older version of firebase messaging. In the current version, the background message function does that part, but the difference is background message is done outside void main so the app is not opening. – Nagulan S Jun 06 '21 at 18:19