Android 15下Flutter项目FirebaseMessaging通知点击API异常求助
Android 15下Flutter FCM通知打开应用无响应的解决方案
问题描述
在Flutter项目中使用Firebase Cloud Messaging时遇到以下问题:Android 15系统上,通过通知打开应用时,FirebaseMessaging.onMessageOpenedApp不会触发,且FirebaseMessaging.instance.getInitialMessage()返回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) {} }
解决方案
- 适配Android 15通知权限
Android 15对通知权限的检查更严格,需确保在AndroidManifest.xml中声明权限,并在运行时主动请求(不仅限于iOS):
在AndroidManifest.xml的<manifest>标签内添加:
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
在NotificationController的initialize方法中补充Android权限请求逻辑(需导入dart:io):
// 新增Android权限请求 if (Platform.isAndroid) { await FirebaseMessaging.instance.requestPermission( alert: true, badge: true, sound: true, ); }
- 升级Firebase Messaging依赖版本
旧版firebase_messaging未适配Android 15的行为变更,打开pubspec.yaml将依赖升级至最新稳定版:
firebase_messaging: ^latest_version
执行flutter pub get更新依赖。
- 确保初始化时机正确
需在Firebase.initializeApp()完成后,再初始化通知控制器,且要确保Widgets绑定已初始化:
void main() async { WidgetsFlutterBinding.ensureInitialized(); await Firebase.initializeApp(); NotificationController().initialize(); runApp(const MyApp()); }
- 检查PendingIntent配置(Android原生层)
Android 15要求PendingIntent必须明确设置FLAG_IMMUTABLE或FLAG_MUTABLE。若你自定义了通知渠道或处理逻辑,需确保创建PendingIntent时使用正确的flag:
// 示例:在自定义FirebaseMessagingService中创建PendingIntent时 PendingIntent pendingIntent = PendingIntent.getActivity( context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_MUTABLE // Android 15要求显式设置 );
- 验证通知Payload格式
确保发送的FCM通知Payload包含data字段,纯notification字段的通知在Android 15上可能无法正确触发应用内监听。示例Payload:
{ "to": "DEVICE_TOKEN", "notification": { "title": "测试通知", "body": "点击打开应用" }, "data": { "screen": "home", "id": "123" } }
内容的提问来源于stack exchange,提问作者Stefano Sommacal
相关产品推荐
相关产品推荐

