-
Notifications
You must be signed in to change notification settings - Fork 212
/
OneSignalPlugin.java
475 lines (388 loc) · 18.3 KB
/
OneSignalPlugin.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
package com.onesignal.flutter;
import android.content.Context;
import com.onesignal.OSDeviceState;
import com.onesignal.OSEmailSubscriptionObserver;
import com.onesignal.OSEmailSubscriptionStateChanges;
import com.onesignal.OSInAppMessageAction;
import com.onesignal.OSNotification;
import com.onesignal.OSNotificationOpenedResult;
import com.onesignal.OSNotificationReceivedEvent;
import com.onesignal.OSPermissionObserver;
import com.onesignal.OSPermissionStateChanges;
import com.onesignal.OSSMSSubscriptionObserver;
import com.onesignal.OSSMSSubscriptionStateChanges;
import com.onesignal.OSSubscriptionObserver;
import com.onesignal.OSSubscriptionStateChanges;
import com.onesignal.OneSignal;
import com.onesignal.OneSignal.EmailUpdateError;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;
import io.flutter.plugin.common.PluginRegistry;
import io.flutter.plugin.common.PluginRegistry.Registrar;
import io.flutter.view.FlutterNativeView;
/** OnesignalPlugin */
public class OneSignalPlugin
extends FlutterRegistrarResponder
implements MethodCallHandler,
OneSignal.OSNotificationOpenedHandler,
OneSignal.OSInAppMessageClickHandler,
OSSubscriptionObserver,
OSEmailSubscriptionObserver,
OSSMSSubscriptionObserver,
OSPermissionObserver,
OneSignal.OSNotificationWillShowInForegroundHandler {
/** Plugin registration. */
private OSInAppMessageAction inAppMessageClickedResult;
private boolean hasSetInAppMessageClickedHandler = false;
private boolean hasSetNotificationWillShowInForegroundHandler = false;
private boolean hasSetRequiresPrivacyConsent = false;
private boolean waitingForUserPrivacyConsent = false;
private HashMap<String, OSNotificationReceivedEvent> notificationReceivedEventCache = new HashMap<>();
public static void registerWith(Registrar registrar) {
OneSignal.sdkType = "flutter";
OneSignalPlugin plugin = new OneSignalPlugin();
plugin.waitingForUserPrivacyConsent = false;
plugin.channel = new MethodChannel(registrar.messenger(), "OneSignal");
plugin.channel.setMethodCallHandler(plugin);
plugin.flutterRegistrar = registrar;
// Create a callback for the flutterRegistrar to connect the applications onDestroy
plugin.flutterRegistrar.addViewDestroyListener(new PluginRegistry.ViewDestroyListener() {
@Override
public boolean onViewDestroy(FlutterNativeView flutterNativeView) {
// Remove all handlers so they aren't triggered with wrong context
OneSignal.setNotificationOpenedHandler(null);
OneSignal.setInAppMessageClickHandler(null);
return false;
}
});
OneSignalTagsController.registerWith(registrar);
OneSignalInAppMessagingController.registerWith(registrar);
OneSignalOutcomeEventsController.registerWith(registrar);
}
@Override
public void onMethodCall(MethodCall call, Result result) {
if (call.method.contentEquals("OneSignal#setAppId"))
this.setAppId(call, result);
else if (call.method.contentEquals("OneSignal#setLogLevel"))
this.setLogLevel(call, result);
else if (call.method.contentEquals("OneSignal#log"))
this.oneSignalLog(call, result);
else if (call.method.contentEquals("OneSignal#requiresUserPrivacyConsent"))
replySuccess(result, OneSignal.requiresUserPrivacyConsent());
else if (call.method.contentEquals("OneSignal#setRequiresUserPrivacyConsent"))
this.setRequiresUserPrivacyConsent(call, result);
else if (call.method.contentEquals("OneSignal#consentGranted"))
this.consentGranted(call, result);
else if (call.method.contentEquals("OneSignal#userProvidedPrivacyConsent"))
this.userProvidedPrivacyConsent(call, result);
else if (call.method.contentEquals("OneSignal#promptPermission"))
this.promptPermission(call, result);
else if (call.method.contentEquals("OneSignal#getDeviceState"))
this.getDeviceState(result);
else if (call.method.contentEquals("OneSignal#disablePush"))
this.disablePush(call, result);
else if (call.method.contentEquals("OneSignal#postNotification"))
this.postNotification(call, result);
else if (call.method.contentEquals("OneSignal#promptLocation"))
this.promptLocation(result);
else if (call.method.contentEquals("OneSignal#setLocationShared"))
this.setLocationShared(call, result);
else if (call.method.contentEquals("OneSignal#setEmail"))
this.setEmail(call, result);
else if (call.method.contentEquals("OneSignal#logoutEmail"))
this.logoutEmail(result);
else if (call.method.contentEquals("OneSignal#setSMSNumber"))
this.setSMSNumber(call, result);
else if (call.method.contentEquals("OneSignal#logoutSMSNumber"))
this.logoutSMSNumber(result);
else if (call.method.contentEquals("OneSignal#setExternalUserId"))
this.setExternalUserId(call, result);
else if (call.method.contentEquals("OneSignal#removeExternalUserId"))
this.removeExternalUserId(result);
else if (call.method.contentEquals("OneSignal#initNotificationOpenedHandlerParams"))
this.initNotificationOpenedHandlerParams();
else if (call.method.contentEquals("OneSignal#initInAppMessageClickedHandlerParams"))
this.initInAppMessageClickedHandlerParams();
else if (call.method.contentEquals("OneSignal#initNotificationWillShowInForegroundHandlerParams"))
this.initNotificationWillShowInForegroundHandlerParams();
else if (call.method.contentEquals("OneSignal#completeNotification"))
this.completeNotification(call, result);
else if (call.method.contentEquals("OneSignal#clearOneSignalNotifications"))
this.clearOneSignalNotifications(call, result);
else if (call.method.contentEquals("OneSignal#removeNotification"))
this.removeNotification(call, result);
else
replyNotImplemented(result);
}
private void disablePush(MethodCall call, Result result) {
OneSignal.disablePush((boolean) call.arguments);
replySuccess(result, null);
}
private void setAppId(MethodCall call, Result reply) {
String appId = call.argument("appId");
Context context = flutterRegistrar.activeContext();
OneSignal.setInAppMessageClickHandler(this);
OneSignal.initWithContext(context);
OneSignal.setAppId(appId);
if (hasSetRequiresPrivacyConsent && !OneSignal.userProvidedPrivacyConsent())
this.waitingForUserPrivacyConsent = true;
else
this.addObservers();
replySuccess(reply, null);
}
private void addObservers() {
// Clean observers before setting, avoid being call twice
OneSignal.removeSubscriptionObserver(this);
OneSignal.removeEmailSubscriptionObserver(this);
OneSignal.removeSMSSubscriptionObserver(this);
OneSignal.removePermissionObserver(this);
OneSignal.addSubscriptionObserver(this);
OneSignal.addEmailSubscriptionObserver(this);
OneSignal.addSMSSubscriptionObserver(this);
OneSignal.addPermissionObserver(this);
OneSignal.setNotificationWillShowInForegroundHandler(this);
}
private void setLogLevel(MethodCall call, Result reply) {
int console = call.argument("console");
int visual = call.argument("visual");
OneSignal.setLogLevel(console, visual);
replySuccess(reply, null);
}
private void oneSignalLog(MethodCall call, Result reply) {
int logLevel = call.argument("logLevel");
String message = call.argument("message");
OneSignal.onesignalLog(OneSignal.LOG_LEVEL.values()[logLevel], message);
replySuccess(reply, null);
}
private void setRequiresUserPrivacyConsent(MethodCall call, Result reply) {
boolean required = call.argument("required");
hasSetRequiresPrivacyConsent = required;
OneSignal.setRequiresUserPrivacyConsent(required);
replySuccess(reply, null);
}
private void consentGranted(MethodCall call, Result reply) {
boolean granted = call.argument("granted");
OneSignal.provideUserConsent(granted);
if (this.waitingForUserPrivacyConsent) {
this.waitingForUserPrivacyConsent = false;
this.addObservers();
}
replySuccess(reply, null);
}
private void userProvidedPrivacyConsent(MethodCall call, Result reply) {
boolean result = OneSignal.userProvidedPrivacyConsent();
replySuccess(reply, result);
}
private void promptPermission(MethodCall call, Result result) {
OneSignal.onesignalLog(OneSignal.LOG_LEVEL.ERROR, "promptPermission() is not applicable in Android");
replySuccess(result, null);
}
private void getDeviceState(Result reply) {
OSDeviceState state = OneSignal.getDeviceState();
replySuccess(reply, OneSignalSerializer.convertDeviceStateToMap(state));
}
private void postNotification(MethodCall call, final Result reply) {
JSONObject json = new JSONObject((Map<String, Object>) call.arguments);
OneSignal.postNotification(json, new OSFlutterResultHandler(flutterRegistrar, channel, reply, "postNotification"));
}
private void promptLocation(Result reply) {
OneSignal.promptLocation();
replySuccess(reply, null);
}
private void setLocationShared(MethodCall call, Result result) {
OneSignal.setLocationShared((boolean) call.arguments);
replySuccess(result, null);
}
private void setEmail(MethodCall call, final Result reply) {
String email = call.argument("email");
String emailAuthHashToken = call.argument("emailAuthHashToken");
OneSignal.setEmail(email, emailAuthHashToken, new OSFlutterEmailHandle(flutterRegistrar, channel, reply, "setEmail"));
}
private void logoutEmail(final Result reply) {
OneSignal.logoutEmail(new OSFlutterEmailHandle(flutterRegistrar, channel, reply, "logoutEmail"));
}
private void setSMSNumber(MethodCall call, final Result reply) {
String smsNumber = call.argument("smsNumber");
String smsAuthHashToken = call.argument("smsAuthHashToken");
OneSignal.setSMSNumber(smsNumber, smsAuthHashToken, new OSFlutterResultHandler(flutterRegistrar, channel, reply, "setSMSNumber"));
}
private void logoutSMSNumber(final Result reply) {
OneSignal.logoutSMSNumber(new OSFlutterResultHandler(flutterRegistrar, channel, reply, "logoutSMSNumber"));
}
private void setExternalUserId(MethodCall call, final Result result) {
String externalUserId = call.argument("externalUserId");
String authHashToken = call.argument("authHashToken");
if (externalUserId != null && externalUserId.length() == 0)
externalUserId = null;
if (authHashToken != null && authHashToken.length() == 0)
authHashToken = null;
OneSignal.setExternalUserId(externalUserId, authHashToken, new OSFlutterResultHandler(flutterRegistrar, channel, result, "setExternalUserId"));
}
private void removeExternalUserId(final Result result) {
OneSignal.removeExternalUserId(new OSFlutterResultHandler(flutterRegistrar, channel, result, "removeExternalUserId"));
}
private void initNotificationOpenedHandlerParams() {
OneSignal.setNotificationOpenedHandler(this);
}
private void initInAppMessageClickedHandlerParams() {
this.hasSetInAppMessageClickedHandler = true;
if (this.inAppMessageClickedResult != null) {
this.inAppMessageClicked(this.inAppMessageClickedResult);
this.inAppMessageClickedResult = null;
}
}
private void initNotificationWillShowInForegroundHandlerParams() {
this.hasSetNotificationWillShowInForegroundHandler = true;
}
private void clearOneSignalNotifications(MethodCall call, final Result reply) {
OneSignal.clearOneSignalNotifications();
replySuccess(reply, null);
}
private void removeNotification(MethodCall call, final Result reply) {
int notificationId = call.argument("notificationId");
OneSignal.removeNotification(notificationId);
replySuccess(reply, null);
}
private void completeNotification(MethodCall call, final Result reply) {
String notificationId = call.argument("notificationId");
boolean shouldDisplay = call.argument("shouldDisplay");
OSNotificationReceivedEvent notificationReceivedEvent = notificationReceivedEventCache.get(notificationId);
if (notificationReceivedEvent == null) {
OneSignal.onesignalLog(OneSignal.LOG_LEVEL.ERROR, "Could not find notification completion block with id: " + notificationId);
return;
}
if (shouldDisplay) {
notificationReceivedEvent.complete(notificationReceivedEvent.getNotification());
} else {
notificationReceivedEvent.complete(null);
}
}
@Override
public void onOSSubscriptionChanged(OSSubscriptionStateChanges stateChanges) {
invokeMethodOnUiThread("OneSignal#subscriptionChanged", OneSignalSerializer.convertSubscriptionStateChangesToMap(stateChanges));
}
@Override
public void onOSEmailSubscriptionChanged(OSEmailSubscriptionStateChanges stateChanges) {
invokeMethodOnUiThread("OneSignal#emailSubscriptionChanged", OneSignalSerializer.convertEmailSubscriptionStateChangesToMap(stateChanges));
}
@Override
public void onSMSSubscriptionChanged(OSSMSSubscriptionStateChanges stateChanges) {
invokeMethodOnUiThread("OneSignal#smsSubscriptionChanged", OneSignalSerializer.convertSMSSubscriptionStateChangesToMap(stateChanges));
}
@Override
public void onOSPermissionChanged(OSPermissionStateChanges stateChanges) {
invokeMethodOnUiThread("OneSignal#permissionChanged", OneSignalSerializer.convertPermissionStateChangesToMap(stateChanges));
}
@Override
public void notificationOpened(OSNotificationOpenedResult result) {
try {
invokeMethodOnUiThread("OneSignal#handleOpenedNotification", OneSignalSerializer.convertNotificationOpenResultToMap(result));
} catch (JSONException e) {
e.getStackTrace();
OneSignal.onesignalLog(OneSignal.LOG_LEVEL.ERROR,
"Encountered an error attempting to convert OSNotificationOpenResult object to hash map: " + e.getMessage());
}
}
@Override
public void inAppMessageClicked(OSInAppMessageAction action) {
if (!this.hasSetInAppMessageClickedHandler) {
this.inAppMessageClickedResult = action;
return;
}
invokeMethodOnUiThread("OneSignal#handleClickedInAppMessage", OneSignalSerializer.convertInAppMessageClickedActionToMap(action));
}
@Override
public void notificationWillShowInForeground(OSNotificationReceivedEvent notificationReceivedEvent) {
if (!this.hasSetNotificationWillShowInForegroundHandler) {
notificationReceivedEvent.complete(notificationReceivedEvent.getNotification());
return;
}
OSNotification notification = notificationReceivedEvent.getNotification();
notificationReceivedEventCache.put(notification.getNotificationId(), notificationReceivedEvent);
try {
HashMap<String, Object> receivedMap = OneSignalSerializer.convertNotificationReceivedEventToMap(notificationReceivedEvent);
invokeMethodOnUiThread("OneSignal#handleNotificationWillShowInForeground", receivedMap);
} catch (JSONException e) {
e.getStackTrace();
OneSignal.onesignalLog(OneSignal.LOG_LEVEL.ERROR,
"Encountered an error attempting to convert OSNotificationReceivedEvent object to hash map: " + e.getMessage());
}
}
static class OSFlutterEmailHandle extends FlutterRegistrarResponder
implements OneSignal.EmailUpdateHandler {
private final Result result;
private final String methodName;
private final AtomicBoolean replySubmitted = new AtomicBoolean(false);
OSFlutterEmailHandle(PluginRegistry.Registrar flutterRegistrar, MethodChannel channel, Result res, String methodName) {
this.flutterRegistrar = flutterRegistrar;
this.channel = channel;
this.result = res;
this.methodName = methodName;
}
@Override
public void onSuccess() {
if (this.replySubmitted.getAndSet(true))
return;
replySuccess(result, null);
}
@Override
public void onFailure(EmailUpdateError error) {
if (this.replySubmitted.getAndSet(true))
return;
replyError(result, "OneSignal",
"Encountered an error when " + methodName + ": " + error.getMessage(),
null);
}
}
static class OSFlutterResultHandler extends FlutterRegistrarResponder
implements OneSignal.OSExternalUserIdUpdateCompletionHandler, OneSignal.OSSMSUpdateHandler, OneSignal.PostNotificationResponseHandler {
private final Result result;
private final String methodName;
private final AtomicBoolean replySubmitted = new AtomicBoolean(false);
OSFlutterResultHandler(PluginRegistry.Registrar flutterRegistrar, MethodChannel channel, Result res, String methodName) {
this.flutterRegistrar = flutterRegistrar;
this.channel = channel;
this.result = res;
this.methodName = methodName;
}
@Override
public void onSuccess(JSONObject results) {
if (this.replySubmitted.getAndSet(true))
return;
try {
replySuccess(result, OneSignalSerializer.convertJSONObjectToHashMap(results));
} catch (JSONException e) {
replyError(result, "OneSignal", "Encountered an error attempting to deserialize server response for " + methodName + ": " + e.getMessage(), null);
}
}
@Override
public void onFailure(OneSignal.OSSMSUpdateError error) {
if (this.replySubmitted.getAndSet(true))
return;
replyError(result, "OneSignal", "Encountered an error when " + methodName + " (" + error.getType() + "): " + error.getMessage(), null);
}
@Override
public void onFailure(OneSignal.ExternalIdError error) {
if (this.replySubmitted.getAndSet(true))
return;
replyError(result, "OneSignal", "Encountered an error when " + methodName + " (" + error.getType() + "): " + error.getMessage(), null);
}
@Override
public void onFailure(JSONObject response) {
if (this.replySubmitted.getAndSet(true))
return;
try {
replyError(result, "OneSignal", "Encountered an error attempting to " + methodName + " " + response.toString(), OneSignalSerializer.convertJSONObjectToHashMap(response));
} catch (JSONException jsonException) {
replyError(result, "OneSignal", "Encountered an error attempting to deserialize server response " + methodName + " " + jsonException.getMessage(), null);
}
}
}
}