颤振:未收到来自 fcm 的通知

时间:2021-07-14 06:35:52

标签: flutter firebase-cloud-messaging

嗨,我的 flutter 应用程序没有收到 fcm 通知,localnotification 工作正常。 我安装了以下软件包并设置了 firebase 项目,并将 google-services.json 文件放在项目的 android\app 文件夹中。更晚,我没有收到任何错误,也没有收到 fcm 通知。请帮帮我,在此先感谢

firebase_core:^0.7.0

firebase_messaging:^8.0.0-dev.15

flutter_local_notifications: ^4.0.1+2

import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';

const AndroidNotificationChannel channel = AndroidNotificationChannel(
    'high_importance_channel', // id
    'High Importance Notifications', // title
    'This channel is used for important notifications.', // description
    importance: Importance.high,
    playSound: true);

final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
    FlutterLocalNotificationsPlugin();

Future _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
  await Firebase.initializeApp();
  print('A bg message just showed up : ${message.messageId}');
}

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

await flutterLocalNotificationsPlugin
          .resolvePlatformSpecificImplementation<
              AndroidFlutterLocalNotificationsPlugin>()
          ?.createNotificationChannel(channel);

  await FirebaseMessaging.instance.setForegroundNotificationPresentationOptions(
    alert: true,
    badge: true,
    sound: true,
  );

  Provider.debugCheckInvalidValueType = null;
  runApp(MultiProvider(
    providers: [
      ChangeNotifierProvider<StatesHandling>(
          create: (context) => StatesHandling()),
    ],
    child: MyApp2(),
  ));
}

class MyApp2 extends StatefulWidget {
  @override
  _MyApp2State createState() => _MyApp2State();
}

class _MyApp2State extends State<MyApp2> {
    @override
  void initState() {
    super.initState();

     FirebaseMessaging.onMessage.listen((RemoteMessage message) {
      RemoteNotification notification = message.notification;
      AndroidNotification android = message.notification?.android;
      print("1");
      if (notification != null && android != null) {
        
        flutterLocalNotificationsPlugin.show(
            notification.hashCode,
            notification.title,
            notification.body,
            NotificationDetails(
              android: AndroidNotificationDetails(
                channel.id,
                channel.name,
                channel.description,
                color: Colors.blue,
                playSound: true,
                icon: '@mipmap/ic_launcher',
              ),
            ));
      }
    });

    FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) {
      print("2");
      print('A new onMessageOpenedApp event was published!');
      RemoteNotification notification = message.notification;
      AndroidNotification android = message.notification?.android;
      if (notification != null && android != null) {
        showDialog(
            context: context,
            builder: (_) {
              return AlertDialog(
                title: Text(notification.title),
                content: SingleChildScrollView(
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [Text(notification.body)],
                  ),
                ),
              );
            });
      }
    });
  }
  @override
  Widget build(BuildContext context) {
    SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle(
        // systemNavigationBarColor: Color(0xFF1c2124), // navigation bar color
        statusBarColor: Colors.black, // status bar color
        statusBarBrightness: Brightness.light));
    return MaterialApp(
      title: "MyApp",
      theme: ThemeData(
          primaryColor: Color(0xFF1c2124),
          fontFamily: 'Arial',
          //0xFFf57e30 color using
          textTheme: TextTheme(
              headline1: TextStyle(
                  fontFamily: 'Poppins',
                  fontSize: 25.0,
                  fontWeight: FontWeight.w700,
                  color: Colors.black87))),
      home: new Home(),
      debugShowCheckedModeBanner: false,
    );
  }
}

0 个答案:

没有答案