首页 > 解决方案 > 如何从后台打开我的应用程序并导航到接收通知消息的页面?

问题描述

我需要在收到通知消息时自动打开应用程序。颤振有可能吗?

下面是处理后台消息的代码,它可以工作。

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()
    )
  );

我需要的是我有一个单独的页面,当执行该特定功能时需要弹出一个单独的页面(当应用程序在后台时)。谁能帮我解决这个问题!提前致谢

标签: firebaseflutterpush-notificationfirebase-cloud-messagingbackground-application

解决方案


    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");
        }
    );

在此代码中,onResume函数将帮助您从后台运行应用程序,以便您可以在 onResume 中编写代码并导航到您指定的屏幕。


推荐阅读