flutter_local_notifications 在 Android 6 以上设备中发布模式下不显示前台通知
我正在使用 Firebase Cloud Messaging(FCM) 将推送通知发送到我的 Flutter 应用程序。我需要在 Android 的前台显示通知,因此尝试了 FlutterFire 文档中提到的插件 -flutter_local_notifications ,但它似乎并不适用于所有设备。我发现它只能在运行 Android 6 的一台设备上运行。
我在 AndroidManifest.xml
中添加了这一行 -
<meta-data
android:name="com.google.firebase.messaging.high_importance_channel"
android:value="high_importance_channel"/>
如何在接收 FCM 消息时触发本地通知 -
// local notif initialisation //
var initializationSettingsAndroid = new AndroidInitializationSettings('@mipmap/ic_launcher');
var initializationSettingsIOS = new IOSInitializationSettings();
var initializationSettings = new InitializationSettings(android: initializationSettingsAndroid, iOS: initializationSettingsIOS);
flutterLocalNotificationsPlugin = new FlutterLocalNotificationsPlugin();
flutterLocalNotificationsPlugin.initialize(initializationSettings, onSelectNotification: onSelectNotification);
/// Foreground
FirebaseMessaging.onMessage.listen((RemoteMessage message) {
if (message.notification != null) {
print('Foreground (onMessage): Title:${message.notification.title}, Subtitle:${message.notification.body}, Data:${message.data.toString()}');
remoteMessage = message;
var data = json.decode(message.data['metadata']);
showNotification(
1234,
"${message.notification.title}",
"${message.notification.body}",
"$message",
);
}
});
我还尝试创建自定义通知通道,例如 我不确定
AndroidNotificationChannel channel = AndroidNotificationChannel(
'high_importance_channel', // id
'High Importance Notifications', // title
description: 'This channel is used for important notifications.', // description
importance: Importance.max,
);
await flutterLocalNotificationsPlugin
.resolvePlatformSpecificImplementation<AndroidFlutterLocalNotificationsPlugin>()
?.createNotificationChannel(channel);
我是否遗漏了什么?对于更高的 Android API 级别是否有任何额外的步骤。插件的自述文件中有一条标题为“发布构建配置” 的注释,要求进行 ProGuard 文件自定义,我也遵循了这些步骤,但对我的情况没有任何帮助。正在寻找有关此问题的一些帮助。
依赖版本:
firebase_core: ^1.13.1
firebase_messaging: ^11.2.8
flutter_local_notifications: ^9.2.0
Flutter SDK 版本:Flutter 2.10.2
I'm using Firebase Cloud Messaging(FCM) to send push notifications to my Flutter app. I need to show notifications in Foreground in Android, so tried the plugin -flutter_local_notifications as mentioned in the FlutterFire documentation, but it doesn't seem to work on all devices. I found it working only on one device running on Android 6.
I added this line in AndroidManifest.xml
-
<meta-data
android:name="com.google.firebase.messaging.high_importance_channel"
android:value="high_importance_channel"/>
How I'm triggering local notifications on receiving FCM message -
// local notif initialisation //
var initializationSettingsAndroid = new AndroidInitializationSettings('@mipmap/ic_launcher');
var initializationSettingsIOS = new IOSInitializationSettings();
var initializationSettings = new InitializationSettings(android: initializationSettingsAndroid, iOS: initializationSettingsIOS);
flutterLocalNotificationsPlugin = new FlutterLocalNotificationsPlugin();
flutterLocalNotificationsPlugin.initialize(initializationSettings, onSelectNotification: onSelectNotification);
/// Foreground
FirebaseMessaging.onMessage.listen((RemoteMessage message) {
if (message.notification != null) {
print('Foreground (onMessage): Title:${message.notification.title}, Subtitle:${message.notification.body}, Data:${message.data.toString()}');
remoteMessage = message;
var data = json.decode(message.data['metadata']);
showNotification(
1234,
"${message.notification.title}",
"${message.notification.body}",
"$message",
);
}
});
I have also tried creating custom notification channel like this
AndroidNotificationChannel channel = AndroidNotificationChannel(
'high_importance_channel', // id
'High Importance Notifications', // title
description: 'This channel is used for important notifications.', // description
importance: Importance.max,
);
await flutterLocalNotificationsPlugin
.resolvePlatformSpecificImplementation<AndroidFlutterLocalNotificationsPlugin>()
?.createNotificationChannel(channel);
I'm not sure if I'm missing something? Are there any additional steps for higher Android API levels. There was a note on the plugin's Readme titled Release build configuration
calling for ProGuard file customisation, I followed those steps too, but nothing helped in my case. Looking for some help on this issue.
Dependency versions:
firebase_core: ^1.13.1
firebase_messaging: ^11.2.8
flutter_local_notifications: ^9.2.0
Flutter SDK Version : Flutter 2.10.2
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在
android\app\
目录中添加proguard-rules.pro
。正如 flutter_location_notification 建议的那样,您可以尝试在
android\app\build.gradle
> 中添加以下行:buildTypes
> >release
buildTypes 将如下所示:
Add the
proguard-rules.pro
inandroid\app\
directory. As flutter_location_notification suggests which isYou can try adding the following line in
android\app\build.gradle
>buildTypes
>release
The buildTypes will look like following: