当flutter android应用程序处于终止状态时,单击通知会调用哪个函数,后台通知单击FCM

问题描述 投票:0回答:1

我正在使用 flutter_local_notifications: ^15.1.0+1 使用 FCM 显示通知。 我能够处理前台和后台点击上的通知点击,但我找不到在应用程序处于终止状态时在通知点击上调用的函数。 欢迎任何帮助。

onDidReceiveNotificationResponse
当应用程序处于后台或前台时被调用

flutter notifications firebase-cloud-messaging terminate localnotification
1个回答
0
投票

flutter_local_notifications
将处理
foreground
通知中的传入消息。因为无论设置什么通知通道,
Firebase Android SDK
都会阻止显示任何 FCM 通知。

阅读此处了解详细信息:https://firebase.flutter.dev/docs/messaging/notifications#application-in-foreground


teminated
状态下,无需使用本地通知。如果您仍然使用
flutter_local_notification
来处理
background
状态,您将同时收到 2 个通知。

要处理收到的消息,请阅读此文档:https://firebase.google.com/docs/cloud-messaging/flutter/receive


  • 每次收到通知时调用方法

你可以在这里调用你的方法,如果通知在终止状态下传入,它会自动调用

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

但是,如果您需要交互,例如,当用户在“终止状态”下单击通知时,您想要导航特定屏幕,请阅读以下内容:

  • 互动

默认情况下,当我们点击通知时,会触发打开应用程序。 根据我的项目,我在导航到主屏幕之前在

splash 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);
   }
 }
...

}
© www.soinside.com 2019 - 2024. All rights reserved.