Cloud Messaging Package #206

Merged
hugo merged 14 commits from notification_bloc/feature/make_the_package_compatible_with_firebase_messaging into master 2023-08-18 14:05:18 +00:00
Showing only changes of commit 51efc56ced - Show all commits

View File

@ -6,6 +6,8 @@ import 'package:firebase_messaging/firebase_messaging.dart'
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:wyatt_cloud_messaging_bloc_base/wyatt_cloud_messaging_bloc.dart';
typedef CloudMessagingStreamController = StreamController<RemoteNotification>;
class CloudMessagingFirebaseDataSource extends CloudMessagingRemoteDataSource {
final FirebaseMessaging _firebaseMessagingInstance =
FirebaseMessaging.instance;
@ -18,8 +20,36 @@ class CloudMessagingFirebaseDataSource extends CloudMessagingRemoteDataSource {
}
@override
Future<Stream<RemoteNotification>> onNotificationBackground() {
throw UnimplementedError();
Future<Stream<RemoteNotification>> onNotificationBackground() async {
final backgroundStreamController = CloudMessagingStreamController();
Future<void> getLastBackgroundNotification() async {
final lastNotification =
await _firebaseMessagingInstance.getInitialMessage();
if (lastNotification != null) {
backgroundStreamController.add(
RemoteNotification(
title: lastNotification.notification?.title,
body: lastNotification.notification?.body,
data: lastNotification.data,
),
);
}
FirebaseMessaging.onMessageOpenedApp.listen((notification) {
backgroundStreamController.add(
RemoteNotification(
title: notification.notification?.title,
body: notification.notification?.body,
data: notification.data,
),
);
});
}
unawaited(getLastBackgroundNotification());
return backgroundStreamController.stream;
}
@override
@ -47,8 +77,8 @@ class CloudMessagingFirebaseDataSource extends CloudMessagingRemoteDataSource {
iOS: DarwinInitializationSettings(),
);
final StreamController<RemoteNotification> foregroundStream =
StreamController<RemoteNotification>();
final CloudMessagingStreamController foregroundStream =
CloudMessagingStreamController();
await _localNotificationInstance.initialize(
initializationSettings,