I am using flutter local notification package to schedule periodic notification. When I run the app, notifications are received for foreground and background state of the app but when the app is in terminated state, the notifications are not received.
I have provided main.dart file code below.
void main() async {
WidgetsFlutterBinding.ensureInitialized();
initializeTimeZones();
AndroidInitializationSettings androidSettings = AndroidInitializationSettings("@mipmap/ic_launcher");
DarwinInitializationSettings iosSettings = DarwinInitializationSettings(
requestAlertPermission: true,
requestBadgePermission: true,
requestCriticalPermission: true,
requestSoundPermission: true
);
InitializationSettings initializationSettings = InitializationSettings(
android: androidSettings,
iOS: iosSettings
);
bool? initialized = await notificationsPlugin.initialize(
initializationSettings,
onDidReceiveNotificationResponse: (response) {
log(response.payload.toString());
}
);
void showNotification() async {
AndroidNotificationDetails androidDetails = AndroidNotificationDetails(
"notifications-youtube",
"YouTube Notifications",
priority: Priority.max,
importance: Importance.max
);
DarwinNotificationDetails iosDetails = DarwinNotificationDetails(
presentAlert: true,
presentBadge: true,
presentSound: true,
);
NotificationDetails notiDetails = NotificationDetails(
android: androidDetails,
iOS: iosDetails
);
DateTime scheduleDate = DateTime.now().add(Duration(seconds: 5));
await notificationsPlugin.periodicallyShow(0, 'repeating title',
'repeating body', RepeatInterval.everyMinute, notiDetails,
androidAllowWhileIdle: true);
}
void checkForNotification() async {
NotificationAppLaunchDetails? details = await notificationsPlugin.getNotificationAppLaunchDetails();
if(details != null) {
if(details.didNotificationLaunchApp) {
NotificationResponse? response = details.notificationResponse;
if(response != null) {
String? payload = response.payload;
log("Notification Payload: $payload");
}
}
}
}
log("Notifications: $initialized");
@override
void initState(){
checkForNotification();
}
showNotification();
runApp(MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: HomeScreen(),
);
}
}
Thank you in advance