38
38
39
39
public class OneSignalNotifications extends FlutterRegistrarResponder implements MethodCallHandler , INotificationClickListener , INotificationLifecycleListener , IPermissionObserver {
40
40
private final HashMap <String , INotificationWillDisplayEvent > notificationOnWillDisplayEventCache = new HashMap <>();
41
+ private final HashMap <String , INotificationWillDisplayEvent > preventedDefaultCache = new HashMap <>();
42
+
41
43
42
44
static void registerWith (BinaryMessenger messenger ) {
43
45
OneSignalNotifications controller = new OneSignalNotifications ();
@@ -64,14 +66,17 @@ else if (call.method.contentEquals("OneSignal#preventDefault"))
64
66
this .preventDefault (call , result );
65
67
else if (call .method .contentEquals ("OneSignal#lifecycleInit" ))
66
68
this .lifecycleInit ();
69
+ else if (call .method .contentEquals ("OneSignal#proceedWithWillDisplay" ))
70
+ this .proceedWithWillDisplay (call , result );
67
71
else
68
72
replyNotImplemented (result );
69
73
}
70
74
71
75
private void requestPermission (MethodCall call , Result result ) {
72
76
boolean fallback = (boolean ) call .argument ("fallbackToSettings" );
73
- OneSignal .getNotifications ().requestPermission (fallback , Continue .none ());
74
- replySuccess (result , null );
77
+ OneSignal .getNotifications ().requestPermission (fallback , Continue .with (permissionResult -> {
78
+ replySuccess (result , permissionResult .getData ());
79
+ }));
75
80
}
76
81
77
82
private void removeNotification (MethodCall call , Result result ) {
@@ -93,6 +98,24 @@ private void clearAll(MethodCall call, Result result) {
93
98
replySuccess (result , null );
94
99
}
95
100
101
+ /// Our bridge layer needs to preventDefault() so that the Flutter listener has time to preventDefault() before the notification is displayed
102
+ /// This function is called after all of the flutter listeners have responded to the willDisplay event.
103
+ /// If any of them have called preventDefault() we will not call display(). Otherwise we will display.
104
+ private void proceedWithWillDisplay (MethodCall call , Result result ) {
105
+ String notificationId = call .argument ("notificationId" );
106
+ INotificationWillDisplayEvent event = notificationOnWillDisplayEventCache .get (notificationId );
107
+ if (event == null ) {
108
+ Logging .error ("Could not find onWillDisplayNotification event for notification with id: " + notificationId , null );
109
+ return ;
110
+ }
111
+ if (this .preventedDefaultCache .containsKey (notificationId )) {
112
+ replySuccess (result , null );
113
+ return ;
114
+ }
115
+ event .getNotification ().display ();
116
+ replySuccess (result , null );
117
+ }
118
+
96
119
private void displayNotification (MethodCall call , Result result ) {
97
120
String notificationId = call .argument ("notificationId" );
98
121
INotificationWillDisplayEvent event = notificationOnWillDisplayEventCache .get (notificationId );
@@ -112,6 +135,7 @@ private void preventDefault(MethodCall call, Result result) {
112
135
return ;
113
136
}
114
137
event .preventDefault ();
138
+ this .preventedDefaultCache .put (notificationId , event );
115
139
replySuccess (result , null );
116
140
}
117
141
@@ -141,6 +165,8 @@ private JSONObject getJsonFromMap(Map<String, Object> map) throws JSONException
141
165
public void onWillDisplay (INotificationWillDisplayEvent event ) {
142
166
INotification notification = event .getNotification ();
143
167
notificationOnWillDisplayEventCache .put (notification .getNotificationId (), event );
168
+ /// Our bridge layer needs to preventDefault() so that the Flutter listener has time to preventDefault() before the notification is displayed
169
+ event .preventDefault ();
144
170
try {
145
171
invokeMethodOnUiThread ("OneSignal#onWillDisplayNotification" , OneSignalSerializer .convertNotificationWillDisplayEventToMap (event ));
146
172
} catch (JSONException e ) {
0 commit comments