I have been trying to allow for local notifications. I have a simple static notification working fine:
< code > Future < void > showNotification () async {
const AndroidNotificationDetails androidNotificationDetails =
AndroidNotificationDetails ( 'your channel id' , 'your channel name' ,
importance: Importance. max ,
const NotificationDetails notificationDetails =
NotificationDetails ( android: androidNotificationDetails ) ;
await flutterLocalNotificationsPlugin. show ( 0 , 'Working Notifications' ,
'Flutter Notifications' , notificationDetails,
<code>Future<void> showNotification() async {
const AndroidNotificationDetails androidNotificationDetails =
AndroidNotificationDetails('your channel id', 'your channel name',
importance: Importance.max,
priority: Priority.high,
ticker: 'ticker');
const NotificationDetails notificationDetails =
NotificationDetails(android: androidNotificationDetails);
await flutterLocalNotificationsPlugin.show(0, 'Working Notifications',
'Flutter Notifications', notificationDetails,
payload: 'item x');
}
</code>
Future<void> showNotification() async {
const AndroidNotificationDetails androidNotificationDetails =
AndroidNotificationDetails('your channel id', 'your channel name',
importance: Importance.max,
priority: Priority.high,
ticker: 'ticker');
const NotificationDetails notificationDetails =
NotificationDetails(android: androidNotificationDetails);
await flutterLocalNotificationsPlugin.show(0, 'Working Notifications',
'Flutter Notifications', notificationDetails,
payload: 'item x');
}
However, as I try to get scheduled notifications working I run into many issues. Permissions for exact alarms, I’ve come up with a workaround by using_ALARMS permission but may end up using inexact depending on the inaccuracy.
TZ was throwing up errors but I made sure the correct timezone was set and initialised within main.dart before initialising the notification service:
< code > Future < void > initNotification () async {
AndroidInitializationSettings initializationSettingsAndroid =
AndroidInitializationSettings ( 'test_noti' ) ;
var initializationSettings =
InitializationSettings ( android: initializationSettingsAndroid ) ;
await flutterLocalNotificationsPlugin. initialize ( initializationSettings,
onDidReceiveNotificationResponse: selectNotification ) ;
await flutterLocalNotificationsPlugin. getNotificationAppLaunchDetails () ;
if ( details?.didNotificationLaunchApp ?? false ) {
var activeNotifications =
await flutterLocalNotificationsPlugin. getActiveNotifications () ;
if ( activeNotifications. isNotEmpty ) {
'Notification launched app: ${activeNotifications.first.payload}' ) ;
<code>Future<void> initNotification() async {
AndroidInitializationSettings initializationSettingsAndroid =
AndroidInitializationSettings('test_noti');
var initializationSettings =
InitializationSettings(android: initializationSettingsAndroid);
await flutterLocalNotificationsPlugin.initialize(initializationSettings,
onDidReceiveNotificationResponse: selectNotification);
var details =
await flutterLocalNotificationsPlugin.getNotificationAppLaunchDetails();
if (details?.didNotificationLaunchApp ?? false) {
var activeNotifications =
await flutterLocalNotificationsPlugin.getActiveNotifications();
if (activeNotifications.isNotEmpty) {
debugPrint(
'Notification launched app: ${activeNotifications.first.payload}');
}
}
}
</code>
Future<void> initNotification() async {
AndroidInitializationSettings initializationSettingsAndroid =
AndroidInitializationSettings('test_noti');
var initializationSettings =
InitializationSettings(android: initializationSettingsAndroid);
await flutterLocalNotificationsPlugin.initialize(initializationSettings,
onDidReceiveNotificationResponse: selectNotification);
var details =
await flutterLocalNotificationsPlugin.getNotificationAppLaunchDetails();
if (details?.didNotificationLaunchApp ?? false) {
var activeNotifications =
await flutterLocalNotificationsPlugin.getActiveNotifications();
if (activeNotifications.isNotEmpty) {
debugPrint(
'Notification launched app: ${activeNotifications.first.payload}');
}
}
}
Scheduled notification function:
< code > Future < void > testSchedule () async {
var permissionStatus = await Permission. scheduleExactAlarm . request () ;
if ( permissionStatus. isGranted ) {
debugPrint ( 'USE_EXACT_ALARM permission granted' ) ;
debugPrint ( 'USE_EXACT_ALARM permission denied' ) ;
tz. TZDateTime scheduledDate =
tz. TZDateTime . now ( tz. local ) . add ( Duration ( seconds: 5 )) ;
await flutterLocalNotificationsPlugin. zonedSchedule (
android: AndroidNotificationDetails ( '0' , 'your channel name' ,
channelDescription: 'your channel description' )) ,
androidScheduleMode: AndroidScheduleMode. exactAllowWhileIdle ,
matchDateTimeComponents: DateTimeComponents. time ,
uiLocalNotificationDateInterpretation:
UILocalNotificationDateInterpretation. absoluteTime ) ;
debugPrint ( 'Scheduled for $scheduledDate' ) ;
debugPrint ( 'Current date ${tz.TZDateTime.now(tz.local)}' ) ;
<code>Future<void> testSchedule() async {
var permissionStatus = await Permission.scheduleExactAlarm.request();
if (permissionStatus.isGranted) {
debugPrint('USE_EXACT_ALARM permission granted');
} else {
debugPrint('USE_EXACT_ALARM permission denied');
}
tz.TZDateTime scheduledDate =
tz.TZDateTime.now(tz.local).add(Duration(seconds: 5));
await flutterLocalNotificationsPlugin.zonedSchedule(
0,
'scheduled title',
'scheduled body',
scheduledDate,
NotificationDetails(
android: AndroidNotificationDetails('0', 'your channel name',
channelDescription: 'your channel description')),
androidScheduleMode: AndroidScheduleMode.exactAllowWhileIdle,
matchDateTimeComponents: DateTimeComponents.time,
uiLocalNotificationDateInterpretation:
UILocalNotificationDateInterpretation.absoluteTime);
debugPrint('Scheduled for $scheduledDate');
debugPrint('Current date ${tz.TZDateTime.now(tz.local)}');
}
</code>
Future<void> testSchedule() async {
var permissionStatus = await Permission.scheduleExactAlarm.request();
if (permissionStatus.isGranted) {
debugPrint('USE_EXACT_ALARM permission granted');
} else {
debugPrint('USE_EXACT_ALARM permission denied');
}
tz.TZDateTime scheduledDate =
tz.TZDateTime.now(tz.local).add(Duration(seconds: 5));
await flutterLocalNotificationsPlugin.zonedSchedule(
0,
'scheduled title',
'scheduled body',
scheduledDate,
NotificationDetails(
android: AndroidNotificationDetails('0', 'your channel name',
channelDescription: 'your channel description')),
androidScheduleMode: AndroidScheduleMode.exactAllowWhileIdle,
matchDateTimeComponents: DateTimeComponents.time,
uiLocalNotificationDateInterpretation:
UILocalNotificationDateInterpretation.absoluteTime);
debugPrint('Scheduled for $scheduledDate');
debugPrint('Current date ${tz.TZDateTime.now(tz.local)}');
}
Function output:
There are 0 other errors or crashes that happen and that’s all the information I get from running the function, the date and time seem to line up fine, the notifications are allowed on my device, dnd silent powersaving are all turned off meaning the fix must be in the code.