feat(notifikace): přenést background notifikaci do main.dart
+ přidat autostart
This commit is contained in:
parent
5583dec569
commit
2d2faf5a59
5 changed files with 66 additions and 25 deletions
|
@ -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
|
||||
|
|
|
@ -38,5 +38,18 @@
|
|||
<meta-data
|
||||
android:name="flutterEmbedding"
|
||||
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>
|
||||
</manifest>
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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());
|
||||
}
|
||||
|
|
|
@ -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<JidelnicekPage> {
|
|||
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);
|
||||
}
|
||||
|
|
Reference in a new issue