1
+ package com .onesignal .flutter ;
2
+
3
+ import com .onesignal .OneSignal ;
4
+ import com .onesignal .Continue ;
5
+
6
+ import com .onesignal .notifications .INotification ;
7
+ import com .onesignal .notifications .INotificationClickHandler ;
8
+ import com .onesignal .notifications .INotificationClickResult ;
9
+ import com .onesignal .notifications .INotificationReceivedEvent ;
10
+ import com .onesignal .notifications .IRemoteNotificationReceivedHandler ;
11
+
12
+ import com .onesignal .notifications .INotificationWillShowInForegroundHandler ;
13
+ import com .onesignal .notifications .INotificationReceivedEvent ;
14
+ import com .onesignal .notifications .IRemoteNotificationReceivedHandler ;
15
+
16
+ import com .onesignal .user .subscriptions .ISubscription ;
17
+ import com .onesignal .user .subscriptions .IPushSubscription ;
18
+ import com .onesignal .notifications .IPermissionChangedHandler ;
19
+
20
+ import org .json .JSONException ;
21
+ import org .json .JSONObject ;
22
+
23
+ import java .util .HashMap ;
24
+ import java .util .Map ;
25
+ import java .util .concurrent .atomic .AtomicBoolean ;
26
+
27
+ import java .util .List ;
28
+ import java .util .Map ;
29
+ import java .util .concurrent .atomic .AtomicBoolean ;
30
+
31
+ import io .flutter .plugin .common .BinaryMessenger ;
32
+ import io .flutter .plugin .common .MethodCall ;
33
+ import io .flutter .plugin .common .MethodChannel ;
34
+ import io .flutter .plugin .common .MethodChannel .MethodCallHandler ;
35
+ import io .flutter .plugin .common .MethodChannel .Result ;
36
+ import io .flutter .plugin .common .PluginRegistry ;
37
+ import io .flutter .plugin .common .PluginRegistry .Registrar ;
38
+
39
+ public class OneSignalNotifications extends FlutterRegistrarResponder implements MethodCallHandler , INotificationClickHandler , INotificationWillShowInForegroundHandler , IPermissionChangedHandler {
40
+ private MethodChannel channel ;
41
+
42
+ private boolean hasSetNotificationWillShowInForegroundHandler = false ;
43
+ private final HashMap <String , INotificationReceivedEvent > notificationReceivedEventCache = new HashMap <>();
44
+
45
+
46
+ static void registerWith (BinaryMessenger messenger ) {
47
+ OneSignalNotifications controller = new OneSignalNotifications ();
48
+ controller .messenger = messenger ;
49
+ controller .channel = new MethodChannel (messenger , "OneSignal#notifications" );
50
+ controller .channel .setMethodCallHandler (controller );
51
+ }
52
+
53
+ @ Override
54
+ public void onMethodCall (MethodCall call , Result result ) {
55
+ if (call .method .contentEquals ("OneSignal#getPermission" ))
56
+ replySuccess (result , OneSignal .getNotifications ().getPermission ());
57
+ else if (call .method .contentEquals ("OneSignal#requestPermission" ))
58
+ this .requestPermission (call , result );
59
+ else if (call .method .contentEquals ("OneSignal#removeNotification" ))
60
+ this .removeNotification (call , result );
61
+ else if (call .method .contentEquals ("OneSignal#removeGroupedNotifications" ))
62
+ this .removeGroupedNotifications (call , result );
63
+ else if (call .method .contentEquals ("OneSignal#clearAll" ))
64
+ this .clearAll (call , result );
65
+ else if (call .method .contentEquals ("OneSignal#initNotificationOpenedHandlerParams" ))
66
+ this .initNotificationOpenedHandlerParams ();
67
+ else if (call .method .contentEquals ("OneSignal#initNotificationWillShowInForegroundHandlerParams" ))
68
+ this .initNotificationWillShowInForegroundHandlerParams ();
69
+ else if (call .method .contentEquals ("OneSignal#completeNotification" ))
70
+ this .initNotificationWillShowInForegroundHandlerParams ();
71
+ else if (call .method .contentEquals ("OneSignal#lifecycleInit" ))
72
+ this .lifecycleInit ();
73
+ else
74
+ replyNotImplemented (result );
75
+ }
76
+
77
+ private void requestPermission (MethodCall call , Result result ) {
78
+ boolean fallback = call .argument ("fallback" );
79
+ OneSignal .getNotifications ().requestPermission (fallback , Continue .none ());
80
+ }
81
+
82
+ private void removeNotification (MethodCall call , Result result ) {
83
+ int notificationId = call .argument ("notificationId" );
84
+ OneSignal .getNotifications ().removeNotification (notificationId );
85
+
86
+ replySuccess (result , null );
87
+ }
88
+
89
+ private void removeGroupedNotifications (MethodCall call , Result result ) {
90
+ String notificationGroup = call .argument ("notificationGroup" );
91
+ OneSignal .getNotifications ().removeGroupedNotifications (notificationGroup );
92
+
93
+ replySuccess (result , null );
94
+ }
95
+
96
+ private void clearAll (MethodCall call , Result result ) {
97
+ OneSignal .getNotifications ().clearAllNotifications ();
98
+ replySuccess (result , null );
99
+ }
100
+
101
+
102
+ private void initNotificationOpenedHandlerParams () {
103
+ OneSignal .getNotifications ().setNotificationClickHandler (this );
104
+ }
105
+
106
+
107
+ private void initNotificationWillShowInForegroundHandlerParams () {
108
+ this .hasSetNotificationWillShowInForegroundHandler = true ;
109
+ }
110
+
111
+ private void completeNotification (MethodCall call , final Result reply ) {
112
+ String notificationId = call .argument ("notificationId" );
113
+ boolean shouldDisplay = call .argument ("shouldDisplay" );
114
+ INotificationReceivedEvent notificationReceivedEvent = notificationReceivedEventCache .get (notificationId );
115
+
116
+ if (notificationReceivedEvent == null ) {
117
+ //OneSignal.onesignalLog(OneSignal.LOG_LEVEL.ERROR, "Could not find notification completion block with id: " + notificationId);
118
+ return ;
119
+ }
120
+
121
+ if (shouldDisplay ) {
122
+ notificationReceivedEvent .complete (notificationReceivedEvent .getNotification ());
123
+ } else {
124
+ notificationReceivedEvent .complete (null );
125
+ }
126
+ }
127
+
128
+ @ Override
129
+ public void notificationClicked (INotificationClickResult result ) {
130
+ try {
131
+ channel .invokeMethod ("OneSignal#handleOpenedNotification" , OneSignalSerializer .convertNotificationClickedResultToMap (result ));
132
+ } catch (JSONException e ) {
133
+ e .getStackTrace ();
134
+ // OneSignal.onesignalLog(OneSignal.LOG_LEVEL.ERROR,
135
+ // "Encountered an error attempting to convert OSNotificationOpenResult object to hash map: " + e.getMessage());
136
+ }
137
+ }
138
+
139
+ @ Override
140
+ public void notificationWillShowInForeground (INotificationReceivedEvent notificationReceivedEvent ) {
141
+ if (!this .hasSetNotificationWillShowInForegroundHandler ) {
142
+ notificationReceivedEvent .complete (notificationReceivedEvent .getNotification ());
143
+ return ;
144
+ }
145
+
146
+ INotification notification = notificationReceivedEvent .getNotification ();
147
+ notificationReceivedEventCache .put (notification .getNotificationId (), notificationReceivedEvent );
148
+
149
+ try {
150
+ HashMap <String , Object > receivedMap = OneSignalSerializer .convertNotificationToMap (notification );
151
+ channel .invokeMethod ("OneSignal#handleNotificationWillShowInForeground" , receivedMap );
152
+ } catch (JSONException e ) {
153
+ e .getStackTrace ();
154
+ // OneSignal.onesignalLog(OneSignal.LOG_LEVEL.ERROR,
155
+ // "Encountered an error attempting to convert OSNotificationReceivedEvent object to hash map: " + e.getMessage());
156
+ }
157
+ }
158
+
159
+ @ Override
160
+ public void onPermissionChanged (boolean permission ) {
161
+
162
+ channel .invokeMethod ("OneSignal#OSPermissionChanged" , OneSignalSerializer .convertPermissionChanged (permission ));
163
+ }
164
+
165
+ public void lifecycleInit () {
166
+ OneSignal .getNotifications ().setNotificationWillShowInForegroundHandler (this );
167
+ OneSignal .getNotifications ().addPermissionChangedHandler (this );
168
+ }
169
+
170
+
171
+ }
0 commit comments