Skip to content
Snippets Groups Projects
Verified Commit c2999411 authored by Lukas Schreiner's avatar Lukas Schreiner
Browse files

Log messages

Fixes:
 - Log messages for push notifications got increased with each trigger
   Removed the duplicate configuration.
 - Push notification processing improved to ensure, things are not
   running in parallel.
parent 0bb02fb5
Branches
Tags
No related merge requests found
Pipeline #2300 waiting for manual action
......@@ -155,11 +155,11 @@ class _FlsVplanAppState extends State<FlsVplanApp> {
bool _notificationsEnabled = false;
@override
@override
void initState() {
super.initState();
final log = getVPlanLogger();
BackgroundPush.initialize();
BackgroundPush.getInstance().initialize();
_isAndroidPermissionGranted();
try {
......
......@@ -165,8 +165,7 @@ class Config extends ChangeNotifier {
/// Update configuration and set plan type.
Future<void> setModeString(String planType) async {
setMode(
planType == configPlanTeacher ? PlanType.teacher : PlanType.pupil);
setMode(planType == configPlanTeacher ? PlanType.teacher : PlanType.pupil);
}
/// Get base school vplan endpoint based on school configuration from
......@@ -239,7 +238,7 @@ class Config extends ChangeNotifier {
throw SchoolNotFoundException("School $school not found!");
}
if (_schoolName != school) {
BackgroundPush.unregister(_school.notifyInstance);
BackgroundPush.getInstance().unregister(_school.notifyInstance);
}
_schoolName = school;
return _storage
......@@ -305,7 +304,8 @@ class Config extends ChangeNotifier {
}
} catch (e) {
final log = Logger(vplanLoggerId);
log.severe("Config: Failed loading teacher permission -- fallback to false!");
log.severe(
"Config: Failed loading teacher permission -- fallback to false!");
}
return false;
}
......
......@@ -26,8 +26,8 @@ class _FlsVplanMainUiState extends State<FlsVplanMainUi> with TickerProviderStat
}
@override
Widget build(BuildContext context) {
BackgroundPush.getInstance().setupPush();
BackgroundPush.setupPush();
return Scaffold(
backgroundColor: PlanColors.AppBackgroundColor,
body: SafeArea(
......
......@@ -6,27 +6,53 @@ import 'package:de_fls_wiesbaden_vplan/ui/helper/consts.dart';
import 'package:de_fls_wiesbaden_vplan/storage/config.dart';
import 'package:de_fls_wiesbaden_vplan/utils/logger.dart';
import 'package:flutter/foundation.dart';
import 'package:mutex/mutex.dart';
import 'package:unifiedpush/unifiedpush.dart';
class BackgroundPush {
static BackgroundPush? _instance;
bool _isRegistered = false;
late Mutex processMessage;
BackgroundPush({bool overwrite = false}) {
// reset instance if requested.
if (_instance != null && overwrite) {
_instance = null;
}
processMessage = Mutex();
_instance ??= this;
}
static BackgroundPush getInstance() {
if (_instance == null) {
BackgroundPush();
}
return _instance!;
}
static void initialize() {
void initialize() async {
final log = getVPlanLogger();
if (!kIsWeb && (Platform.isAndroid || Platform.isIOS)) {
log.info("BackgroundPush is initializing...");
UnifiedPush.initialize(
await UnifiedPush.initialize(
onNewEndpoint:
onNewEndpoint, // takes (String endpoint, String instance) in args
onRegistrationFailed: onRegistrationFailed, // takes (String instance)
onUnregistered: onUnregistered, // takes (String instance)
onMessage: pushNotifyReceived, // takes (String message, String instance) in args
onMessage:
pushNotifyReceived, // takes (String message, String instance) in args
);
} else {
log.info("BackgroundPush disabled as its not supported on this platform...");
log.info(
"BackgroundPush disabled as its not supported on this platform...");
}
}
static Future<void> setupPush() async {
Future<void> setupPush() async {
if (_isRegistered) {
return;
}
_isRegistered = true;
final log = getVPlanLogger();
if (kIsWeb || (!Platform.isAndroid && !Platform.isIOS)) {
return;
......@@ -42,18 +68,17 @@ class BackgroundPush {
return;
}*/
if (!Platform.isIOS &&
(await UnifiedPush.getDistributors()).isNotEmpty) {
setupUp();
if (!Platform.isIOS && (await UnifiedPush.getDistributors()).isNotEmpty) {
await setupUp();
} else {
//setupFirebase();
}
}
static Future<void> setupUp() async {
Future<void> setupUp() async {
final log = getVPlanLogger();
final distributors = await UnifiedPush.getDistributors([featureAndroidBytesMessage]);
final distributors =
await UnifiedPush.getDistributors([featureAndroidBytesMessage]);
String distributor = distributors.first;
for (var element in distributors) {
log.finer("Found notification distributor: $element");
......@@ -70,17 +95,18 @@ class BackgroundPush {
UnifiedPush.registerApp(instance, [featureAndroidBytesMessage]);
}
static Future<void> unregister(String? instance) async {
Future<void> unregister(String? instance) async {
Config cfg = Config.getInstance();
if (cfg.notifyRegistered) {
await UnifiedPush.unregister(instance ?? vplanNotifyInstance);
}
}
static void onNewEndpoint(String endpoint, String instance) {
void onNewEndpoint(String endpoint, String instance) {
Config cfg = Config.getInstance();
final log = getVPlanLogger();
log.fine("Got new endpoint for instance $instance (waiting: $vplanNotifyInstance)");
log.fine(
"Got new endpoint for instance $instance (waiting: $vplanNotifyInstance)");
if (instance != cfg.getSchoolObj().notifyInstance) {
return;
......@@ -90,13 +116,14 @@ class BackgroundPush {
log.info("Notification endpoint is $endpoint");
}
static void onRegistrationFailed(String instance) {
void onRegistrationFailed(String instance) {
onUnregistered(instance);
}
static void onUnregistered(String instance) {
void onUnregistered(String instance) {
final log = getVPlanLogger();
log.fine("Got new endpoint for instance $instance (waiting: $vplanNotifyInstance)");
log.fine(
"Got new endpoint for instance $instance (waiting: $vplanNotifyInstance)");
if (instance != vplanNotifyInstance) {
return;
......@@ -106,26 +133,33 @@ class BackgroundPush {
log.info("Notification registration is disabled.");
}
static Future<bool> pushNotifyReceived(Uint8List message, String instance) async {
configureVPlanLogger();
Future<void> pushNotifyReceived(Uint8List message, String instance) async {
processMessage.protect(() => processNotification(message, instance));
}
Future<bool> processNotification(Uint8List message, String instance) async {
final log = getVPlanLogger();
log.finer("Cloud message received: ${utf8.decode(message)}");
log.finer("Cloud message processing: ${utf8.decode(message)}");
final notificationBasics = Map<String, dynamic>.from(
json.decode(utf8.decode(message)),
);
final notificationObj = notificationBasics.containsKey('gcm.notification.body') ? json.decode(notificationBasics['gcm.notification.body']) : notificationBasics;
final notificationObj =
notificationBasics.containsKey('gcm.notification.body')
? json.decode(notificationBasics['gcm.notification.body'])
: notificationBasics;
final Map<String, dynamic> data = notificationObj['notification'];
Config cfg = Config.getInstance();
final endpoint = cfg.getSchoolObj().endpoint;
if (!data.containsKey("endpoint") || data["endpoint"] != endpoint) {
log.info("Ignore push notification: wrong endpoint ${data["endpoint"]}. Expected: $endpoint");
return Future.value(true);
log.info(
"Ignore push notification: wrong endpoint ${data["endpoint"]}. Expected: $endpoint");
return true;
}
log.fine("Got push notification for $instance: $data");
PlanStorage().refresh();
await PlanStorage().refresh();
return Future.value(true);
return true;
}
}
\ No newline at end of file
}
......@@ -181,10 +181,10 @@ packages:
dependency: transitive
description:
name: cross_file
sha256: "55d7b444feb71301ef6b8838dbc1ae02e63dd48c8773f3810ff53bb1e2945b32"
sha256: fedaadfa3a6996f75211d835aaeb8fede285dae94262485698afd832371b9a5e
url: "https://pub.dev"
source: hosted
version: "0.3.4+1"
version: "0.3.3+8"
crypto:
dependency: transitive
description:
......@@ -401,10 +401,10 @@ packages:
dependency: "direct main"
description:
name: http
sha256: "761a297c042deedc1ffbb156d6e2af13886bb305c2a343a4d972504cd67dd938"
sha256: a2bbf9d017fcced29139daa8ed2bba4ece450ab222871df93ca9eec6f80c34ba
url: "https://pub.dev"
source: hosted
version: "1.2.1"
version: "1.2.0"
http_multi_server:
dependency: transitive
description:
......@@ -565,6 +565,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "5.4.4"
mutex:
dependency: "direct main"
description:
name: mutex
sha256: "8827da25de792088eb33e572115a5eb0d61d61a3c01acbc8bcbe76ed78f1a1f2"
url: "https://pub.dev"
source: hosted
version: "3.1.0"
nested:
dependency: transitive
description:
......@@ -585,10 +593,10 @@ packages:
dependency: "direct main"
description:
name: package_info_plus
sha256: "7e76fad405b3e4016cd39d08f455a4eb5199723cf594cd1b8916d47140d93017"
sha256: "88bc797f44a94814f2213db1c9bd5badebafdfb8290ca9f78d4b9ee2a3db4d79"
url: "https://pub.dev"
source: hosted
version: "4.2.0"
version: "5.0.1"
package_info_plus_platform_interface:
dependency: transitive
description:
......@@ -777,10 +785,10 @@ packages:
dependency: transitive
description:
name: shared_preferences_web
sha256: "9aee1089b36bd2aafe06582b7d7817fd317ef05fc30e6ba14bff247d0933042a"
sha256: "7b15ffb9387ea3e237bb7a66b8a23d2147663d391cafc5c8f37b2e7b4bde5d21"
url: "https://pub.dev"
source: hosted
version: "2.3.0"
version: "2.2.2"
shared_preferences_windows:
dependency: transitive
description:
......@@ -982,10 +990,10 @@ packages:
dependency: transitive
description:
name: url_launcher_web
sha256: "3692a459204a33e04bc94f5fb91158faf4f2c8903281ddd82915adecdb1a901d"
sha256: fff0932192afeedf63cdd50ecbb1bc825d31aed259f02bb8dba0f3b729a5e88b
url: "https://pub.dev"
source: hosted
version: "2.3.0"
version: "2.2.3"
url_launcher_windows:
dependency: transitive
description:
......@@ -1054,18 +1062,18 @@ packages:
dependency: transitive
description:
name: web
sha256: "1d9158c616048c38f712a6646e317a3426da10e884447626167240d45209cbad"
sha256: "4188706108906f002b3a293509234588823c8c979dc83304e229ff400c996b05"
url: "https://pub.dev"
source: hosted
version: "0.5.0"
version: "0.4.2"
web_socket_channel:
dependency: transitive
description:
name: web_socket_channel
sha256: "1d8e795e2a8b3730c41b8a98a2dff2e0fb57ae6f0764a1c46ec5915387d257b2"
sha256: "939ab60734a4f8fa95feacb55804fa278de28bdeef38e616dc08e44a84adea23"
url: "https://pub.dev"
source: hosted
version: "2.4.4"
version: "2.4.3"
win32:
dependency: transitive
description:
......@@ -1107,5 +1115,5 @@ packages:
source: hosted
version: "3.1.2"
sdks:
dart: ">=3.3.0 <4.0.0"
flutter: ">=3.19.0"
dart: ">=3.3.0-279.1.beta <4.0.0"
flutter: ">=3.16.6"
......@@ -18,7 +18,7 @@ publish_to: 'none' # Remove this line if you wish to publish to pub.dev
# https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html
# In Windows, build-name is used as the major, minor, and patch parts
# of the product and file versions while build-number is used as the build suffix.
version: 3.0.9+3000905
version: 3.0.10+3001001
environment:
sdk: '>=2.19.4 <3.0.0'
......@@ -58,16 +58,17 @@ dependencies:
# - https://github.com/juliuscanute/qr_code_scanner/issues/719
# That means for now, when using mobile_scanner, we really have
# a problem to be available in projects like F-Droid.
mobile_scanner: ^3.5.0 # > anti-feature (uses ml-kit)
mobile_scanner: ^3.5.0 # > anti-feature (uses ml-kit); version >= 4 requires java 17
#qr_code_scanner: ^1.0.1 # maintenance mode; has some ugly bugs.
logging: ^1.1.1
package_info_plus: ^4.0.2
package_info_plus: ^5.0.1
url_launcher: ^6.1.10
unifiedpush: ^5.0.0
flutter_localizations:
sdk: flutter
jwt_decoder: ^2.0.1
auto_route: ^7.8.4
mutex: ^3.1.0
dev_dependencies:
flutter_test:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment