我將顫振應用程式與 Firebase 集成。當應用程式在后臺時收到推送通知沒有問題,但我沒有收到前臺通知。
我添加了處理前臺訊息,現在它拋出 FirebaseException([core/no-app] No Firebase App '[DEFAULT]' has been created - call Firebase.initializeApp()),盡管我在主方法中添加了 initilizeApp()。
這是我的主要內容:
WidgetsFlutterBinding.ensureInitialized();
Firebase.initializeApp().then((value) => print("Firebase initialized"));
runApp(const MyApp());
這就是我在 MyApp 中處理前臺通知的方式:
// It is assumed that all messages contain a data field with the key 'type'
Future<void> setupInteractedMessage() async {
// Get any messages which caused the application to open from
// a terminated state.
RemoteMessage? initialMessage =
await FirebaseMessaging.instance.getInitialMessage();
// If the message also contains a data property with a "type" of "chat",
// navigate to a chat screen
if (initialMessage != null) {
_handleMessage(initialMessage);
}
// Also handle any interaction when the app is in the background via a
// Stream listener
FirebaseMessaging.onMessageOpenedApp.listen(_handleMessage);
}
void _listenForMessages() {
FirebaseMessaging.onMessage.listen((RemoteMessage message) {
if (message.notification != null) {
setState(() {
String? body = message.notification?.body;
if (body != null) {
} else {}
});
}
});
}
void _handleMessage(RemoteMessage message) {
if (message.data['type'] == 'chat') {
Navigator.pushNamed(
context,
'/chat',
arguments: message,
);
}
}
@override
void initState() {
super.initState();
_requestPermission();
setupInteractedMessage();
_listenForMessages();
}
void _requestPermission() async {
NotificationSettings settings =
await FirebaseMessaging.instance.requestPermission(
alert: true,
announcement: false,
badge: true,
carPlay: false,
criticalAlert: false,
provisional: false,
sound: true,
);
if (settings.authorizationStatus == AuthorizationStatus.authorized) {
} else if (settings.authorizationStatus ==
AuthorizationStatus.provisional) {
} else {}
}
我在互聯網上發現的所有錯誤都說在 main 方法中呼叫 initilazieApp(),但我已經這樣做了,但錯誤不會消失。
uj5u.com熱心網友回復:
使用 await Firebase.initializeApp();
你必須添加await
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/529198.html
標籤:Google Cloud Collective 安卓IOS扑火力基地firebase-云消息传递
