notification_util.dart 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import 'package:custom_notification/custom_notification.dart';
  2. import 'package:electronic_assistant/data/bean/talks.dart';
  3. import 'package:electronic_assistant/module/talk/view.dart';
  4. import 'package:flutter_local_notifications/flutter_local_notifications.dart';
  5. class NotificationUtil {
  6. NotificationUtil._();
  7. static String formatDuration(double seconds) {
  8. final duration = Duration(seconds: seconds.toInt());
  9. final hours = duration.inHours.toString().padLeft(2, '0');
  10. final minutes = (duration.inMinutes % 60).toString().padLeft(2, '0');
  11. final secs = (duration.inSeconds % 60).toString().padLeft(2, '0');
  12. return '$hours:$minutes:$secs';
  13. }
  14. static void showRecordNotification(
  15. int notificationId, bool isRecording, double recordDuration,
  16. {required String channelId, required String channelName}) {
  17. CustomNotification.showRecordNotification(
  18. notificationId, isRecording, formatDuration(recordDuration),
  19. channelId: channelId, channelName: channelName);
  20. }
  21. static int _analyseNotificationId = 2000;
  22. static int getAnalyseNotificationId() {
  23. return _analyseNotificationId++;
  24. }
  25. static void showAnalyseSuccessNotification(String talkId) async {
  26. final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
  27. FlutterLocalNotificationsPlugin();
  28. const AndroidInitializationSettings initializationSettingsAndroid =
  29. AndroidInitializationSettings('@mipmap/ic_launcher');
  30. const InitializationSettings initializationSettings =
  31. InitializationSettings(
  32. android: initializationSettingsAndroid,
  33. );
  34. await flutterLocalNotificationsPlugin.initialize(
  35. initializationSettings,
  36. onDidReceiveNotificationResponse:
  37. (NotificationResponse notificationResponse) async {
  38. if (notificationResponse.payload != null) {
  39. TalkPage.startById(notificationResponse.payload!);
  40. }
  41. },
  42. );
  43. const AndroidNotificationDetails androidPlatformChannelSpecifics =
  44. AndroidNotificationDetails(
  45. 'talk_analyse_success',
  46. '谈话分析通知',
  47. channelDescription: '谈话分析成功',
  48. importance: Importance.max,
  49. priority: Priority.high,
  50. );
  51. const NotificationDetails platformChannelSpecifics =
  52. NotificationDetails(android: androidPlatformChannelSpecifics);
  53. await flutterLocalNotificationsPlugin.show(
  54. getAnalyseNotificationId(),
  55. '小听谈话分析完成',
  56. '老板,谈话分析已完成,快点击查看吧。',
  57. platformChannelSpecifics,
  58. payload: talkId,
  59. );
  60. }
  61. // static void showAnalyseResultNotification(
  62. // int notificationId, bool isSuccess, String talkId,
  63. // {required String channelId, required String channelName}) {
  64. // CustomNotification.showAnalyseResultNotification(
  65. // notificationId, isSuccess, talkId,
  66. // channelId: channelId, channelName: channelName);
  67. // }
  68. }