null
。 在Android 10上,一切正常。我正在用来处理通知的课程。
// notification_controller.dart
import 'package:firebase_messaging/firebase_messaging.dart';
class NotificationController {
final FirebaseMessaging \_firebaseMessaging = FirebaseMessaging.instance;
// Initialize Firebase Messaging
void initialize() {
// Request for permission on iOS
_firebaseMessaging.requestPermission();
// Get the token to identify the device
_firebaseMessaging.getToken().then((token) {
print("Firebase Messaging Token: $token");
});
// Handle the background message
FirebaseMessaging.onBackgroundMessage(_backgroundMessageHandler);
// Handle foreground messages
FirebaseMessaging.onMessage.listen((RemoteMessage message) {
print('Message received: ${message.notification?.title}');
// You can add custom behavior for foreground messages here
});
// Handle when the app is opened via a notification
FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) {
print('Opened from onMessageOpenedApp: ${message.toMap()}');
_handleNavigation(message);
});
// Handle initial message when the app is launched
FirebaseMessaging.instance
.getInitialMessage()
.then((RemoteMessage? message) {
print('Opened from getInitialMessage: ${message?.notification?.title}');
if (message != null) {
_handleNavigation(message);
}
});
}
// Background message handler (optional)
static Future<void> _backgroundMessageHandler(message) async {
print("Background message received: ${message.notification?.title}");
}
// Handle navigation based on the notification payload
void _handleNavigation(message) {}
}
@pragma('vm:entry-point')
Future<void> _backgroundMessageHandler(RemoteMessage message) async {
}
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
FirebaseMessaging.onBackgroundMessage(_backgroundMessageHandler);
await NotificationController.initialize();
runApp(const MyApp());
}
步调这一,还检查Android侧的必要实现
implementation(platform("com.google.firebase:firebase-bom:33.10.0"))
id("com.google.gms.google-services") version "4.4.2" apply false