feat: implement background notif firebase stream
Some checks failed
continuous-integration/drone/pr Build is failing
Some checks failed
continuous-integration/drone/pr Build is failing
This commit is contained in:
parent
f4972e629d
commit
51efc56ced
@ -6,6 +6,8 @@ import 'package:firebase_messaging/firebase_messaging.dart'
|
|||||||
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
|
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
|
||||||
import 'package:wyatt_cloud_messaging_bloc_base/wyatt_cloud_messaging_bloc.dart';
|
import 'package:wyatt_cloud_messaging_bloc_base/wyatt_cloud_messaging_bloc.dart';
|
||||||
|
|
||||||
|
typedef CloudMessagingStreamController = StreamController<RemoteNotification>;
|
||||||
|
|
||||||
class CloudMessagingFirebaseDataSource extends CloudMessagingRemoteDataSource {
|
class CloudMessagingFirebaseDataSource extends CloudMessagingRemoteDataSource {
|
||||||
final FirebaseMessaging _firebaseMessagingInstance =
|
final FirebaseMessaging _firebaseMessagingInstance =
|
||||||
FirebaseMessaging.instance;
|
FirebaseMessaging.instance;
|
||||||
@ -18,8 +20,36 @@ class CloudMessagingFirebaseDataSource extends CloudMessagingRemoteDataSource {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<Stream<RemoteNotification>> onNotificationBackground() {
|
Future<Stream<RemoteNotification>> onNotificationBackground() async {
|
||||||
throw UnimplementedError();
|
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
|
@override
|
||||||
@ -47,8 +77,8 @@ class CloudMessagingFirebaseDataSource extends CloudMessagingRemoteDataSource {
|
|||||||
iOS: DarwinInitializationSettings(),
|
iOS: DarwinInitializationSettings(),
|
||||||
);
|
);
|
||||||
|
|
||||||
final StreamController<RemoteNotification> foregroundStream =
|
final CloudMessagingStreamController foregroundStream =
|
||||||
StreamController<RemoteNotification>();
|
CloudMessagingStreamController();
|
||||||
|
|
||||||
await _localNotificationInstance.initialize(
|
await _localNotificationInstance.initialize(
|
||||||
initializationSettings,
|
initializationSettings,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user