Untitled

 avatar
unknown
plain_text
5 months ago
2.3 kB
4
Indexable
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:nb_utils/nb_utils.dart';
import 'package:open_file/open_file.dart';

class NotificationService {
  static final FlutterLocalNotificationsPlugin _notificationsPlugin = FlutterLocalNotificationsPlugin();

  static Future<void> initialize() async {
    const AndroidInitializationSettings androidInitializationSettings = AndroidInitializationSettings(
      '@mipmap/ic_launcher',
    );

    const InitializationSettings settings = InitializationSettings(
      android: androidInitializationSettings,
    );

    await _notificationsPlugin.initialize(
      settings,
      onDidReceiveNotificationResponse: (details) async {
        log("payload" + " ======> " + "${details.payload}");

        if (details.payload != null) {
          try {
            var res = await OpenFile.open(
              details.payload, /*isIOSAppOpen: true*/
            );
            log("res" + " ======> " + "${res}");
          } on Exception catch (e) {
            toast(e.toString());
          }
        }
      },
    );
  }

  static Future<void> showProgressNotification({
    required int id,
    required String title,
    required String body,
    required int progress,
    required int maxProgress,
    String? filePath,
  }) async {
    AndroidNotificationDetails androidPlatformChannelSpecifics = AndroidNotificationDetails(
      'download_channel',
      'Download',
      channelDescription: 'This channel is used for download notifications',
      importance: Importance.max,
      priority: Priority.high,
      onlyAlertOnce: false,
      channelAction: AndroidNotificationChannelAction.createIfNotExists,
      showProgress: true,
      progress: progress,
      maxProgress: maxProgress,
    );

    NotificationDetails platformChannelSpecifics = NotificationDetails(android: androidPlatformChannelSpecifics);

    await _notificationsPlugin.show(
      id,
      title,
      body,
      platformChannelSpecifics,
      payload: filePath ?? 'progress',
    );

    /*  await _notificationsPlugin.show(
      id,
      title,
      body,
      platformChannelSpecifics,
    );*/
  }

  static Future<void> cancelNotification(int id) async {
    await _notificationsPlugin.cancel(id);
  }
}
Editor is loading...
Leave a Comment