当前位置: 首页 > 知识库问答 >
问题:

我正在尝试为我的移动应用程序获取firebase云消息,但我不断收到平台异常错误

吴浩皛
2023-03-14

嗨,我想为我的应用程序添加云消息。我已经完成了将Firebase与我的应用程序集成的所有必要步骤。然后,我按照youtube视频上的步骤让firebase云消息为我的应用程序工作,然后这些错误就出现了。在添加所有firebase相关代码之前,该应用程序运行良好。现在我只得到一个白色屏幕。这是我的主要。飞奔

import 'package:flutter/material.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:get/get_navigation/src/root/get_material_app.dart';
import 'splashScreen.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_messaging/firebase_messaging.dart';

const AndroidNotificationChannel channel = AndroidNotificationChannel('high_importance_channel',
    'High Importance Notifications',
importance: Importance.high,
playSound: true);

final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
    FlutterLocalNotificationsPlugin();

Future<void> _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,
  );
  runApp(MyApp());
}

class MyApp extends StatefulWidget {
  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  @override
  void initState(){
    super.initState();
    FirebaseMessaging.onMessage.listen((RemoteMessage message){
    RemoteNotification? notification = message.notification;
    AndroidNotification? android = message.notification?.android;
    if(notification!=null && android!=null){
      flutterLocalNotificationsPlugin.show(
        notification.hashCode,
        notification.title,
        notification.body,
        NotificationDetails(
          android: AndroidNotificationDetails(
            channel.id,
            channel.name,
            color: Colors.blue,
            playSound: true,
            icon: '@ipmap/ic_launcher',

          )
        )
      );
    }
    });
    
    FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) {
    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) {
    return GetMaterialApp(
      home: SplashScreen(),
    );
  }
}

在此之前,我遇到了另一个错误,但在我更改依赖项后得到了解决,因此这不是问题。这就是我得到的错误

E/flutter ( 3318): [ERROR:flutter/lib/ui/ui_dart_state.cc(198)] Unhandled Exception: PlatformException(channel-error, Unable to establish connection on channel., null, null)
E/flutter ( 3318): #0      FirebaseCoreHostApi.initializeCore (package:firebase_core_platform_interface/src/pigeon/messages.pigeon.dart:205:7)
E/flutter ( 3318): <asynchronous suspension>
E/flutter ( 3318): #1      MethodChannelFirebase._initializeCore (package:firebase_core_platform_interface/src/method_channel/method_channel_firebase.dart:29:44)
E/flutter ( 3318): <asynchronous suspension>
E/flutter ( 3318): #2      MethodChannelFirebase.initializeApp (package:firebase_core_platform_interface/src/method_channel/method_channel_firebase.dart:73:7)
E/flutter ( 3318): <asynchronous suspension>
E/flutter ( 3318): #3      Firebase.initializeApp (package:firebase_core/src/firebase.dart:40:31)
E/flutter ( 3318): <asynchronous suspension>
E/flutter ( 3318): #4      main (package:activepeers_app_internship/main.dart:23:3)
E/flutter ( 3318): <asynchronous suspension>
E/flutter ( 3318): 
I/TRuntime.CctTransportBackend( 3318): Making request to: https://firebaselogging.googleapis.com/v0cc/log/batch?format=json_proto3
D/NetworkSecurityConfig( 3318): No Network Security Config specified, using platform default
I/TRuntime.CctTransportBackend( 3318): Status Code: 200

共有1个答案

劳烨
2023-03-14

我能够通过更新所有firebase依赖项来修复此问题。以下命令对我有效。

flutter pub outdated
flutter pub upgrade
 类似资料: