fix: zachování zapnutí skrz flutter_background

This commit is contained in:
Matyáš Caras 2022-11-17 13:34:18 +01:00
parent 799887eae3
commit 1dcad3c716
10 changed files with 60 additions and 1 deletions

View file

@ -1,3 +1,5 @@
# 1.4.3
- Android: přidat permanentní oznámení
# 1.4.2 # 1.4.2
- aktualizace knihovny flutter_local_notifications - aktualizace knihovny flutter_local_notifications
- lepší podpora pro Android 13 - lepší podpora pro Android 13

View file

@ -1,6 +1,9 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android" <manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="cz.hernikplays.opencanteen"> package="cz.hernikplays.opencanteen">
<uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />
<application <application
android:label="OpenCanteen" android:label="OpenCanteen"
android:name="${applicationName}" android:name="${applicationName}"

View file

@ -168,4 +168,6 @@ abstract class Languages {
String get error; String get error;
String get needRemember; String get needRemember;
String get wakeLock;
} }

View file

@ -237,4 +237,8 @@ class LanguageCz extends Languages {
@override @override
String get review => "Ohodnotit aplikaci"; String get review => "Ohodnotit aplikaci";
@override
String get wakeLock =>
"Toto oznámení slouží k tomu, aby nebyl proces spontánně ukončen.";
} }

View file

@ -235,4 +235,8 @@ class LanguageEn extends Languages {
@override @override
String get review => "Review the app"; String get review => "Review the app";
@override
String get wakeLock =>
"This notification is used to hopefully block unexpected process kill.";
} }

View file

