feat(notifikace): přenést background notifikaci do main.dart

+ přidat autostart
This commit is contained in:
Matyáš Caras 2022-11-21 17:23:49 +01:00
parent 5583dec569
commit 2d2faf5a59
5 changed files with 66 additions and 25 deletions

View file

@ -1,5 +1,6 @@
# 1.4.3 # 1.5.0
- Android: přidat permanentní oznámení - Android: přidat permanentní oznámení pro zabránění ukončení procesu
- přejít na flutter wrapper
# 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

@ -38,5 +38,18 @@
<meta-data <meta-data
android:name="flutterEmbedding" android:name="flutterEmbedding"
android:value="2" /> android:value="2" />
<!-- Autostart -->
<receiver
android:enabled="true"
android:exported="true"
android:name="cz.hernikplays.opencanteen.BootReceiver"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
</application> </application>
</manifest> </manifest>

View file

@ -4,3 +4,13 @@ import io.flutter.embedding.android.FlutterActivity
class MainActivity: 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)
}
}
}

View file

@ -3,6 +3,7 @@ import 'dart:io';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter/services.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_local_notifications/flutter_local_notifications.dart';
import 'package:flutter_localizations/flutter_localizations.dart'; import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_native_timezone/flutter_native_timezone.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 { void oznamitPredem(SharedPreferences prefs, tz.Location l) async {
String title; String title;
String notif;
String locale = Intl.getCurrentLocale(); String locale = Intl.getCurrentLocale();
debugPrint(locale); debugPrint(locale);
switch (locale) { switch (locale) {
case "cs_CZ": case "cs_CZ":
title = LanguageCz().lunchNotif; title = LanguageCz().lunchNotif;
notif = LanguageCz().wakeLock;
break; break;
default: default:
notif = LanguageEn().wakeLock;
title = LanguageEn().lunchNotif; title = LanguageEn().lunchNotif;
break; break;
} }
@ -85,6 +88,21 @@ void oznamitPredem(SharedPreferences prefs, tz.Location l) async {
styleInformation: BigTextStyleInformation(''), styleInformation: BigTextStyleInformation(''),
ticker: 'today meal'); 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 // naplánovat
await flutterLocalNotificationsPlugin.zonedSchedule( await flutterLocalNotificationsPlugin.zonedSchedule(
0, 0,
@ -110,11 +128,6 @@ void main() async {
var l = tz.getLocation(await FlutterNativeTimezone.getLocalTimezone()); var l = tz.getLocation(await FlutterNativeTimezone.getLocalTimezone());
tz.setLocalLocation(l); tz.setLocalLocation(l);
var prefs = await SharedPreferences.getInstance();
if (prefs.getBool("oznamit") ?? false) {
oznamitPredem(prefs, l);
}
// nastavit oznámení // nastavit oznámení
const AndroidInitializationSettings initializationSettingsAndroid = const AndroidInitializationSettings initializationSettingsAndroid =
AndroidInitializationSettings('notif_icon'); AndroidInitializationSettings('notif_icon');
@ -124,6 +137,11 @@ void main() async {
); );
await flutterLocalNotificationsPlugin.initialize(initializationSettings); await flutterLocalNotificationsPlugin.initialize(initializationSettings);
var prefs = await SharedPreferences.getInstance();
if (prefs.getBool("oznamit") ?? false) {
oznamitPredem(prefs, l);
}
// spustit aplikaci // spustit aplikaci
runApp(const MyApp()); runApp(const MyApp());
} }

View file

@ -3,7 +3,6 @@ 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';
@ -379,22 +378,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 (prefs.getBool("oznamit") ?? false) {
if (!mounted) return; // if (!mounted) return;
if (Platform.isAndroid) { // if (Platform.isAndroid) {
// ! TODO: OTESTOVAT, JESTLI FUNGUJE IMPORT NA IOSu // // ! TODO: OTESTOVAT, JESTLI FUNGUJE IMPORT NA IOSu
var androidConfig = FlutterBackgroundAndroidConfig( // var androidConfig = FlutterBackgroundAndroidConfig(
notificationTitle: "OpenCanteen", // notificationTitle: "OpenCanteen",
notificationText: Languages.of(context)!.wakeLock, // notificationText: Languages.of(context)!.wakeLock,
notificationImportance: AndroidNotificationImportance.Default, // notificationImportance: AndroidNotificationImportance.Default,
notificationIcon: // notificationIcon:
const AndroidResource(name: 'notif_icon', defType: 'drawable'), // const AndroidResource(name: 'notif_icon', defType: 'drawable'),
enableWifiLock: true); // enableWifiLock: true);
bool success = // bool success =
await FlutterBackground.initialize(androidConfig: androidConfig); // await FlutterBackground.initialize(androidConfig: androidConfig);
if (success) await FlutterBackground.enableBackgroundExecution(); // if (success) await FlutterBackground.enableBackgroundExecution();
} // }
} // }
if (!mounted) return; if (!mounted) return;
kontrolaTyden(context); kontrolaTyden(context);
} }