I use flutter local notification package to show FCM notifications and every thing is okay it show notifications in foreground , background and terminated modes . After I add custom sound Notification worked in foreground and background modes only . Doesn't receive any thing in terminated mode . When I open the app again I received the missed notifications "while terminated " .
Here is my notification class
import 'dart:math';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
class Noti{
static AndroidNotificationDetails androidPlatformChannelSpecifics =
const AndroidNotificationDetails(
'SchoolGObus_horn',
'SchoolGObus_horn',
sound: RawResourceAndroidNotificationSound('bus_horn'),
playSound: true,
importance: Importance.max,
priority: Priority.high,
);
static Future initialize(FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin) async {
var androidInitialize = const AndroidInitializationSettings('@mipmap/launcher_icon');
var iOSInitialize = const DarwinInitializationSettings();
var initializationsSettings = InitializationSettings(android: androidInitialize,
iOS: iOSInitialize);
await flutterLocalNotificationsPlugin.initialize(initializationsSettings );
await flutterLocalNotificationsPlugin.getNotificationAppLaunchDetails();
await flutterLocalNotificationsPlugin
.resolvePlatformSpecificImplementation<AndroidFlutterLocalNotificationsPlugin>()
?.createNotificationChannel(androidPlatformChannelSpecifics as AndroidNotificationChannel);
}
static Future showBigTextNotification({required String title, required String body,
var payload, required FlutterLocalNotificationsPlugin fln
} ) async {
int id = getRandomInteger() ;
var not= NotificationDetails(android: androidPlatformChannelSpecifics,
iOS: const DarwinNotificationDetails()
);
await fln.show(id, title, body,not );
}
static int getRandomInteger() {
final random = Random();
return random.nextInt(10000);
}
}
Here is my Background Listen function :
@pragma('vm:entry-point')
Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
FlutterLocalNotificationsPlugin();
Noti.initialize(flutterLocalNotificationsPlugin);
await Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform,
);
print("Handling a background message: ${message.messageId}");
Noti.showBigTextNotification(
title: '${message.data['action']}',
body: '${message.data['message']}',
fln: flutterLocalNotificationsPlugin);
}
Here is related code in main :
await Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform,
);
await FirebaseMessaging.instance.getInitialMessage();
AppConstance.deviceToken = await FirebaseMessaging.instance.getToken();
Noti.initialize(flutterLocalNotificationsPlugin);
FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);
And I have flutter local notification as global
final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
FlutterLocalNotificationsPlugin();
I need notification working in terminated mode