Moved notification handling to serverside

This commit is contained in:
2023-09-08 20:54:01 +02:00
parent 5f0426aab9
commit 6816e77d21
5 changed files with 77 additions and 86 deletions

View File

@ -1,46 +1,45 @@
import 'dart:developer';
import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter/cupertino.dart';
import 'package:provider/provider.dart';
import '../api/marianumcloud/talk/room/getRoom.dart';
import '../api/marianumcloud/talk/room/getRoomParams.dart';
import '../model/accountData.dart';
import 'notificationService.dart';
import '../model/chatList/chatListProps.dart';
import '../model/message/messageProps.dart';
class NotificationController {
@pragma('vm:entry-point')
static Future<void> onBackgroundMessageHandler(RemoteMessage message) async {
log("Handling a background notification: ${message.messageId}");
await Firebase.initializeApp();
AccountData().waitForPopulation().then((value) {
log("User account status: $value");
if(value) {
GetRoom(
GetRoomParams(
includeStatus: false,
),
).run().then((value) {
var messageCount = value.data.map((e) => e.unreadMessages).reduce((a, b) => a + b);
var chatCount = value.data.where((e) => e.unreadMessages > 0).length;
var people = value.data.where((e) => e.unreadMessages > 0).map((e) => e.displayName.split(" ")[0]);
final NotificationService service = NotificationService();
service.initializeNotifications().then((value) {
service.showNotification(
title: "Du hast $messageCount ungelesene Nachrichten!",
body: "In $chatCount Chats, von ${people.join(", ")}",
badgeCount: messageCount,
);
});
});
}
});
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) {
// log("User account status: $value");
// if(value) {
// GetRoom(
// GetRoomParams(
// includeStatus: false,
// ),
// ).run().then((value) {
// var messageCount = value.data.map((e) => e.unreadMessages).reduce((a, b) => a + b);
// var chatCount = value.data.where((e) => e.unreadMessages > 0).length;
// var people = value.data.where((e) => e.unreadMessages > 0).map((e) => e.displayName.split(" ")[0]);
//
// final NotificationService service = NotificationService();
// service.initializeNotifications().then((value) {
// service.showNotification(
// title: "Du hast $messageCount ungelesene Nachrichten!",
// body: "In $chatCount Chats, von ${people.join(", ")}",
// badgeCount: messageCount,
// );
// });
// });
// }
// });
}
static Future<void> onForegroundMessageHandler(RemoteMessage message, BuildContext context) async {
NotificationService().showToast(message: "Du hast eine neue Talk Nachricht!", context: context);
//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);
}
}