I am interested in getting a notification in the status bar. Nothing else, just the status bar after a file is downloaded. Tapping the notification should open the my files app/location.
final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();
@override
void initState() {
super.initState();
_initializeNotifications();
// Register the notification tap handler
flutterLocalNotificationsPlugin
.getNotificationAppLaunchDetails()
.then((details) {
if (details?.didNotificationLaunchApp ?? false) {
_handleNotificationTap(details!.payload);
}
});
}
The above code has an issue
The getter 'payload' isn't defined for the type 'NotificationAppLaunchDetails'.
Try importing the library that defines 'payload', correcting the name to the name of an existing getter, or defining a getter or field named 'payload'.
Future<void> _initializeNotifications() async {
const AndroidInitializationSettings initializationSettingsAndroid =
AndroidInitializationSettings(
'@mipmap/ic_launcher'); // Using default launcher icon
const InitializationSettings initializationSettings =
InitializationSettings(
android: initializationSettingsAndroid,
);
await flutterLocalNotificationsPlugin.initialize(initializationSettings);
}
Future<void> _showStatusBarNotification(String message) async {
const AndroidNotificationDetails androidPlatformChannelSpecifics =
AndroidNotificationDetails(
'your_channel_id', // Replace with your actual channel ID
'your_channel_name', // Replace with your actual channel name
channelDescription: 'your_channel_description',
importance: Importance.max,
priority: Priority.high,
showWhen: false, // Don't show timestamp in notification
icon: '@mipmap/ic_launcher', // Use your app's launcher icon
);
const NotificationDetails platformChannelSpecifics =
NotificationDetails(android: androidPlatformChannelSpecifics);
await flutterLocalNotificationsPlugin.show(
0, // Notification ID (unique for each notification)
'Download Complete', // Notification title
message, // Notification message
platformChannelSpecifics,
payload: '/storage/emulated/0/Download', // Payload for handling tap
);
}
void _handleNotificationTap(String? payload) {
if (payload != null) {
// Intent to open Downloads directory
AndroidIntent intent = AndroidIntent(
action: 'android.intent.action.VIEW',
data: Uri.parse('file://$payload').toString(), // Use file:// prefix
flags: <int>[Flag.FLAG_ACTIVITY_NEW_TASK],
);
intent.launch();
}
}
This is part of a function that performs an API request
final filePath = '${directory.path}/data.xlsx';
final file = File(filePath);
await file.writeAsBytes(response.bodyBytes);
final downloadsPath = await _moveFileToDownloads(filePath);
if (downloadsPath != null) {
_showStatusBarNotification('temperature_data.xlsx downloaded');
} else {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text('Error moving file to downloads directory')),
);
}
These are the plugins
path_provider: ^2.1.3
flutter_local_notifications: ^17.1.2
android_intent_plus: ^5.0.2
I really don’t know what else to write. SO is asking for more details. I think that my question is clear.