Firebase 云消息传递。 onMessage.onListen 不起作用。 (颤振机器人)

时间:2021-03-25 15:53:07

标签: firebase flutter notifications firebase-cloud-messaging

信息

  • Firebase 云消息传递 onMessage.listen 不起作用。它没有错误 它什么都不做。甚至没有激活 onListen 功能。
  • 可以正常发送消息。
  • 已经在android/app/build.gradle中添加了需要的代码

pubspec.yaml

firebase_messaging: ^8.0.0-dev.10

ma​​in.dart

 Future<void> main() async {
      await Firebase.initializeApp();
      FirebaseMessaging.onBackgroundMessage(FirebaseMessagingBackgroundHandler);
      await flutterLocalNotificationsPlugin
          .resolvePlatformSpecificImplementation<
              AndroidFlutterLocalNotificationsPlugin>()
          ?.createNotificationChannel(channel);
    
      runApp(ModularApp(
        module: AppModule(),
      ));
    }
    
    const AndroidNotificationChannel channel = AndroidNotificationChannel(
      'high_importance_channel',
      'High Importance Notifications', 
      'This channel is used for important notifications.', 
      importance: Importance.high,
    );
    
    final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
        FlutterLocalNotificationsPlugin();
    
    class AppWidget extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        onListen();
        return LayoutBuilder(
            builder: (BuildContext context, BoxConstraints constraints) {
          return OrientationBuilder(
            builder: (BuildContext context, Orientation orientation) {
              return MaterialApp(
                initialRoute: "/main",
                title: 'Flutter Demo',
              );
            },
          );
        });
      }
    }

onListen 方法

 onListen() {
    FirebaseMessaging.onMessage.listen((RemoteMessage message) {
    RemoteNotification notification = message.notification;
    AndroidNotification android = message.notification?.android;
    if (notification != null && android != null) {
      flutterLocalNotificationsPlugin.show(//....);
    }
  });
}

0 个答案:

没有答案