Implemented structure for push Notifications
This commit is contained in:
45
lib/notification/notificationController.dart
Normal file
45
lib/notification/notificationController.dart
Normal file
@ -0,0 +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 '../api/marianumcloud/talk/room/getRoom.dart';
|
||||
import '../api/marianumcloud/talk/room/getRoomParams.dart';
|
||||
import '../model/accountData.dart';
|
||||
import 'notificationService.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.map((e) => e.unreadMessages).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(", ")}"
|
||||
);
|
||||
});
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
static Future<void> onForegroundMessageHandler(RemoteMessage message, BuildContext context) async {
|
||||
NotificationService().showToast(message: "Du hast eine neue Talk Nachricht!", context: context);
|
||||
}
|
||||
}
|
75
lib/notification/notificationService.dart
Normal file
75
lib/notification/notificationService.dart
Normal file
@ -0,0 +1,75 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
|
||||
import 'package:fluttertoast/fluttertoast.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
import '../model/chatList/chatListProps.dart';
|
||||
import '../model/message/messageProps.dart';
|
||||
|
||||
class NotificationService {
|
||||
static final NotificationService _instance = NotificationService._internal();
|
||||
|
||||
factory NotificationService() {
|
||||
return _instance;
|
||||
}
|
||||
|
||||
NotificationService._internal();
|
||||
|
||||
FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();
|
||||
|
||||
Future<void> initializeNotifications() async {
|
||||
const AndroidInitializationSettings androidSettings = AndroidInitializationSettings(
|
||||
'@mipmap/ic_launcher'
|
||||
);
|
||||
|
||||
final DarwinInitializationSettings iosSettings = DarwinInitializationSettings(
|
||||
onDidReceiveLocalNotification: (id, title, body, payload) {
|
||||
// TODO Navigate to Talk section (This runs when an Notification is tapped)
|
||||
},
|
||||
);
|
||||
|
||||
|
||||
final InitializationSettings initializationSettings = InitializationSettings(
|
||||
android: androidSettings,
|
||||
iOS: iosSettings,
|
||||
);
|
||||
|
||||
await flutterLocalNotificationsPlugin.initialize(
|
||||
initializationSettings,
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> showNotification({required String title, required String body}) async {
|
||||
const AndroidNotificationDetails androidPlatformChannelSpecifics =
|
||||
AndroidNotificationDetails(
|
||||
'your_channel_id',
|
||||
'Your Channel Name',
|
||||
importance: Importance.defaultImportance,
|
||||
priority: Priority.defaultPriority,
|
||||
ticker: 'ticker',
|
||||
);
|
||||
|
||||
const NotificationDetails platformChannelSpecifics = NotificationDetails(android: androidPlatformChannelSpecifics);
|
||||
|
||||
await flutterLocalNotificationsPlugin.show(
|
||||
0,
|
||||
title,
|
||||
body,
|
||||
platformChannelSpecifics,
|
||||
);
|
||||
}
|
||||
|
||||
void showToast({required String message, required BuildContext context, ToastGravity gravity = ToastGravity.BOTTOM}) {
|
||||
Fluttertoast.showToast(
|
||||
msg: message,
|
||||
gravity: gravity,
|
||||
toastLength: Toast.LENGTH_SHORT,
|
||||
backgroundColor: Theme.of(context).primaryColor,
|
||||
textColor: Colors.white,
|
||||
fontSize: 13.0,
|
||||
);
|
||||
|
||||
Provider.of<ChatListProps>(context, listen: false).run(renew: true);
|
||||
Provider.of<MessageProps>(context, listen: false).run(renew: true);
|
||||
}
|
||||
}
|
22
lib/notification/notifyUpdater.dart
Normal file
22
lib/notification/notifyUpdater.dart
Normal file
@ -0,0 +1,22 @@
|
||||
|
||||
import 'package:firebase_messaging/firebase_messaging.dart';
|
||||
|
||||
import '../api/mhsl/notify/register/notifyRegister.dart';
|
||||
import '../api/mhsl/notify/register/notifyRegisterParams.dart';
|
||||
import '../model/accountData.dart';
|
||||
|
||||
class NotifyUpdater {
|
||||
static void registerToServer() async {
|
||||
String? fcmToken = await FirebaseMessaging.instance.getToken();
|
||||
|
||||
if(fcmToken == null) throw Exception("Failed to register push notification because there is no FBC token!");
|
||||
|
||||
NotifyRegister(
|
||||
NotifyRegisterParams(
|
||||
username: AccountData().getUsername(),
|
||||
password: AccountData().getPassword(),
|
||||
fcmToken: fcmToken,
|
||||
),
|
||||
).run();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user