@ -47,12 +47,14 @@ void oznamitPredem(SharedPreferences prefs, tz.Location l) async {
String title; String title;
String locale = Intl.getCurrentLocale(); String locale = Intl.getCurrentLocale();
debugPrint(locale);
switch (locale) { switch (locale) {
case "cs_CZ": case "cs_CZ":
title = LanguageCz().lunchNotif; title = LanguageCz().lunchNotif;
break; break;
default: default:
title = LanguageEn().lunchNotif; title = LanguageEn().lunchNotif;
break;
} }
/*if (prefs.getBool("offline") ?? false) { /*if (prefs.getBool("offline") ?? false) {
@ -73,6 +75,7 @@ void oznamitPredem(SharedPreferences prefs, tz.Location l) async {
TimeOfDay(hour: kdy.hour, minute: kdy.minute), TimeOfDay(hour: kdy.hour, minute: kdy.minute),
); );
if (cas.isBefore(DateTime.now())) return; if (cas.isBefore(DateTime.now())) return;
// data o oznámení // data o oznámení
const AndroidNotificationDetails androidSpec = const AndroidNotificationDetails androidSpec =
AndroidNotificationDetails('predobedem', 'Oznámení před obědem', AndroidNotificationDetails('predobedem', 'Oznámení před obědem',
@ -94,6 +97,7 @@ void oznamitPredem(SharedPreferences prefs, tz.Location l) async {
UILocalNotificationDateInterpretation.absoluteTime); UILocalNotificationDateInterpretation.absoluteTime);
} on StateError catch (_) { } on StateError catch (_) {
// nenalezeno // nenalezeno
debugPrint("Nenalezeno");
} }
} }
// } // }

View file

@ -3,6 +3,7 @@ import 'dart:io';
import 'package:canteenlib/canteenlib.dart'; import 'package:canteenlib/canteenlib.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_background/flutter_background.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart'; import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:flutter_secure_storage/flutter_secure_storage.dart'; import 'package:flutter_secure_storage/flutter_secure_storage.dart';
import 'package:opencanteen/okna/nastaveni.dart'; import 'package:opencanteen/okna/nastaveni.dart';
@ -378,6 +379,22 @@ class _JidelnicekPageState extends State<JidelnicekPage> {
void nactiNastaveni() async { void nactiNastaveni() async {
var prefs = await SharedPreferences.getInstance(); var prefs = await SharedPreferences.getInstance();
_skipWeekend = prefs.getBool("skip") ?? false; _skipWeekend = prefs.getBool("skip") ?? false;
if (prefs.getBool("oznamit") ?? false) {
if (!mounted) return;
if (Platform.isAndroid) {
// ! TODO: OTESTOVAT, JESTLI FUNGUJE IMPORT NA IOSu
var androidConfig = FlutterBackgroundAndroidConfig(
notificationTitle: "OpenCanteen",
notificationText: Languages.of(context)!.wakeLock,
notificationImportance: AndroidNotificationImportance.Default,
notificationIcon:
const AndroidResource(name: 'notif_icon', defType: 'drawable'),
enableWifiLock: true);
bool success =
await FlutterBackground.initialize(androidConfig: androidConfig);
if (success) await FlutterBackground.enableBackgroundExecution();
}
}
if (!mounted) return; if (!mounted) return;
kontrolaTyden(context); kontrolaTyden(context);
} }

View file

@ -2,6 +2,7 @@ import 'dart:io';
import 'package:canteenlib/canteenlib.dart'; import 'package:canteenlib/canteenlib.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_background/flutter_background.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart'; import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:flutter_native_timezone/flutter_native_timezone.dart'; import 'package:flutter_native_timezone/flutter_native_timezone.dart';
import 'package:path_provider/path_provider.dart'; import 'package:path_provider/path_provider.dart';
@ -235,9 +236,23 @@ class _NastaveniState extends State<Nastaveni> {
var l = var l =
tz.getLocation(await FlutterNativeTimezone.getLocalTimezone()); tz.getLocation(await FlutterNativeTimezone.getLocalTimezone());
if (!mounted) return; if (!mounted) return;
if (Platform.isAndroid) {
// ! TODO: OTESTOVAT, JESTLI FUNGUJE IMPORT NA IOSu
var androidConfig = FlutterBackgroundAndroidConfig(
notificationTitle: "OpenCanteen",
notificationText: Languages.of(context)!.wakeLock,
notificationImportance: AndroidNotificationImportance.Default,
notificationIcon: const AndroidResource(
name: 'notif_icon', defType: 'drawable'),
enableWifiLock: true);
bool success = await FlutterBackground.initialize(
androidConfig: androidConfig);
if (success) await FlutterBackground.enableBackgroundExecution();
}
await widget.n.zonedSchedule( await widget.n.zonedSchedule(
// Vytvoří nové oznámení pro daný čas a datum // Vytvoří nové oznámení pro daný čas a datum
0, 0,
// ignore: use_build_context_synchronously
Languages.of(context)!.lunchNotif, Languages.of(context)!.lunchNotif,
"${jidlo.varianta} - ${jidlo.nazev}", "${jidlo.varianta} - ${jidlo.nazev}",
tz.TZDateTime.from(den, l), tz.TZDateTime.from(den, l),

View file

@ -111,6 +111,13 @@ packages:
description: flutter description: flutter
source: sdk source: sdk
version: "0.0.0" version: "0.0.0"
flutter_background:
dependency: "direct main"
description:
name: flutter_background
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.0"
flutter_launcher_icons: flutter_launcher_icons:
dependency: "direct dev" dependency: "direct dev"
description: description:

View file

@ -6,7 +6,7 @@ publish_to: 'none'
# The following defines the version and build number for your application. # The following defines the version and build number for your application.
# A version number is three numbers separated by dots, like 1.2.43 # A version number is three numbers separated by dots, like 1.2.43
# followed by an optional build number separated by a +. # followed by an optional build number separated by a +.
version: 1.4.2+22 version: 1.4.3+23
environment: environment:
sdk: ">=2.16.1 <3.0.0" sdk: ">=2.16.1 <3.0.0"
@ -27,6 +27,7 @@ dependencies:
flutter_native_timezone: ^2.0.0 flutter_native_timezone: ^2.0.0
intl: ^0.17.0 intl: ^0.17.0
package_info_plus: ^1.4.3+1 package_info_plus: ^1.4.3+1
flutter_background: ^1.1.0
dev_dependencies: dev_dependencies:
flutter_lints: ^2.0.1 flutter_lints: ^2.0.1