Skip to content

Commit

Permalink
[NOT-3059] notification events
Browse files Browse the repository at this point in the history
  • Loading branch information
anamachadoldo committed Jun 25, 2024
1 parent 1395ec8 commit dbefa73
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 17 deletions.
5 changes: 0 additions & 5 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@ Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
DitoSDK dito = DitoSDK();
dito.onBackgroundMessageHandler(message,
apiKey: Constants.ditoApiKey, secretKey: Constants.ditoSecretKey);
dito.setOnMessageClick((data) {
print('app is in a terminated or background state');
print(data.toJson());
});
}

void main() async {
Expand All @@ -26,7 +22,6 @@ void main() async {
apiKey: Constants.ditoApiKey, secretKey: Constants.ditoSecretKey);
await dito.initializePushNotificationService();
dito.setOnMessageClick((data) {
print('app is open');
print(data.toJson());
});

Expand Down
6 changes: 3 additions & 3 deletions package/lib/dito_sdk.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
library dito_sdk;

import 'package:dito_sdk/notification/notification_entity.dart';
import 'package:dito_sdk/notification/notification_events.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:http/http.dart' as http;
import 'package:event_bus/event_bus.dart';

import 'data/dito_api.dart';
import 'event/event_entity.dart';
Expand All @@ -20,7 +20,7 @@ class DitoSDK {
final UserInterface _userInterface = UserInterface();
final EventInterface _eventInterface = EventInterface();
final NotificationInterface _notificationInterface = NotificationInterface();
EventBus eventBus = EventBus();
final NotificationEvents _notificationEvents = NotificationEvents();

static final DitoSDK _instance = DitoSDK._internal();

Expand Down Expand Up @@ -49,7 +49,7 @@ class DitoSDK {
}

setOnMessageClick(Function(DataPayload) onMessageClicked) {
eventBus.on<NotificationClickedEvent>().listen((event) {
_notificationEvents.stream.on<MessageClickedEvent>().listen((event) {
onMessageClicked(event.data);
});
}
Expand Down
22 changes: 22 additions & 0 deletions package/lib/notification/notification_events.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import 'package:dito_sdk/notification/notification_entity.dart';
import 'package:event_bus/event_bus.dart';

class MessageClickedEvent {
DataPayload data;

MessageClickedEvent(this.data);
}

class NotificationEvents {
EventBus eventBus = EventBus();

static final NotificationEvents _instance = NotificationEvents._internal();

factory NotificationEvents() {
return _instance;
}

EventBus get stream => eventBus;

NotificationEvents._internal();
}
11 changes: 3 additions & 8 deletions package/lib/notification/notification_interface.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'dart:async';
import 'dart:convert';

import 'package:dito_sdk/notification/notification_events.dart';
import 'package:event_bus/event_bus.dart';
import 'package:flutter/foundation.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
Expand All @@ -11,18 +12,12 @@ import 'notification_entity.dart';
import 'notification_repository.dart';
import 'notification_controller.dart';

class NotificationClickedEvent {
DataPayload data;

NotificationClickedEvent(this.data);
}

/// NotificationInterface is an interface for communication with the notification repository and notification controller
class NotificationInterface {
final NotificationRepository _repository = NotificationRepository();
final NotificationController _controller = NotificationController();
final NotificationEvents _notificationEvents = NotificationEvents();
final DitoApi _api = DitoApi();
EventBus eventBus = EventBus();

/// The broadcast stream for received notifications
final StreamController<NotificationEntity>
Expand Down Expand Up @@ -54,7 +49,7 @@ class NotificationInterface {
_controller.showNotification(receivedNotification);
});
_selectNotificationStream.stream.listen((DataPayload data) async {
eventBus.fire(NotificationClickedEvent(data));
_notificationEvents.stream.fire(MessageClickedEvent(data));

final notificationId = data.notification;

Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ environment:
dev_dependencies:
melos: ^6.0.0
dependencies:
event_bus: ^2.0.0
event_bus: 2.0.0

0 comments on commit dbefa73

Please sign in to comment.