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
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: