我正在尝试使用 FirebaseMessaging ^8.0.0-dev.14 向我的应用程序的用户发送推送通知,但我遇到了一些问题。我可以在 IOS 上收到通知,但在我的 Android 模拟器上没有显示任何内容。我在 init 上设置了侦听器,但他们只能在 IOS 设备上看到通知。有没有人遇到过类似的问题或知道如何在 Android 上显示通知?这是我的代码:
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter/material.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart'
as locNots;
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:url_launcher/url_launcher.dart';
import '../widgets/mentorActionsButton.dart';
import '../widgets/list_of_events.dart';
import 'userPage.dart';
import 'loginPage.dart';
class MentorMainPage extends StatefulWidget {
@override
_MentorMainPageState createState() => _MentorMainPageState();
}
class _MentorMainPageState extends State<MentorMainPage> {
@override
void initState() {
super.initState();
final fbm = FirebaseMessaging.instance;
// IOS Configurations
fbm.setForegroundNotificationPresentationOptions(
alert: true, badge: true, sound: true);
fbm.requestPermission();
FirebaseMessaging.onMessage.listen((RemoteMessage message) {
print('IOS Listener');
print('Got a message whilst in the foreground!');
print('Message data: ${message.data}');
if (message.notification != null) {
print('Message also contained a notification: ${message.notification}');
}
});
//Android Configurations
const AndroidNotificationChannel channel = AndroidNotificationChannel(
'high_importance_channel', // id
'High Importance Notifications', // title
'This channel is used for important notifications.', // description
importance: Importance.max,
);
final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
FlutterLocalNotificationsPlugin();
flutterLocalNotificationsPlugin
.resolvePlatformSpecificImplementation<
AndroidFlutterLocalNotificationsPlugin>()
?.createNotificationChannel(channel);
FirebaseMessaging.onMessage.listen((RemoteMessage message) {
print('Android Listener');
RemoteNotification notification = message.notification;
AndroidNotification android = message.notification?.android;
print('Android Notification:');
if (notification != null && android != null) {
flutterLocalNotificationsPlugin.show(
notification.hashCode,
notification.title,
notification.body,
NotificationDetails(
android: AndroidNotificationDetails(
channel.id, channel.name, channel.description,
icon: android?.smallIcon,
showWhen: false,
importance: Importance.max,
priority: locNots.Priority.high),
));
}
});
}
var userLoggedIn = true;
@override
Widget build(BuildContext context) {
Code for screen view.....
}
答案 0 :(得分:0)
在调用 onMessage() 之前添加这些
await Firebase.initializeApp(); // necessary for initializing firebase
FirebaseMessaging.instance.getInitialMessage().then((RemoteMessage message) {
print('message recived');
});
尝试使 main 函数异步并在那里添加前面的行