My app notification work good in background and foreground and have this problem in terminated state
I use flutter local notification and flutter firebase messaging .
this is my firebase messaging background listing function .
@pragma('vm:entry-point')
Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
FlutterLocalNotificationsPlugin();
await Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform,
);
Noti.initialize(flutterLocalNotificationsPlugin) ;
Noti.showBigTextNotification(
title: '${message.data['action']}',
body: '${message.data['message']}', fln: flutterLocalNotificationsPlugin);
}
void main() async {
WidgetsFlutterBinding.ensureInitialized();
DioHelper.init();
setUpServiceLocator();
Bloc.observer = MyBlocObserver();
await CacheHelper.init();
String? parentauthCode = CacheHelper.getData(key: 'authCode');
String? driverAuthCode = CacheHelper.getData(key: 'driverAuthCode');
Widget startWidget = TryingToEnter();
if (parentauthCode != null) {
AppConstance.parentauthCode = parentauthCode;
startWidget = HomeScreen();
}
if (driverAuthCode != null) {
AppConstance.driverauthCode = driverAuthCode;
startWidget = DriverStartJourneyScreen();
}
await Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform,
);
await FirebaseMessaging.instance.getInitialMessage();
AppConstance.deviceToken = await FirebaseMessaging.instance.getToken();
Noti.initialize(flutterLocalNotificationsPlugin);
FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);
runApp(MultiBlocProvider(
providers: [
BlocProvider<StudentsCubit>(
create: (context) => StudentsCubit(getIt.get<StudentRepo>())
..getStudentMatchList()
..getMyCurrentLocation()),
BlocProvider(
create: (context) =>
ProfileCubit(getIt.get<ProfileRepo>())..getUserData()),
BlocProvider(
create: (context) =>
TripsHistoryCubit(getIt.get<TripsHistoryRepo>())
..getTripsHistoryList()),
BlocProvider(
create: (context) =>
MessagesCubit(getIt.get<MessageRepo>())..getMessagesList()),
BlocProvider(
create: (context) =>
NotificationsCubit(getIt.get<NotificationsRebo>())
..getNotificationsList()),
BlocProvider(
create: (context) => DriverAuthCubit(getIt.get<DriverAuthRebo>())),
BlocProvider(
create: (context) =>
DriverProfileCubit(getIt.get<DriverProfileRebo>())
..getDriverProfileData()),
BlocProvider(
create: (context) => DriverHomeCubit(getIt.get<DriverHomeRebo>())
..getDriverStudentAssignedList()),
BlocProvider(
create: (context) => DriverRoutesCubit(getIt.get<DriverRoutesRepo>())
..getDriverRoutesData(),
),
BlocProvider(
create: (context) => DriverMapCubit()..getMyCurrentLocation()),
BlocProvider(
create: (context) =>
DriverNotificationsCubit(getIt.get<DriverNotificationsRebo>())
..getDriverNotifications())
],
child: MainApp(
startWidget: startWidget,
)));
}
class MainApp extends StatelessWidget {
final Widget startWidget;
MainApp({required this.startWidget, super.key});
@override
Widget build(BuildContext context) {
return ScreenUtilInit(
designSize: const Size(360, 690),
minTextAdapt: true,
splitScreenMode: true,
builder: (_, context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: startWidget,
builder: EasyLoading.init(),
);
});
}
}
this is my flutter local notification implementation :
import 'dart:math';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
class Noti {
static Future initialize(
FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin) async {
var androidInitialize =
const AndroidInitializationSettings('@mipmap/launcher_icon');
var iOSInitialize = const DarwinInitializationSettings(
requestAlertPermission: true,
requestBadgePermission: true,
requestSoundPermission: true,
);
var initializationsSettings =
InitializationSettings(android: androidInitialize, iOS: iOSInitialize);
await flutterLocalNotificationsPlugin.initialize(initializationsSettings);
}
static Future showBigTextNotification(
{required String title,
required String body,
var payload,
required FlutterLocalNotificationsPlugin fln,
}) async {
var not = const NotificationDetails(
android: AndroidNotificationDetails(
'SchoolGObus_horn',
'SchoolGObus_horn',
playSound: true,
sound: RawResourceAndroidNotificationSound("bus_horn"),
importance: Importance.max,
priority: Priority.high,
) ,
iOS: DarwinNotificationDetails());
await fln.show( 0 , title, body, not);
}
static int getRandomInteger() {
final random = Random();
return random.nextInt(10000);
}
}
Solving terminated problem .