【发布时间】:2022-07-14 22:30:05
【问题描述】:
您好,我想为我的应用添加云消息传递。我已经完成了将 Firebase 与我的应用程序集成的所有必要步骤。然后我按照youtube video 上的步骤为我的应用程序获取 Firebase 云消息传递工作,然后弹出这些错误。在添加所有与 firebase 相关的代码之前,该应用程序运行良好。现在我只是得到一个白屏。这是我的 main.dart
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
【问题讨论】:
-
尝试按照此处的说明初始化 flirebase stackoverflow.com/a/71990307/10409567
标签: flutter firebase firebase-cloud-messaging