|
1 | 1 | package com.onesignal.flutter;
|
2 | 2 |
|
| 3 | +import androidx.annotation.NonNull; |
| 4 | + |
3 | 5 | import com.onesignal.debug.internal.logging.Logging;
|
4 | 6 | import com.onesignal.OneSignal;
|
5 |
| -import com.onesignal.Continue; |
6 |
| - |
7 | 7 |
|
8 | 8 | import com.onesignal.notifications.INotification;
|
9 | 9 | import com.onesignal.notifications.INotificationClickEvent;
|
|
23 | 23 | import io.flutter.plugin.common.MethodChannel;
|
24 | 24 | import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
|
25 | 25 | import io.flutter.plugin.common.MethodChannel.Result;
|
| 26 | +import kotlin.coroutines.Continuation; |
| 27 | +import kotlin.coroutines.CoroutineContext; |
| 28 | +import kotlinx.coroutines.Dispatchers; |
26 | 29 |
|
27 | 30 | public class OneSignalNotifications extends FlutterMessengerResponder implements MethodCallHandler, INotificationClickListener, INotificationLifecycleListener, IPermissionObserver {
|
28 | 31 | private final HashMap<String, INotificationWillDisplayEvent> notificationOnWillDisplayEventCache = new HashMap<>();
|
29 | 32 | private final HashMap<String, INotificationWillDisplayEvent> preventedDefaultCache = new HashMap<>();
|
30 | 33 |
|
| 34 | + /** |
| 35 | + * A helper class to encapsulate invoking the suspending function [requestPermission] in Java. |
| 36 | + * To support API level < 24, the SDK cannot use the OneSignal-defined [Continue.with] helper method. |
| 37 | + */ |
| 38 | + private class RequestPermissionContinuation implements Continuation<Boolean> { |
| 39 | + |
| 40 | + private final MethodChannel.Result result; |
| 41 | + |
| 42 | + public RequestPermissionContinuation(MethodChannel.Result result) { |
| 43 | + this.result = result; |
| 44 | + } |
| 45 | + |
| 46 | + @NonNull |
| 47 | + @Override |
| 48 | + public CoroutineContext getContext() { |
| 49 | + return (CoroutineContext) Dispatchers.getMain(); |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + public void resumeWith(@NonNull Object o) { |
| 54 | + if (o instanceof kotlin.Result.Failure) { |
| 55 | + Throwable e = ((kotlin.Result.Failure) o).exception; |
| 56 | + replyError(result, "OneSignal", "requestPermission failed with error: " + e.getMessage() + "\n" + e.getStackTrace(), null); |
| 57 | + } |
| 58 | + else { |
| 59 | + replySuccess(result, o); |
| 60 | + } |
| 61 | + } |
| 62 | + } |
31 | 63 |
|
32 | 64 | static void registerWith(BinaryMessenger messenger) {
|
33 | 65 | OneSignalNotifications controller = new OneSignalNotifications();
|
@@ -72,9 +104,7 @@ private void requestPermission(MethodCall call, Result result) {
|
72 | 104 | return;
|
73 | 105 | }
|
74 | 106 |
|
75 |
| - OneSignal.getNotifications().requestPermission(fallback, Continue.with(permissionResult -> { |
76 |
| - replySuccess(result, permissionResult.getData()); |
77 |
| - })); |
| 107 | + OneSignal.getNotifications().requestPermission(fallback, new RequestPermissionContinuation(result)); |
78 | 108 | }
|
79 | 109 |
|
80 | 110 | private void removeNotification(MethodCall call, Result result) {
|
|
0 commit comments