当应用程序处于后台时,不会触发firebaseMessagingBackgroundHandler

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

我假设我遵循了在 flutter 中处理来自 firebase 的后台通知的所有步骤。我创建了一个 top-level 函数,希望在收到通知时触发该函数。但是,该函数永远不会被触发。

这是我的主页小部件中存在但在类之外的顶级后台处理程序函数:

Future<void> firebaseMessagingBackgroundHandler(RemoteMessage message) async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();
  // ignore: avoid_print
  print('A background message just showed up :  ${message.messageId}');

  // update SQLite
  var result = await PageService.instance
      .add(PageService.instance.convertToPage(message.data));
  print('added to db: ${result}');
}

这是我的主页初始化状态,它调用函数来初始化 Firebase 消息:

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

    _initializeFirebaseMessaging();
  }

然后这里也是在主页类中定义的 _initializeFirebaseMessaging 函数:

void _initializeFirebaseMessaging() {
    FirebaseMessaging.onBackgroundMessage(firebaseMessagingBackgroundHandler);

    FirebaseMessaging.onMessage.listen((RemoteMessage message) async {
      print('new notification arrived');
      RemoteNotification? notification = message.notification;
      AndroidNotification? android = message.notification?.android;

      // update SQLite
      var result = await PageService.instance
          .add(PageService.instance.convertToPage(message.data));
      print('added to db: ${result}');

      if (notification != null && android != null) {
        // show notification
        flutterLocalNotificationsPlugin.show(
            notification.hashCode,
            notification.title,
            notification.body,
            NotificationDetails(
              android: AndroidNotificationDetails(
                channel.id,
                channel.name,
                color: Colors.blue,
                playSound: true,
                icon: '@mipmap/ic_launcher',
              ),
            ));
      }
    });
  }

onmessage.listen 工作正常,因为我在应用程序中收到通知并处理它们,但后台处理程序根本没有触发。

我将不胜感激任何帮助!

flutter firebase push-notification firebase-cloud-messaging
4个回答
1
投票

您必须在 main() 中而不是 initState() 中调用 FirebaseMessaging.onBackgroundMessage


1
投票

onMessage
处理前台通知

onMessageOpenedApp
处理后台通知

如果您想在应用程序处于

background
时收到通知,请使用
onMessage
添加另一个
FirebaseMessaging
功能块,而不是使用 onMessageOpenedApp 来处理后台通知,如下所示:

FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message){...})

onMessage
仅当应用程序位于前台时才有效。 如果您希望在应用程序处于“终止”状态时收到通知,您可以使用: FirebaseMessaging.instance.getInitialMessage().then((message){...})

将您的 
firebaseMessagingBackgroundHandler

更新为:

Future<void> firebaseMessagingBackgroundHandler(RemoteMessage message) async {
  await Firebase.initializeApp();
  
  // ignore: avoid_print
  print('A background message just showed up :  ${message.messageId}');

  // update SQLite
  var result = await PageService.instance
      .add(PageService.instance.convertToPage(message.data));
  print('added to db: ${result}');
}

最后,
main()

应该看起来像这样: Future<void> main() async { WidgetsFlutterBinding.ensureInitialized(); // Add Firebase Initialization: await Firebase.initializeApp(); // This will initialize a new Firebase instance. // Background message handler FirebaseMessaging.onBackgroundMessage(firebaseMessagingBackgroundHandler); runApp(const App()); }

您应该从 
main()

中调用

FirebaseMessaging.onBackgroundMessage(firebaseMessagingBackgroundHandler);
,而不是从主页 initState(){}
    


0
投票

Future<void> main() async { WidgetsFlutterBinding.ensureInitialized(); await Firebase.initializeApp(); ... }

并删除 
firebaseMessagingBackgroundHandler

中的 WidgetsFlutterBinding.ensureInitialized();


0
投票
onBackgroundMessage

处理程序已遵循以下步骤:


它不能是匿名函数。
  1. 它必须是顶级函数(例如,不是需要初始化的类方法)。
  2. 使用 Flutter 3.3.0 或更高版本时,消息处理程序必须在函数声明正上方用
  3. @pragma('vm:entry-point')
  4. 进行注释(否则在发布模式的 tree shake 过程中可能会被删除)。
    
    
  5. 来源:
https://firebase.google.com/docs/cloud-messaging/flutter/receive#apple_platforms_and_android

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