Flutter应用Firebase推送:Android 9通知仅显图标如何实现弹窗?
解决Android 9设备FCM通知仅显示状态栏图标不弹窗的问题
看起来你遇到的是Android O(API 26)及以上版本的通知渠道限制问题——从这个版本开始,所有通知必须归属到一个通知渠道,而渠道的优先级设置直接决定了通知是否会弹窗显示。摩托罗拉设备在通知管理上可能更严格,即使你设置了FCM消息的高优先级,没有正确配置渠道的话还是不会触发弹窗。
下面是具体的解决步骤:
1. 在Flutter端创建高优先级通知渠道
你需要用Flutter的本地通知插件(比如flutter_local_notifications)创建一个优先级为high的通知渠道,这个渠道ID要和你FCM消息里指定的一致。
首先确保你已经添加了依赖:
# pubspec.yaml dependencies: flutter_local_notifications: ^16.1.0 firebase_messaging: ^14.6.4
然后添加渠道创建的代码:
import 'package:flutter/material.dart'; import 'package:firebase_core/firebase_core.dart'; import 'package:firebase_messaging/firebase_messaging.dart'; import 'package:flutter_local_notifications/flutter_local_notifications.dart'; Future<void> setupNotificationChannel() async { // 创建高优先级通知渠道 const AndroidNotificationChannel notificationChannel = AndroidNotificationChannel( 'high_importance_channel', // 渠道ID,后续FCM消息要用到 '重要通知', // 渠道名称(用户在系统设置里能看到) description: '用于显示即时弹窗的重要通知', // 渠道描述 importance: Importance.high, // 高优先级,确保弹窗 playSound: true, enableVibration: true, ); final FlutterLocalNotificationsPlugin notificationsPlugin = FlutterLocalNotificationsPlugin(); // 向系统注册该渠道 await notificationsPlugin .resolvePlatformSpecificImplementation<AndroidFlutterLocalNotificationsPlugin>() ?.createNotificationChannel(notificationChannel); // 同时设置Firebase Messaging的默认渠道展示选项 await FirebaseMessaging.instance.setForegroundNotificationPresentationOptions( alert: true, badge: true, sound: true, ); } void main() async { WidgetsFlutterBinding.ensureInitialized(); await Firebase.initializeApp(); await setupNotificationChannel(); // 初始化渠道 runApp(const MyApp()); }
2. 修改FCM消息的Android配置,指定渠道ID
在你的Firebase PHP SDK代码中,需要在android.notification部分添加channel_id字段,指向你刚才创建的渠道ID:
'android' => [ 'ttl' => '3600s', 'priority' => 'high', "collapse_key"=> $colkey, 'notification' => [ 'notification_priority' => 'PRIORITY_MAX', 'visibility' => 'PUBLIC', 'title' => $title, 'body' => $body, 'notification_count' => $count, 'channel_id' => 'high_importance_channel', // 这里添加渠道ID,和Flutter端一致 ], ],
3. 检查设备的通知渠道权限
摩托罗拉设备可能会默认关闭某些渠道的弹窗权限,你需要让用户手动确认:
- 打开设备的「设置」→「应用和通知」→「你的应用名称」→「通知」
- 找到你创建的「重要通知」渠道,确保「弹窗通知」选项是开启的
4. 确认AndroidManifest的必要配置
虽然你的Manifest已经有了基础配置,但还需要确保Firebase消息服务能正常接收通知。如果还没添加,需要在<application>标签内加入:
<service android:name="com.google.firebase.messaging.FirebaseMessagingService" android:exported="false"> <intent-filter> <action android:name="com.google.firebase.MESSAGING_EVENT" /> </intent-filter> </service>
完成以上步骤后,重新构建Flutter应用并发送通知,Android 9的摩托罗拉设备应该就能正常弹窗显示通知了。
内容的提问来源于stack exchange,提问作者Sebastian
相关产品推荐
相关产品推荐

