Added home-screen app-icon Notification badge

This commit is contained in:
Elias Müller 2023-09-16 16:15:16 +02:00
parent c4dae8df8c
commit 248483be4e
9 changed files with 67 additions and 13 deletions

View File

@ -5,11 +5,11 @@ import 'package:crypto/crypto.dart';
import 'package:device_info_plus/device_info_plus.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:http/http.dart' as http;
import 'package:marianum_mobile/api/mhsl/server/userIndex/update/updateUserIndexParams.dart';
import 'package:package_info/package_info.dart';
import '../../../../../model/accountData.dart';
import '../../../mhslApi.dart';
import 'updateUserIndexParams.dart';
class UpdateUserIndex extends MhslApi<void> {
UpdateUserIndexParams params;

View File

@ -56,11 +56,13 @@ class _AppState extends State<App> {
NotifyUpdater.registerToServer();
});
}
FirebaseMessaging.onMessage.listen((message) => NotificationController.onForegroundMessageHandler(message, context));
FirebaseMessaging.onMessageOpenedApp.listen((message) => NotificationController.onForegroundMessageHandler(message, context));
FirebaseMessaging.onBackgroundMessage(NotificationController.onBackgroundMessageHandler);
FirebaseMessaging.onMessageOpenedApp.listen((message) => NotificationController.onAppOpenedByNotification(message, context));
FirebaseMessaging.instance.getInitialMessage().then((message) => message == null ? null : NotificationController.onAppOpenedByNotification(message, context));
super.initState();
}

View File

@ -1,4 +1,6 @@
import 'package:flutter_app_badger/flutter_app_badger.dart';
import '../../api/apiResponse.dart';
import '../../api/marianumcloud/talk/room/getRoomCache.dart';
import '../../api/marianumcloud/talk/room/getRoomResponse.dart';
@ -20,6 +22,7 @@ class ChatListProps extends DataHolder {
onUpdate: (GetRoomResponse data) => {
_getRoomResponse = data,
notifyListeners(),
FlutterAppBadger.updateBadgeCount(data.data.map((e) => e.unreadMessages).reduce((a, b) => a+b))
}
);
}

View File

@ -1,15 +1,16 @@
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter/cupertino.dart';
import 'package:provider/provider.dart';
import 'package:flutter/material.dart';
import '../model/chatList/chatListProps.dart';
import '../model/message/messageProps.dart';
import '../widget/debug/debugTile.dart';
import '../widget/debug/jsonViewer.dart';
import 'notificationTasks.dart';
class NotificationController {
@pragma('vm:entry-point')
static Future<void> onBackgroundMessageHandler(RemoteMessage message) async {
NotificationTasks.updateBadgeCount(message);
return; // Displaying the notification is curently done via the Firebase SDK itself. The Message is server-generated.
// log("Handling a background notification: ${message.messageId}");
//
// await Firebase.initializeApp();
// AccountData().waitForPopulation().then((value) {
@ -38,8 +39,24 @@ class NotificationController {
}
static Future<void> onForegroundMessageHandler(RemoteMessage message, BuildContext context) async {
//NotificationService().showToast(message: "Du hast eine neue Talk Nachricht!", context: context);
Provider.of<ChatListProps>(context, listen: false).run(renew: true);
Provider.of<MessageProps>(context, listen: false).run(renew: true);
NotificationTasks.updateProviders(context);
NotificationTasks.updateBadgeCount(message);
}
static Future<void> onAppOpenedByNotification(RemoteMessage message, BuildContext context) async {
NotificationTasks.updateProviders(context);
DebugTile(context).run(() {
showDialog(context: context, builder: (context) => AlertDialog(
title: const Text("Notification report"),
content: Column(
mainAxisSize: MainAxisSize.min,
children: [
const Text("Dieser Bericht wird angezeigt, da du den Entwicklermodus aktiviert hast und die App über eine Benachrichtigung geöffnet wurde."),
Text(JsonViewer.format(message.data)),
],
),
));
});
}
}

View File

@ -0,0 +1,22 @@
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter_app_badger/flutter_app_badger.dart';
import 'package:provider/provider.dart';
import '../model/chatList/chatListProps.dart';
import '../model/chatList/chatProps.dart';
class NotificationTasks {
static void updateBadgeCount(RemoteMessage notification) {
FlutterAppBadger.updateBadgeCount(int.parse(notification.data['unreadCount'] ?? 0));
}
static void updateProviders(BuildContext context) {
Provider.of<ChatListProps>(context, listen: false).run(renew: true);
Provider.of<ChatProps>(context, listen: false).run();
}
static void navigateToTalk() {
// TODO Navigate
}
}

View File

@ -80,7 +80,7 @@ class JoinChat extends SearchDelegate<String> {
}
);
} else if(snapshot.hasError) {
return PlaceholderView(icon: Icons.search_off, text: snapshot.error.toString());
return const PlaceholderView(icon: Icons.search_off, text: "Ein fehler ist aufgetreten. Bist du mit dem Internet verbunden?");
}
return const Center(child: CircularProgressIndicator());

View File

@ -11,6 +11,8 @@ class DebugTile {
bool onlyInDebug;
DebugTile(this.context, {this.onlyInDebug = false});
bool devConditionFulfilled() => Provider.of<SettingsProvider>(context, listen: false).val().devToolsEnabled && (onlyInDebug ? kDebugMode : true);
Widget jsonData(Map<String, dynamic> data, {bool ignoreConfig = false}) {
return callback(
title: "JSON daten anzeigen",
@ -31,8 +33,13 @@ class DebugTile {
Widget child(Widget child) {
return Visibility(
visible: Provider.of<SettingsProvider>(context).val().devToolsEnabled && (onlyInDebug ? kDebugMode : true),
visible: devConditionFulfilled(),
child: child,
);
}
void run(void Function() callback) {
if(!devConditionFulfilled()) return;
callback();
}
}

View File

@ -10,6 +10,7 @@ import fast_rsa
import file_selector_macos
import firebase_core
import firebase_messaging
import flutter_app_badger
import flutter_local_notifications
import package_info
import path_provider_foundation
@ -25,6 +26,7 @@ func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) {
FileSelectorPlugin.register(with: registry.registrar(forPlugin: "FileSelectorPlugin"))
FLTFirebaseCorePlugin.register(with: registry.registrar(forPlugin: "FLTFirebaseCorePlugin"))
FLTFirebaseMessagingPlugin.register(with: registry.registrar(forPlugin: "FLTFirebaseMessagingPlugin"))
FlutterAppBadgerPlugin.register(with: registry.registrar(forPlugin: "FlutterAppBadgerPlugin"))
FlutterLocalNotificationsPlugin.register(with: registry.registrar(forPlugin: "FlutterLocalNotificationsPlugin"))
FLTPackageInfoPlugin.register(with: registry.registrar(forPlugin: "FLTPackageInfoPlugin"))
PathProviderPlugin.register(with: registry.registrar(forPlugin: "PathProviderPlugin"))

View File

@ -91,6 +91,7 @@ dependencies:
flutter_split_view: ^0.1.2
bottom_sheet: ^4.0.0
device_info_plus: ^9.0.3
flutter_app_badger: ^1.5.0
dev_dependencies:
flutter_test: