NotificationProvider.dart 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter_local_notifications/flutter_local_notifications.dart';
  3. import 'package:flutter_habit/common/I18N.dart';
  4. import 'package:flutter_habit/common/LocalData.dart';
  5. import 'package:flutter_habit/provider/ConfigProvider.dart';
  6. import 'package:shared_preferences/shared_preferences.dart';
  7. class NotificationProvider extends ChangeNotifier {
  8. FlutterLocalNotificationsPlugin? flutterLocalNotificationsPlugin;
  9. late bool getUpNotification;
  10. late bool breakfastNotification;
  11. late bool lunchNotification;
  12. late bool midRestNotification;
  13. late bool dinnerNotification;
  14. late bool restNotification;
  15. Future<void> init() async {
  16. await initFlutterLocalNotificationsPlugin();
  17. load();
  18. store();
  19. await startScheduledTasks();
  20. }
  21. Future<void> initFlutterLocalNotificationsPlugin() async {
  22. if (flutterLocalNotificationsPlugin == null) {
  23. flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();
  24. // initialise the plugin. app_icon needs to be a added as a drawable resource to the Android head project
  25. AndroidInitializationSettings initializationSettingsAndroid =
  26. AndroidInitializationSettings("logo");
  27. DarwinInitializationSettings initializationSettingsIOS =
  28. DarwinInitializationSettings();
  29. InitializationSettings initializationSettings = InitializationSettings(
  30. android: initializationSettingsAndroid,
  31. iOS: initializationSettingsIOS);
  32. flutterLocalNotificationsPlugin!.initialize(initializationSettings);
  33. debugPrint("init NotificationsUtils");
  34. }
  35. }
  36. Future<void> cancelScheduledTasks() async {
  37. await cancel(1);
  38. await cancel(2);
  39. await cancel(3);
  40. await cancel(4);
  41. await cancel(5);
  42. await cancel(6);
  43. debugPrint("cancelScheduledTasks");
  44. }
  45. Future<List<PendingNotificationRequest>> getNotifications() async {
  46. List pendingNotificationRequests =
  47. await flutterLocalNotificationsPlugin!.pendingNotificationRequests();
  48. return pendingNotificationRequests as FutureOr<List<PendingNotificationRequest>>;
  49. }
  50. Future<void> startScheduledTasks() async {
  51. await cancelScheduledTasks();
  52. ConfigProvider configProvider = ConfigProvider();
  53. configProvider.load();
  54. if (getUpNotification) {
  55. DateTime dateTime =
  56. DateTime.fromMillisecondsSinceEpoch(configProvider.getUpTimeStart);
  57. await scheduledNotice(
  58. 1,
  59. "Habit ${I18N.of("打卡提醒")}",
  60. I18N.of("起床打卡开始了"),
  61. dateTime.hour,
  62. dateTime.minute,
  63. dateTime.second,
  64. );
  65. }
  66. if (breakfastNotification) {
  67. DateTime dateTime = DateTime.fromMillisecondsSinceEpoch(
  68. configProvider.breakfastTimeStart);
  69. await scheduledNotice(
  70. 2,
  71. "Habit ${I18N.of("打卡提醒")}",
  72. I18N.of("早饭打卡开始了"),
  73. dateTime.hour,
  74. dateTime.minute,
  75. dateTime.second,
  76. );
  77. }
  78. if (lunchNotification) {
  79. DateTime dateTime =
  80. DateTime.fromMillisecondsSinceEpoch(configProvider.lunchTimeStart);
  81. await scheduledNotice(
  82. 3,
  83. "Habit ${I18N.of("打卡提醒")}",
  84. I18N.of("午饭打卡开始了"),
  85. dateTime.hour,
  86. dateTime.minute,
  87. dateTime.second,
  88. );
  89. }
  90. if (midRestNotification) {
  91. DateTime dateTime =
  92. DateTime.fromMillisecondsSinceEpoch(configProvider.midRestTimeStart);
  93. await scheduledNotice(
  94. 4,
  95. "Habit ${I18N.of("打卡提醒")}",
  96. I18N.of("午休打卡开始了"),
  97. dateTime.hour,
  98. dateTime.minute,
  99. dateTime.second,
  100. );
  101. }
  102. if (dinnerNotification) {
  103. DateTime dateTime =
  104. DateTime.fromMillisecondsSinceEpoch(configProvider.dinnerTimeStart);
  105. await scheduledNotice(
  106. 5,
  107. "Habit ${I18N.of("打卡提醒")}",
  108. I18N.of("起床打卡开始了"),
  109. dateTime.hour,
  110. dateTime.minute,
  111. dateTime.second,
  112. );
  113. }
  114. if (restNotification) {
  115. DateTime dateTime =
  116. DateTime.fromMillisecondsSinceEpoch(configProvider.restTimeStart);
  117. await scheduledNotice(
  118. 6,
  119. "Habit ${I18N.of("打卡提醒")}",
  120. I18N.of("睡觉打卡开始了"),
  121. dateTime.hour,
  122. dateTime.minute,
  123. dateTime.second,
  124. );
  125. }
  126. debugPrint("startScheduledTasks");
  127. }
  128. Future<void> scheduledNotice(int id, String title, String body, int hour,
  129. int minute, int second) async {
  130. // Time time = Time(hour, minute, second);
  131. AndroidNotificationDetails androidPlatformChannelSpecifics =
  132. AndroidNotificationDetails(id.toString(), title,
  133. channelDescription: body);
  134. DarwinNotificationDetails iOSPlatformChannelSpecifics =
  135. DarwinNotificationDetails();
  136. NotificationDetails platformChannelSpecifics = NotificationDetails(
  137. android: androidPlatformChannelSpecifics,
  138. iOS: iOSPlatformChannelSpecifics);
  139. await flutterLocalNotificationsPlugin!.show(
  140. id, title, body, platformChannelSpecifics);
  141. debugPrint("scheduledNotice $hour $minute $second");
  142. }
  143. Future<void> notice(int id, String title, String body) async {
  144. var androidPlatformChannelSpecifics = AndroidNotificationDetails(
  145. id.toString(), title,
  146. channelDescription: body,
  147. importance: Importance.max,
  148. priority: Priority.high,
  149. ticker: title);
  150. var iOSPlatformChannelSpecifics = DarwinNotificationDetails();
  151. var platformChannelSpecifics = NotificationDetails(
  152. android: androidPlatformChannelSpecifics,
  153. iOS: iOSPlatformChannelSpecifics);
  154. await flutterLocalNotificationsPlugin!.show(
  155. id, title, body, platformChannelSpecifics);
  156. }
  157. Future<void> cancel(int id) async {
  158. await flutterLocalNotificationsPlugin!.cancel(id);
  159. }
  160. void load() {
  161. SharedPreferences sp = LocalData.getInstance()!;
  162. getUpNotification = sp.getBool("getUpNotification") ?? true;
  163. breakfastNotification = sp.getBool("breakfastNotification") ?? true;
  164. lunchNotification = sp.getBool("lunchNotification") ?? true;
  165. midRestNotification = sp.getBool("midRestNotification") ?? true;
  166. dinnerNotification = sp.getBool("dinnerNotification") ?? true;
  167. restNotification = sp.getBool("restNotification") ?? true;
  168. }
  169. void store() {
  170. SharedPreferences sp = LocalData.getInstance()!;
  171. sp.setBool("getUpNotification", getUpNotification);
  172. sp.setBool("breakfastNotification", breakfastNotification);
  173. sp.setBool("lunchNotification", lunchNotification);
  174. sp.setBool("midRestNotification", midRestNotification);
  175. sp.setBool("dinnerNotification", dinnerNotification);
  176. sp.setBool("restNotification", restNotification);
  177. }
  178. Future<void> refresh() async {
  179. store();
  180. await startScheduledTasks();
  181. notifyListeners();
  182. }
  183. }