From 2d2faf5a59b0337c78adf08bea41be07b7af6b93 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maty=C3=A1=C5=A1=20Caras?= Date: Mon, 21 Nov 2022 17:23:49 +0100 Subject: [PATCH] =?UTF-8?q?feat(notifikace):=20p=C5=99en=C3=A9st=20backgro?= =?UTF-8?q?und=20notifikaci=20do=20main.dart?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit + přidat autostart --- CHANGELOG.md | 5 +-- android/app/src/main/AndroidManifest.xml | 13 ++++++++ .../hernikplays/opencanteen/MainActivity.kt | 10 ++++++ lib/main.dart | 30 +++++++++++++---- lib/okna/jidelnicek.dart | 33 +++++++++---------- 5 files changed, 66 insertions(+), 25 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fe3e723..ccfe023 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,6 @@ -# 1.4.3 -- Android: přidat permanentní oznámení +# 1.5.0 +- Android: přidat permanentní oznámení pro zabránění ukončení procesu +- přejít na flutter wrapper # 1.4.2 - aktualizace knihovny flutter_local_notifications - lepší podpora pro Android 13 diff --git a/android/app/src/main/AndroidManifest.xml b/android/app/src/main/AndroidManifest.xml index 5373cee..81c17fb 100644 --- a/android/app/src/main/AndroidManifest.xml +++ b/android/app/src/main/AndroidManifest.xml @@ -38,5 +38,18 @@ + + + + + + + + + diff --git a/android/app/src/main/kotlin/cz/hernikplays/opencanteen/MainActivity.kt b/android/app/src/main/kotlin/cz/hernikplays/opencanteen/MainActivity.kt index 4ec1b56..e00dbda 100644 --- a/android/app/src/main/kotlin/cz/hernikplays/opencanteen/MainActivity.kt +++ b/android/app/src/main/kotlin/cz/hernikplays/opencanteen/MainActivity.kt @@ -4,3 +4,13 @@ import io.flutter.embedding.android.FlutterActivity class MainActivity: FlutterActivity() { } + +class BootReceiver: BroadcastReceiver() { + override fun onReceive(context: Context, intent: Intent) { + if (intent.action == Intent.ACTION_BOOT_COMPLETED) { + val i = Intent(context, MainActivity::class.java) + i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) + context.startActivity(i) + } + } +} \ No newline at end of file diff --git a/lib/main.dart b/lib/main.dart index e6d496c..2f3b6ae 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -3,6 +3,7 @@ import 'dart:io'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; +import 'package:flutter_background/flutter_background.dart'; import 'package:flutter_local_notifications/flutter_local_notifications.dart'; import 'package:flutter_localizations/flutter_localizations.dart'; import 'package:flutter_native_timezone/flutter_native_timezone.dart'; @@ -45,14 +46,16 @@ final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin = void oznamitPredem(SharedPreferences prefs, tz.Location l) async { String title; - + String notif; String locale = Intl.getCurrentLocale(); debugPrint(locale); switch (locale) { case "cs_CZ": title = LanguageCz().lunchNotif; + notif = LanguageCz().wakeLock; break; default: + notif = LanguageEn().wakeLock; title = LanguageEn().lunchNotif; break; } @@ -85,6 +88,21 @@ void oznamitPredem(SharedPreferences prefs, tz.Location l) async { styleInformation: BigTextStyleInformation(''), ticker: 'today meal'); + // blokovat vypnutí + if (Platform.isAndroid) { + // ! TODO: OTESTOVAT, JESTLI FUNGUJE IMPORT NA IOSu + var androidConfig = FlutterBackgroundAndroidConfig( + notificationTitle: "OpenCanteen", + notificationText: notif, + 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(); + } + // naplánovat await flutterLocalNotificationsPlugin.zonedSchedule( 0, @@ -110,11 +128,6 @@ void main() async { var l = tz.getLocation(await FlutterNativeTimezone.getLocalTimezone()); tz.setLocalLocation(l); - var prefs = await SharedPreferences.getInstance(); - if (prefs.getBool("oznamit") ?? false) { - oznamitPredem(prefs, l); - } - // nastavit oznámení const AndroidInitializationSettings initializationSettingsAndroid = AndroidInitializationSettings('notif_icon'); @@ -124,6 +137,11 @@ void main() async { ); await flutterLocalNotificationsPlugin.initialize(initializationSettings); + var prefs = await SharedPreferences.getInstance(); + if (prefs.getBool("oznamit") ?? false) { + oznamitPredem(prefs, l); + } + // spustit aplikaci runApp(const MyApp()); } diff --git a/lib/okna/jidelnicek.dart b/lib/okna/jidelnicek.dart index 556510d..4365f7f 100644 --- a/lib/okna/jidelnicek.dart +++ b/lib/okna/jidelnicek.dart @@ -3,7 +3,6 @@ import 'dart:io'; import 'package:canteenlib/canteenlib.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_secure_storage/flutter_secure_storage.dart'; import 'package:opencanteen/okna/nastaveni.dart'; @@ -379,22 +378,22 @@ class _JidelnicekPageState extends State { void nactiNastaveni() async { var prefs = await SharedPreferences.getInstance(); _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 (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; kontrolaTyden(context); }