Skip to content

Commit 43b0e83

Browse files
committed
Use functions instead of classes for notification will display listeners
1 parent 729f9d6 commit 43b0e83

File tree

2 files changed

+33
-36
lines changed

2 files changed

+33
-36
lines changed

example/lib/main.dart

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,7 @@ class MyApp extends StatefulWidget {
1111
_MyAppState createState() => new _MyAppState();
1212
}
1313

14-
class _MyAppState extends State<MyApp>
15-
with
16-
OneSignalNotificationLifecycleListener,
17-
OneSignalNotificationClickListener {
14+
class _MyAppState extends State<MyApp> with OneSignalNotificationClickListener {
1815
String _debugLabelString = "";
1916
String? _emailAddress;
2017
String? _smsNumber;
@@ -61,7 +58,25 @@ class _MyAppState extends State<MyApp>
6158
});
6259

6360
OneSignal.Notifications.addClickListener(this);
64-
OneSignal.Notifications.addLifecycleListener(this);
61+
62+
OneSignal.Notifications.addForegroundWillDisplayListener((event) {
63+
print(
64+
'NOTIFICATION WILL DISPLAY LISTENER CALLED WITH: ${event.notification.jsonRepresentation()}');
65+
66+
/// Display Notification, preventDefault to not display
67+
event.preventDefault();
68+
69+
/// Do async work
70+
71+
/// notification.display() to display after preventing default
72+
event.notification.display();
73+
74+
this.setState(() {
75+
_debugLabelString =
76+
"Notification received in foreground notification: \n${event.notification.jsonRepresentation().replaceAll("\\n", "\n")}";
77+
});
78+
});
79+
6580
OneSignal.InAppMessages.addClickListener((event) {
6681
this.setState(() {
6782
_debugLabelString =
@@ -94,7 +109,7 @@ class _MyAppState extends State<MyApp>
94109
// Some examples of how to use Outcome Events public methods with OneSignal SDK
95110
oneSignalOutcomeExamples();
96111

97-
OneSignal.InAppMessages.paused(false);
112+
OneSignal.InAppMessages.paused(true);
98113
}
99114

100115
void onClickNotification(OSNotificationClickEvent event) {
@@ -105,24 +120,6 @@ class _MyAppState extends State<MyApp>
105120
});
106121
}
107122

108-
void onWillDisplayNotification(OSNotificationWillDisplayEvent event) {
109-
print(
110-
'NOTIFICATION WILL DISPLAY LISTENER CALLED WITH: ${event.notification.jsonRepresentation()}');
111-
112-
/// Display Notification, preventDefault to not display
113-
event.preventDefault();
114-
115-
/// Do async work
116-
117-
/// notification.display() to display after preventing default
118-
event.notification.display();
119-
120-
this.setState(() {
121-
_debugLabelString =
122-
"Notification received in foreground notification: \n${event.notification.jsonRepresentation().replaceAll("\\n", "\n")}";
123-
});
124-
}
125-
126123
void _handleSendTags() {
127124
print("Sending tags");
128125
OneSignal.User.addTagWithKey("test2", "val2");

lib/src/notifications.dart

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@ import 'package:onesignal_flutter/src/permission.dart';
77

88
typedef void OnNotificationPermissionChangeObserver(bool permission);
99

10-
class OneSignalNotificationLifecycleListener {
11-
void onWillDisplayNotification(OSNotificationWillDisplayEvent event) {}
12-
}
10+
typedef void OnNotificationWillDisplayListener(
11+
OSNotificationWillDisplayEvent event);
1312

1413
class OneSignalNotificationClickListener {
1514
void onClickNotification(OSNotificationClickEvent event) {}
@@ -19,8 +18,8 @@ class OneSignalNotifications {
1918
// event listeners
2019
List<OneSignalNotificationClickListener> _clickListeners =
2120
<OneSignalNotificationClickListener>[];
22-
List<OneSignalNotificationLifecycleListener> _lifecycleListeners =
23-
<OneSignalNotificationLifecycleListener>[];
21+
List<OnNotificationWillDisplayListener> _willDisplayListeners =
22+
<OnNotificationWillDisplayListener>[];
2423

2524
// private channels used to bridge to ObjC/Java
2625
MethodChannel _channel = const MethodChannel('OneSignal#notifications');
@@ -132,8 +131,8 @@ class OneSignalNotifications {
132131
OSNotificationClickEvent(call.arguments.cast<String, dynamic>()));
133132
}
134133
} else if (call.method == 'OneSignal#onWillDisplayNotification') {
135-
for (var listener in _lifecycleListeners) {
136-
listener.onWillDisplayNotification(OSNotificationWillDisplayEvent(
134+
for (var listener in _willDisplayListeners) {
135+
listener(OSNotificationWillDisplayEvent(
137136
call.arguments.cast<String, dynamic>()));
138137
}
139138
var event = OSNotificationWillDisplayEvent(
@@ -152,13 +151,14 @@ class OneSignalNotifications {
152151
}
153152
}
154153

155-
void addLifecycleListener(OneSignalNotificationLifecycleListener listener) {
156-
_lifecycleListeners.add(listener);
154+
void addForegroundWillDisplayListener(
155+
OnNotificationWillDisplayListener listener) {
156+
_willDisplayListeners.add(listener);
157157
}
158158

159-
void removeLifecycleListener(
160-
OneSignalNotificationLifecycleListener listener) {
161-
_lifecycleListeners.remove(listener);
159+
void removeForegroundWillDisplayListener(
160+
OnNotificationWillDisplayListener listener) {
161+
_willDisplayListeners.remove(listener);
162162
}
163163

164164
/// The notification willDisplay listener is called whenever a notification arrives

0 commit comments

Comments
 (0)