3

I am working on a flutter app. I am creating a pdf file in the "Downloads" folder, after creating the file in the "Downloads" folder, showing a notification "Download Completed" using the "local_notification" plugin.

Now I want to show that pdf when the user clicks on the notification. Can anybody guide me on how I can do that?

Below is my code

final android = AndroidNotificationDetails('0', 'Adun Accounts',
        channelDescription: 'channel description',
        priority: Priority.high,
        importance: Importance.max,
        icon: '');
    final iOS = IOSNotificationDetails();
    final platform = NotificationDetails(android: android, iOS: iOS);

    await flutterLocalNotificationsPlugin.show(
        0, // notification id
        fileName,
        'Download complete.',
        platform);
user1595266
  • 85
  • 3
  • 12

1 Answers1

4

show() has a parameter called payload , you can pass the path to the pdf file here. Add the plugin open_file in your pubspec.yaml. Then inside main.dart, add the following code

FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
    FlutterLocalNotificationsPlugin();

Future<void> initNotification() async {
// initialise the plugin. app_icon needs to be a added as a drawable resource to the Android head project
  const AndroidInitializationSettings initializationSettingsAndroid =
      AndroidInitializationSettings('notification_icon');
  final IOSInitializationSettings initializationSettingsIOS =
      IOSInitializationSettings();
  final MacOSInitializationSettings initializationSettingsMacOS =
      MacOSInitializationSettings();
  final InitializationSettings initializationSettings = InitializationSettings(
      android: initializationSettingsAndroid,
      iOS: initializationSettingsIOS,
      macOS: initializationSettingsMacOS);
  await flutterLocalNotificationsPlugin.initialize(initializationSettings,
      onSelectNotification: (String? payload) {
    if (payload != null) OpenFile.open(payload);
  });
}

Note: This function should not be inside any class.

Chinmay Kabi
  • 489
  • 4
  • 9
  • 1
    Thanks for your answer. I have a question, Can you tell me how can I show the download icon instead of the app icon in the notification? – user1595266 Jan 29 '22 at 10:18
  • 1
    Add the download icon to the drawable resource. Then pass the name of the file in initializationSettingsAndroid. As you can see here I have passed 'notification_icon', which shows app logo. – Chinmay Kabi Jan 31 '22 at 04:24
  • Thanks it's working good in android but not working in iOS.any suggestions – Taizul Islam Jun 05 '22 at 00:08
  • For showing notifications in iOS you will need to ask for permission to the user first, are you doing that? @TaizulIslam – Chinmay Kabi Jun 07 '22 at 10:12
  • but when tapping the notification when app terminated it opens the app again not. the file , how to handle this case – Ahmed Elhenidy Aug 15 '23 at 09:42