diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 6cc31ef9..8f12292b 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -22,8 +22,11 @@ jobs: - name: Static Analysis run: flutter analyze - - name: Ensure the Dart code is formatted correctly - run: dart format --set-exit-if-changed --output=none . + - name: Setup Clang + uses: OneSignal/sdk-actions/.github/actions/setup-clang@main + + - name: Check formatting (dart,java,c) + run: dart run rps format-check - name: Run Flutter unit tests run: dart run rps test diff --git a/.gitignore b/.gitignore index 7868544b..4ddccc77 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ .DS_Store .dart_tool/ +.gradle/ .packages .pub/ diff --git a/analysis_options.yaml b/analysis_options.yaml new file mode 100644 index 00000000..f7375932 --- /dev/null +++ b/analysis_options.yaml @@ -0,0 +1,3 @@ +analyzer: + exclude: + - test/dlcov_references_test.dart diff --git a/android/src/main/java/com/onesignal/flutter/FlutterMessengerResponder.java b/android/src/main/java/com/onesignal/flutter/FlutterMessengerResponder.java index 1b38377c..8c739f6e 100644 --- a/android/src/main/java/com/onesignal/flutter/FlutterMessengerResponder.java +++ b/android/src/main/java/com/onesignal/flutter/FlutterMessengerResponder.java @@ -3,75 +3,72 @@ import android.content.Context; import android.os.Handler; import android.os.Looper; - -import java.util.HashMap; - import io.flutter.plugin.common.BinaryMessenger; import io.flutter.plugin.common.MethodChannel; +import java.util.HashMap; abstract class FlutterMessengerResponder { - Context context; - protected MethodChannel channel; - BinaryMessenger messenger; + Context context; + protected MethodChannel channel; + BinaryMessenger messenger; - /** - * MethodChannel class is home to success() method used by Result class - * It has the @UiThread annotation and must be run on UI thread, otherwise a RuntimeException will be thrown - * This will communicate success back to Dart - */ - void replySuccess(final MethodChannel.Result reply, final Object response) { - runOnMainThread(new Runnable() { - @Override - public void run() { - reply.success(response); - } - }); - } + /** + * MethodChannel class is home to success() method used by Result class + * It has the @UiThread annotation and must be run on UI thread, otherwise a RuntimeException will be thrown + * This will communicate success back to Dart + */ + void replySuccess(final MethodChannel.Result reply, final Object response) { + runOnMainThread(new Runnable() { + @Override + public void run() { + reply.success(response); + } + }); + } - /** - * MethodChannel class is home to error() method used by Result class - * It has the @UiThread annotation and must be run on UI thread, otherwise a RuntimeException will be thrown - * This will communicate error back to Dart - */ - void replyError(final MethodChannel.Result reply, final String tag, final String message, final Object response) { - runOnMainThread(new Runnable() { - @Override - public void run() { - reply.error(tag, message, response); - } - }); - } + /** + * MethodChannel class is home to error() method used by Result class + * It has the @UiThread annotation and must be run on UI thread, otherwise a RuntimeException will be thrown + * This will communicate error back to Dart + */ + void replyError(final MethodChannel.Result reply, final String tag, final String message, final Object response) { + runOnMainThread(new Runnable() { + @Override + public void run() { + reply.error(tag, message, response); + } + }); + } - /** - * MethodChannel class is home to notImplemented() method used by Result class - * It has the @UiThread annotation and must be run on UI thread, otherwise a RuntimeException will be thrown - * This will communicate not implemented back to Dart - */ - void replyNotImplemented(final MethodChannel.Result reply) { - runOnMainThread(new Runnable() { - @Override - public void run() { - reply.notImplemented(); - } - }); - } + /** + * MethodChannel class is home to notImplemented() method used by Result class + * It has the @UiThread annotation and must be run on UI thread, otherwise a RuntimeException will be thrown + * This will communicate not implemented back to Dart + */ + void replyNotImplemented(final MethodChannel.Result reply) { + runOnMainThread(new Runnable() { + @Override + public void run() { + reply.notImplemented(); + } + }); + } - private void runOnMainThread(final Runnable runnable) { - if (Looper.getMainLooper().getThread() == Thread.currentThread()) - runnable.run(); - else { - Handler handler = new Handler(Looper.getMainLooper()); - handler.post(runnable); - } - } + private void runOnMainThread(final Runnable runnable) { + if (Looper.getMainLooper().getThread() == Thread.currentThread()) runnable.run(); + else { + Handler handler = new Handler(Looper.getMainLooper()); + handler.post(runnable); + } + } - void invokeMethodOnUiThread(final String methodName, final HashMap map) { - //final MethodChannel channel = this.channel; - runOnMainThread(new Runnable() { - @Override - public void run() { - channel.invokeMethod(methodName, map); - } - }); - } + void invokeMethodOnUiThread(final String methodName, final HashMap map) { + // final MethodChannel channel = this.channel; + runOnMainThread(new Runnable() { + @Override + public void run() { + channel.invokeMethod(methodName, map); + } + }); + } } diff --git a/android/src/main/java/com/onesignal/flutter/OneSignalDebug.java b/android/src/main/java/com/onesignal/flutter/OneSignalDebug.java index e4e446e5..5b92a503 100644 --- a/android/src/main/java/com/onesignal/flutter/OneSignalDebug.java +++ b/android/src/main/java/com/onesignal/flutter/OneSignalDebug.java @@ -2,7 +2,6 @@ import com.onesignal.OneSignal; import com.onesignal.debug.LogLevel; - import io.flutter.plugin.common.BinaryMessenger; import io.flutter.plugin.common.MethodCall; import io.flutter.plugin.common.MethodChannel; @@ -10,8 +9,8 @@ import io.flutter.plugin.common.MethodChannel.Result; public class OneSignalDebug extends FlutterMessengerResponder implements MethodCallHandler { - - static void registerWith(BinaryMessenger messenger) { + + static void registerWith(BinaryMessenger messenger) { OneSignalDebug controller = new OneSignalDebug(); controller.messenger = messenger; controller.channel = new MethodChannel(messenger, "OneSignal#debug"); @@ -20,12 +19,9 @@ static void registerWith(BinaryMessenger messenger) { @Override public void onMethodCall(MethodCall call, Result result) { - if (call.method.contentEquals("OneSignal#setLogLevel")) - this.setLogLevel(call, result); - else if (call.method.contentEquals("OneSignal#setAlertLevel")) - this.setAlertLevel(call, result); - else - replyNotImplemented(result); + if (call.method.contentEquals("OneSignal#setLogLevel")) this.setLogLevel(call, result); + else if (call.method.contentEquals("OneSignal#setAlertLevel")) this.setAlertLevel(call, result); + else replyNotImplemented(result); } private void setLogLevel(MethodCall call, Result reply) { @@ -34,21 +30,19 @@ private void setLogLevel(MethodCall call, Result reply) { LogLevel consoleLogLevel = LogLevel.fromInt(console); OneSignal.getDebug().setLogLevel(consoleLogLevel); replySuccess(reply, null); - } - catch(ClassCastException e) { + } catch (ClassCastException e) { replyError(reply, "OneSignal", "failed with error: " + e.getMessage() + "\n" + e.getStackTrace(), null); - } + } } - private void setAlertLevel(MethodCall call, Result reply) { + private void setAlertLevel(MethodCall call, Result reply) { try { int visual = call.argument("visualLevel"); LogLevel visualLogLevel = LogLevel.fromInt(visual); OneSignal.getDebug().setAlertLevel(visualLogLevel); replySuccess(reply, null); - } - catch(ClassCastException e) { + } catch (ClassCastException e) { replyError(reply, "OneSignal", "failed with error: " + e.getMessage() + "\n" + e.getStackTrace(), null); - } + } } } diff --git a/android/src/main/java/com/onesignal/flutter/OneSignalInAppMessages.java b/android/src/main/java/com/onesignal/flutter/OneSignalInAppMessages.java index 0afccdd5..7d2a29c0 100644 --- a/android/src/main/java/com/onesignal/flutter/OneSignalInAppMessages.java +++ b/android/src/main/java/com/onesignal/flutter/OneSignalInAppMessages.java @@ -2,55 +2,45 @@ import com.onesignal.OneSignal; import com.onesignal.debug.internal.logging.Logging; -import org.json.JSONException; - -import com.onesignal.inAppMessages.IInAppMessageClickListener; import com.onesignal.inAppMessages.IInAppMessageClickEvent; -import com.onesignal.inAppMessages.IInAppMessageLifecycleListener; -import com.onesignal.inAppMessages.IInAppMessageWillDisplayEvent; +import com.onesignal.inAppMessages.IInAppMessageClickListener; +import com.onesignal.inAppMessages.IInAppMessageDidDismissEvent; import com.onesignal.inAppMessages.IInAppMessageDidDisplayEvent; +import com.onesignal.inAppMessages.IInAppMessageLifecycleListener; import com.onesignal.inAppMessages.IInAppMessageWillDismissEvent; -import com.onesignal.inAppMessages.IInAppMessageDidDismissEvent; -import java.util.Collection; -import java.util.Map; - +import com.onesignal.inAppMessages.IInAppMessageWillDisplayEvent; import io.flutter.plugin.common.BinaryMessenger; 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 java.util.Collection; +import java.util.Map; +import org.json.JSONException; -public class OneSignalInAppMessages extends FlutterMessengerResponder implements MethodCallHandler, -IInAppMessageClickListener, IInAppMessageLifecycleListener{ +public class OneSignalInAppMessages extends FlutterMessengerResponder + implements MethodCallHandler, IInAppMessageClickListener, IInAppMessageLifecycleListener { static void registerWith(BinaryMessenger messenger) { OneSignalInAppMessages sharedInstance = new OneSignalInAppMessages(); sharedInstance.messenger = messenger; sharedInstance.channel = new MethodChannel(messenger, "OneSignal#inappmessages"); - sharedInstance.channel.setMethodCallHandler(sharedInstance); + sharedInstance.channel.setMethodCallHandler(sharedInstance); } @Override public void onMethodCall(MethodCall call, Result result) { - if (call.method.contentEquals("OneSignal#addTrigger")) - this.addTriggers(call, result); - else if (call.method.contentEquals("OneSignal#addTriggers")) - this.addTriggers(call, result); - else if (call.method.contentEquals("OneSignal#removeTrigger")) - this.removeTrigger(call, result); - else if (call.method.contentEquals("OneSignal#removeTriggers")) - this.removeTriggers(call, result); - else if (call.method.contentEquals("OneSignal#clearTriggers")) - this.clearTriggers(call, result); + if (call.method.contentEquals("OneSignal#addTrigger")) this.addTriggers(call, result); + else if (call.method.contentEquals("OneSignal#addTriggers")) this.addTriggers(call, result); + else if (call.method.contentEquals("OneSignal#removeTrigger")) this.removeTrigger(call, result); + else if (call.method.contentEquals("OneSignal#removeTriggers")) this.removeTriggers(call, result); + else if (call.method.contentEquals("OneSignal#clearTriggers")) this.clearTriggers(call, result); else if (call.method.contentEquals("OneSignal#arePaused")) replySuccess(result, OneSignal.getInAppMessages().getPaused()); - else if (call.method.contentEquals("OneSignal#paused")) - this.paused(call, result); - else if (call.method.contentEquals("OneSignal#lifecycleInit")) - this.lifecycleInit(result); - else - replyNotImplemented(result); + else if (call.method.contentEquals("OneSignal#paused")) this.paused(call, result); + else if (call.method.contentEquals("OneSignal#lifecycleInit")) this.lifecycleInit(result); + else replyNotImplemented(result); } private void addTriggers(MethodCall call, Result result) { @@ -60,7 +50,11 @@ private void addTriggers(MethodCall call, Result result) { OneSignal.getInAppMessages().addTriggers((Map) call.arguments); replySuccess(result, null); } catch (ClassCastException e) { - replyError(result, "OneSignal", "Add triggers failed with error: " + e.getMessage() + "\n" + e.getStackTrace(), null); + replyError( + result, + "OneSignal", + "Add triggers failed with error: " + e.getMessage() + "\n" + e.getStackTrace(), + null); } } @@ -76,7 +70,11 @@ private void removeTriggers(MethodCall call, Result result) { OneSignal.getInAppMessages().removeTriggers((Collection) call.arguments); replySuccess(result, null); } catch (ClassCastException e) { - replyError(result, "OneSignal", "Remove triggers for keys failed with error: " + e.getMessage() + "\n" + e.getStackTrace(), null); + replyError( + result, + "OneSignal", + "Remove triggers for keys failed with error: " + e.getMessage() + "\n" + e.getStackTrace(), + null); } } @@ -99,56 +97,74 @@ public void lifecycleInit(Result result) { @Override public void onClick(IInAppMessageClickEvent event) { try { - invokeMethodOnUiThread("OneSignal#onClickInAppMessage", OneSignalSerializer.convertInAppMessageClickEventToMap(event)); + invokeMethodOnUiThread( + "OneSignal#onClickInAppMessage", OneSignalSerializer.convertInAppMessageClickEventToMap(event)); } catch (JSONException e) { e.getStackTrace(); - Logging.error("Encountered an error attempting to convert IInAppMessageClickEvent object to hash map:" + e.toString(), null); - } + Logging.error( + "Encountered an error attempting to convert IInAppMessageClickEvent object to hash map:" + + e.toString(), + null); + } } @Override - public void onWillDisplay(IInAppMessageWillDisplayEvent event) { + public void onWillDisplay(IInAppMessageWillDisplayEvent event) { try { - invokeMethodOnUiThread("OneSignal#onWillDisplayInAppMessage", - OneSignalSerializer.convertInAppMessageWillDisplayEventToMap(event)); + invokeMethodOnUiThread( + "OneSignal#onWillDisplayInAppMessage", + OneSignalSerializer.convertInAppMessageWillDisplayEventToMap(event)); } catch (JSONException e) { e.getStackTrace(); - Logging.error("Encountered an error attempting to convert IInAppMessageWillDisplayEvent object to hash map:" + e.toString(), null); - } - + Logging.error( + "Encountered an error attempting to convert IInAppMessageWillDisplayEvent object to hash map:" + + e.toString(), + null); + } } @Override public void onDidDisplay(IInAppMessageDidDisplayEvent event) { try { - invokeMethodOnUiThread("OneSignal#onDidDisplayInAppMessage", - OneSignalSerializer.convertInAppMessageDidDisplayEventToMap(event)); + invokeMethodOnUiThread( + "OneSignal#onDidDisplayInAppMessage", + OneSignalSerializer.convertInAppMessageDidDisplayEventToMap(event)); } catch (JSONException e) { e.getStackTrace(); - Logging.error("Encountered an error attempting to convert IInAppMessageDidDisplayEvent object to hash map:" + e.toString(), null); + Logging.error( + "Encountered an error attempting to convert IInAppMessageDidDisplayEvent object to hash map:" + + e.toString(), + null); } } @Override public void onWillDismiss(IInAppMessageWillDismissEvent event) { try { - invokeMethodOnUiThread("OneSignal#onWillDismissInAppMessage", - OneSignalSerializer.convertInAppMessageWillDismissEventToMap(event)); + invokeMethodOnUiThread( + "OneSignal#onWillDismissInAppMessage", + OneSignalSerializer.convertInAppMessageWillDismissEventToMap(event)); } catch (JSONException e) { e.getStackTrace(); - Logging.error("Encountered an error attempting to convert IInAppMessageWillDismissEvent object to hash map:" + e.toString(), null); + Logging.error( + "Encountered an error attempting to convert IInAppMessageWillDismissEvent object to hash map:" + + e.toString(), + null); } } @Override public void onDidDismiss(IInAppMessageDidDismissEvent event) { try { - invokeMethodOnUiThread("OneSignal#onDidDismissInAppMessage", - OneSignalSerializer.convertInAppMessageDidDismissEventToMap(event)); + invokeMethodOnUiThread( + "OneSignal#onDidDismissInAppMessage", + OneSignalSerializer.convertInAppMessageDidDismissEventToMap(event)); } catch (JSONException e) { e.getStackTrace(); - Logging.error("Encountered an error attempting to convert IInAppMessageDidDismissEvent object to hash map:" + e.toString(), null); + Logging.error( + "Encountered an error attempting to convert IInAppMessageDidDismissEvent object to hash map:" + + e.toString(), + null); } - } } diff --git a/android/src/main/java/com/onesignal/flutter/OneSignalLocation.java b/android/src/main/java/com/onesignal/flutter/OneSignalLocation.java index 2433f384..e7549a01 100644 --- a/android/src/main/java/com/onesignal/flutter/OneSignalLocation.java +++ b/android/src/main/java/com/onesignal/flutter/OneSignalLocation.java @@ -1,8 +1,7 @@ package com.onesignal.flutter; -import com.onesignal.OneSignal; import com.onesignal.Continue; - +import com.onesignal.OneSignal; import io.flutter.plugin.common.BinaryMessenger; import io.flutter.plugin.common.MethodCall; import io.flutter.plugin.common.MethodChannel; @@ -20,21 +19,18 @@ static void registerWith(BinaryMessenger messenger) { @Override public void onMethodCall(MethodCall call, Result result) { - if (call.method.contentEquals("OneSignal#requestPermission")) - this.requestPermission(result); - else if (call.method.contentEquals("OneSignal#setShared")) - this.setShared(call, result); + if (call.method.contentEquals("OneSignal#requestPermission")) this.requestPermission(result); + else if (call.method.contentEquals("OneSignal#setShared")) this.setShared(call, result); else if (call.method.contentEquals("OneSignal#isShared")) replySuccess(result, OneSignal.getLocation().isShared()); - else - replyNotImplemented(result); + else replyNotImplemented(result); } private void requestPermission(Result reply) { OneSignal.getLocation().requestPermission(Continue.none()); replySuccess(reply, null); } - + private void setShared(MethodCall call, Result result) { OneSignal.getLocation().setShared((boolean) call.arguments); replySuccess(result, null); diff --git a/android/src/main/java/com/onesignal/flutter/OneSignalNotifications.java b/android/src/main/java/com/onesignal/flutter/OneSignalNotifications.java index b333e60d..869fb9a6 100644 --- a/android/src/main/java/com/onesignal/flutter/OneSignalNotifications.java +++ b/android/src/main/java/com/onesignal/flutter/OneSignalNotifications.java @@ -1,33 +1,29 @@ package com.onesignal.flutter; import androidx.annotation.NonNull; - -import com.onesignal.debug.internal.logging.Logging; import com.onesignal.OneSignal; - +import com.onesignal.debug.internal.logging.Logging; import com.onesignal.notifications.INotification; import com.onesignal.notifications.INotificationClickEvent; -import com.onesignal.notifications.INotificationWillDisplayEvent; import com.onesignal.notifications.INotificationClickListener; import com.onesignal.notifications.INotificationLifecycleListener; +import com.onesignal.notifications.INotificationWillDisplayEvent; import com.onesignal.notifications.IPermissionObserver; - -import org.json.JSONException; -import org.json.JSONObject; - -import java.util.HashMap; -import java.util.Map; - import io.flutter.plugin.common.BinaryMessenger; 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 java.util.HashMap; +import java.util.Map; import kotlin.coroutines.Continuation; import kotlin.coroutines.CoroutineContext; import kotlinx.coroutines.Dispatchers; +import org.json.JSONException; +import org.json.JSONObject; -public class OneSignalNotifications extends FlutterMessengerResponder implements MethodCallHandler, INotificationClickListener, INotificationLifecycleListener, IPermissionObserver { +public class OneSignalNotifications extends FlutterMessengerResponder + implements MethodCallHandler, INotificationClickListener, INotificationLifecycleListener, IPermissionObserver { private final HashMap notificationOnWillDisplayEventCache = new HashMap<>(); private final HashMap preventedDefaultCache = new HashMap<>(); @@ -53,9 +49,12 @@ public CoroutineContext getContext() { public void resumeWith(@NonNull Object o) { if (o instanceof kotlin.Result.Failure) { Throwable e = ((kotlin.Result.Failure) o).exception; - replyError(result, "OneSignal", "requestPermission failed with error: " + e.getMessage() + "\n" + e.getStackTrace(), null); - } - else { + replyError( + result, + "OneSignal", + "requestPermission failed with error: " + e.getMessage() + "\n" + e.getStackTrace(), + null); + } else { replySuccess(result, o); } } @@ -70,30 +69,22 @@ static void registerWith(BinaryMessenger messenger) { @Override public void onMethodCall(MethodCall call, Result result) { - if (call.method.contentEquals("OneSignal#permission")) - replySuccess(result, OneSignal.getNotifications().getPermission()); - else if (call.method.contentEquals("OneSignal#canRequest")) - replySuccess(result, OneSignal.getNotifications().getCanRequestPermission()); - else if (call.method.contentEquals("OneSignal#requestPermission")) - this.requestPermission(call, result); - else if (call.method.contentEquals("OneSignal#removeNotification")) - this.removeNotification(call, result); - else if (call.method.contentEquals("OneSignal#removeGroupedNotifications")) - this.removeGroupedNotifications(call, result); - else if (call.method.contentEquals("OneSignal#clearAll")) - this.clearAll(call, result); - else if (call.method.contentEquals("OneSignal#displayNotification")) - this.displayNotification(call, result); - else if (call.method.contentEquals("OneSignal#preventDefault")) - this.preventDefault(call, result); - else if (call.method.contentEquals("OneSignal#lifecycleInit")) - this.lifecycleInit(result); - else if (call.method.contentEquals("OneSignal#proceedWithWillDisplay")) - this.proceedWithWillDisplay(call, result); - else if (call.method.contentEquals("OneSignal#addNativeClickListener")) - this.registerClickListener(); - else - replyNotImplemented(result); + if (call.method.contentEquals("OneSignal#permission")) + replySuccess(result, OneSignal.getNotifications().getPermission()); + else if (call.method.contentEquals("OneSignal#canRequest")) + replySuccess(result, OneSignal.getNotifications().getCanRequestPermission()); + else if (call.method.contentEquals("OneSignal#requestPermission")) this.requestPermission(call, result); + else if (call.method.contentEquals("OneSignal#removeNotification")) this.removeNotification(call, result); + else if (call.method.contentEquals("OneSignal#removeGroupedNotifications")) + this.removeGroupedNotifications(call, result); + else if (call.method.contentEquals("OneSignal#clearAll")) this.clearAll(call, result); + else if (call.method.contentEquals("OneSignal#displayNotification")) this.displayNotification(call, result); + else if (call.method.contentEquals("OneSignal#preventDefault")) this.preventDefault(call, result); + else if (call.method.contentEquals("OneSignal#lifecycleInit")) this.lifecycleInit(result); + else if (call.method.contentEquals("OneSignal#proceedWithWillDisplay")) + this.proceedWithWillDisplay(call, result); + else if (call.method.contentEquals("OneSignal#addNativeClickListener")) this.registerClickListener(); + else replyNotImplemented(result); } private void requestPermission(MethodCall call, Result result) { @@ -110,14 +101,14 @@ private void requestPermission(MethodCall call, Result result) { private void removeNotification(MethodCall call, Result result) { int notificationId = call.argument("notificationId"); OneSignal.getNotifications().removeNotification(notificationId); - + replySuccess(result, null); } private void removeGroupedNotifications(MethodCall call, Result result) { String notificationGroup = call.argument("notificationGroup"); OneSignal.getNotifications().removeGroupedNotifications(notificationGroup); - + replySuccess(result, null); } @@ -126,14 +117,16 @@ private void clearAll(MethodCall call, Result result) { replySuccess(result, null); } - /// Our bridge layer needs to preventDefault() so that the Flutter listener has time to preventDefault() before the notification is displayed - /// This function is called after all of the flutter listeners have responded to the willDisplay event. + /// Our bridge layer needs to preventDefault() so that the Flutter listener has time to preventDefault() before the + // notification is displayed + /// This function is called after all of the flutter listeners have responded to the willDisplay event. /// If any of them have called preventDefault() we will not call display(). Otherwise we will display. private void proceedWithWillDisplay(MethodCall call, Result result) { String notificationId = call.argument("notificationId"); INotificationWillDisplayEvent event = notificationOnWillDisplayEventCache.get(notificationId); if (event == null) { - Logging.error("Could not find onWillDisplayNotification event for notification with id: " + notificationId, null); + Logging.error( + "Could not find onWillDisplayNotification event for notification with id: " + notificationId, null); return; } if (this.preventedDefaultCache.containsKey(notificationId)) { @@ -148,7 +141,8 @@ private void displayNotification(MethodCall call, Result result) { String notificationId = call.argument("notificationId"); INotificationWillDisplayEvent event = notificationOnWillDisplayEventCache.get(notificationId); if (event == null) { - Logging.error("Could not find onWillDisplayNotification event for notification with id: " + notificationId, null); + Logging.error( + "Could not find onWillDisplayNotification event for notification with id: " + notificationId, null); return; } event.getNotification().display(); @@ -159,7 +153,8 @@ private void preventDefault(MethodCall call, Result result) { String notificationId = call.argument("notificationId"); INotificationWillDisplayEvent event = notificationOnWillDisplayEventCache.get(notificationId); if (event == null) { - Logging.error("Could not find onWillDisplayNotification event for notification with id: " + notificationId, null); + Logging.error( + "Could not find onWillDisplayNotification event for notification with id: " + notificationId, null); return; } event.preventDefault(); @@ -170,10 +165,14 @@ private void preventDefault(MethodCall call, Result result) { @Override public void onClick(INotificationClickEvent event) { try { - invokeMethodOnUiThread("OneSignal#onClickNotification", OneSignalSerializer.convertNotificationClickEventToMap(event)); + invokeMethodOnUiThread( + "OneSignal#onClickNotification", OneSignalSerializer.convertNotificationClickEventToMap(event)); } catch (JSONException e) { e.getStackTrace(); - Logging.error("Encountered an error attempting to convert INotificationClickEvent object to hash map:" + e.toString(), null); + Logging.error( + "Encountered an error attempting to convert INotificationClickEvent object to hash map:" + + e.toString(), + null); } } @@ -193,18 +192,24 @@ private JSONObject getJsonFromMap(Map map) throws JSONException public void onWillDisplay(INotificationWillDisplayEvent event) { INotification notification = event.getNotification(); notificationOnWillDisplayEventCache.put(notification.getNotificationId(), event); - /// Our bridge layer needs to preventDefault() so that the Flutter listener has time to preventDefault() before the notification is displayed + /// Our bridge layer needs to preventDefault() so that the Flutter listener has time to preventDefault() before + // the notification is displayed event.preventDefault(); try { - invokeMethodOnUiThread("OneSignal#onWillDisplayNotification", OneSignalSerializer.convertNotificationWillDisplayEventToMap(event)); + invokeMethodOnUiThread( + "OneSignal#onWillDisplayNotification", + OneSignalSerializer.convertNotificationWillDisplayEventToMap(event)); } catch (JSONException e) { e.getStackTrace(); - Logging.error("Encountered an error attempting to convert INotificationWillDisplayEvent object to hash map:" + e.toString(), null); + Logging.error( + "Encountered an error attempting to convert INotificationWillDisplayEvent object to hash map:" + + e.toString(), + null); } } @Override - public void onNotificationPermissionChange(boolean permission) { + public void onNotificationPermissionChange(boolean permission) { HashMap hash = new HashMap<>(); hash.put("permission", permission); invokeMethodOnUiThread("OneSignal#onNotificationPermissionDidChange", hash); @@ -219,4 +224,4 @@ private void lifecycleInit(Result result) { private void registerClickListener() { OneSignal.getNotifications().addClickListener(this); } -} \ No newline at end of file +} diff --git a/android/src/main/java/com/onesignal/flutter/OneSignalPlugin.java b/android/src/main/java/com/onesignal/flutter/OneSignalPlugin.java index ba9df9a7..9d5ed6dd 100644 --- a/android/src/main/java/com/onesignal/flutter/OneSignalPlugin.java +++ b/android/src/main/java/com/onesignal/flutter/OneSignalPlugin.java @@ -1,15 +1,12 @@ package com.onesignal.flutter; import android.content.Context; - -import io.flutter.embedding.engine.plugins.activity.ActivityAware; -import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding; - +import androidx.annotation.NonNull; import com.onesignal.OneSignal; import com.onesignal.common.OneSignalWrapper; - -import androidx.annotation.NonNull; import io.flutter.embedding.engine.plugins.FlutterPlugin; +import io.flutter.embedding.engine.plugins.activity.ActivityAware; +import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding; import io.flutter.plugin.common.BinaryMessenger; import io.flutter.plugin.common.MethodCall; import io.flutter.plugin.common.MethodChannel; @@ -17,112 +14,97 @@ import io.flutter.plugin.common.MethodChannel.Result; /** OnesignalPlugin */ -public class OneSignalPlugin extends FlutterMessengerResponder implements FlutterPlugin, MethodCallHandler, ActivityAware { - - public OneSignalPlugin() { - } - - private void init(Context context, BinaryMessenger messenger) - { - this.context = context; - this.messenger = messenger; - OneSignalWrapper.setSdkType("flutter"); - // For 5.0.0, hard code to reflect SDK version - OneSignalWrapper.setSdkVersion("050305"); - - channel = new MethodChannel(messenger, "OneSignal"); - channel.setMethodCallHandler(this); - - OneSignalDebug.registerWith(messenger); - OneSignalLocation.registerWith(messenger); - OneSignalSession.registerWith(messenger); - OneSignalInAppMessages.registerWith(messenger); - OneSignalUser.registerWith(messenger); - OneSignalPushSubscription.registerWith(messenger); - OneSignalNotifications.registerWith(messenger); - } - - @Override - public void onAttachedToEngine(@NonNull FlutterPlugin.FlutterPluginBinding flutterPluginBinding) { - init( - flutterPluginBinding.getApplicationContext(), - flutterPluginBinding.getBinaryMessenger() - ); - } - - @Override - public void onDetachedFromEngine(@NonNull FlutterPlugin.FlutterPluginBinding binding) { - onDetachedFromEngine(); - } - - private void onDetachedFromEngine() { - } - - @Override - public void onAttachedToActivity(@NonNull ActivityPluginBinding binding) { - this.context = binding.getActivity(); - } - - @Override - public void onDetachedFromActivity() { - } - - @Override - public void onReattachedToActivityForConfigChanges(@NonNull ActivityPluginBinding binding) { - } - - @Override - public void onDetachedFromActivityForConfigChanges() { - } - - @Override - public void onMethodCall(MethodCall call, Result result) { - if (call.method.contentEquals("OneSignal#initialize")) - this.initWithContext(call, result); - else if (call.method.contentEquals("OneSignal#consentRequired")) - this.setConsentRequired(call, result); - else if (call.method.contentEquals("OneSignal#consentGiven")) - this.setConsentGiven(call, result); - else if (call.method.contentEquals("OneSignal#login")) - this.login(call, result); - else if (call.method.contentEquals("OneSignal#loginWithJWT")) - this.loginWithJWT(call, result); - else if (call.method.contentEquals("OneSignal#logout")) - this.logout(call, result); - else - replyNotImplemented(result); - } - - private void initWithContext(MethodCall call, Result reply) { - String appId = call.argument("appId"); - OneSignal.initWithContext(context, appId); - replySuccess(reply, null); - } - - private void setConsentRequired(MethodCall call, Result reply) { - boolean required = call.argument("required"); - OneSignal.setConsentRequired(required); - replySuccess(reply, null); - } - - private void setConsentGiven(MethodCall call, Result reply) { - boolean granted = call.argument("granted"); - OneSignal.setConsentGiven(granted); - replySuccess(reply, null); - } - - private void login(MethodCall call, Result result) { - OneSignal.login((String) call.argument("externalId")); - replySuccess(result, null); - } - - private void loginWithJWT(MethodCall call, Result result) { - OneSignal.login((String) call.argument("externalId"), (String) call.argument("jwt")); - replySuccess(result, null); - } - - private void logout(MethodCall call, Result result) { - OneSignal.logout(); - replySuccess(result, null); - } +public class OneSignalPlugin extends FlutterMessengerResponder + implements FlutterPlugin, MethodCallHandler, ActivityAware { + + public OneSignalPlugin() {} + + private void init(Context context, BinaryMessenger messenger) { + this.context = context; + this.messenger = messenger; + OneSignalWrapper.setSdkType("flutter"); + // For 5.0.0, hard code to reflect SDK version + OneSignalWrapper.setSdkVersion("050305"); + + channel = new MethodChannel(messenger, "OneSignal"); + channel.setMethodCallHandler(this); + + OneSignalDebug.registerWith(messenger); + OneSignalLocation.registerWith(messenger); + OneSignalSession.registerWith(messenger); + OneSignalInAppMessages.registerWith(messenger); + OneSignalUser.registerWith(messenger); + OneSignalPushSubscription.registerWith(messenger); + OneSignalNotifications.registerWith(messenger); + } + + @Override + public void onAttachedToEngine(@NonNull FlutterPlugin.FlutterPluginBinding flutterPluginBinding) { + init(flutterPluginBinding.getApplicationContext(), flutterPluginBinding.getBinaryMessenger()); + } + + @Override + public void onDetachedFromEngine(@NonNull FlutterPlugin.FlutterPluginBinding binding) { + onDetachedFromEngine(); + } + + private void onDetachedFromEngine() {} + + @Override + public void onAttachedToActivity(@NonNull ActivityPluginBinding binding) { + this.context = binding.getActivity(); + } + + @Override + public void onDetachedFromActivity() {} + + @Override + public void onReattachedToActivityForConfigChanges(@NonNull ActivityPluginBinding binding) {} + + @Override + public void onDetachedFromActivityForConfigChanges() {} + + @Override + public void onMethodCall(MethodCall call, Result result) { + if (call.method.contentEquals("OneSignal#initialize")) this.initWithContext(call, result); + else if (call.method.contentEquals("OneSignal#consentRequired")) this.setConsentRequired(call, result); + else if (call.method.contentEquals("OneSignal#consentGiven")) this.setConsentGiven(call, result); + else if (call.method.contentEquals("OneSignal#login")) this.login(call, result); + else if (call.method.contentEquals("OneSignal#loginWithJWT")) this.loginWithJWT(call, result); + else if (call.method.contentEquals("OneSignal#logout")) this.logout(call, result); + else replyNotImplemented(result); + } + + private void initWithContext(MethodCall call, Result reply) { + String appId = call.argument("appId"); + OneSignal.initWithContext(context, appId); + replySuccess(reply, null); + } + + private void setConsentRequired(MethodCall call, Result reply) { + boolean required = call.argument("required"); + OneSignal.setConsentRequired(required); + replySuccess(reply, null); + } + + private void setConsentGiven(MethodCall call, Result reply) { + boolean granted = call.argument("granted"); + OneSignal.setConsentGiven(granted); + replySuccess(reply, null); + } + + private void login(MethodCall call, Result result) { + OneSignal.login((String) call.argument("externalId")); + replySuccess(result, null); + } + + private void loginWithJWT(MethodCall call, Result result) { + OneSignal.login((String) call.argument("externalId"), (String) call.argument("jwt")); + replySuccess(result, null); + } + + private void logout(MethodCall call, Result result) { + OneSignal.logout(); + replySuccess(result, null); + } } diff --git a/android/src/main/java/com/onesignal/flutter/OneSignalPushSubscription.java b/android/src/main/java/com/onesignal/flutter/OneSignalPushSubscription.java index 3e8d23e5..c0b1ca0b 100644 --- a/android/src/main/java/com/onesignal/flutter/OneSignalPushSubscription.java +++ b/android/src/main/java/com/onesignal/flutter/OneSignalPushSubscription.java @@ -1,20 +1,18 @@ package com.onesignal.flutter; import com.onesignal.OneSignal; - +import com.onesignal.debug.internal.logging.Logging; import com.onesignal.user.subscriptions.IPushSubscriptionObserver; import com.onesignal.user.subscriptions.PushSubscriptionChangedState; -import com.onesignal.debug.internal.logging.Logging; - -import org.json.JSONException; - import io.flutter.plugin.common.BinaryMessenger; 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 org.json.JSONException; -public class OneSignalPushSubscription extends FlutterMessengerResponder implements MethodCallHandler, IPushSubscriptionObserver { +public class OneSignalPushSubscription extends FlutterMessengerResponder + implements MethodCallHandler, IPushSubscriptionObserver { static void registerWith(BinaryMessenger messenger) { OneSignalPushSubscription controller = new OneSignalPushSubscription(); @@ -25,26 +23,23 @@ static void registerWith(BinaryMessenger messenger) { @Override public void onMethodCall(MethodCall call, Result result) { - if (call.method.contentEquals("OneSignal#optIn")) - this.optIn(call, result); - else if (call.method.contentEquals("OneSignal#optOut")) - this.optOut(call, result); + if (call.method.contentEquals("OneSignal#optIn")) this.optIn(call, result); + else if (call.method.contentEquals("OneSignal#optOut")) this.optOut(call, result); else if (call.method.contentEquals("OneSignal#pushSubscriptionId")) replySuccess(result, OneSignal.getUser().getPushSubscription().getId()); else if (call.method.contentEquals("OneSignal#pushSubscriptionToken")) replySuccess(result, OneSignal.getUser().getPushSubscription().getToken()); else if (call.method.contentEquals("OneSignal#pushSubscriptionOptedIn")) replySuccess(result, OneSignal.getUser().getPushSubscription().getOptedIn()); - else if (call.method.contentEquals("OneSignal#lifecycleInit")) - this.lifecycleInit(result); - else - replyNotImplemented(result); + else if (call.method.contentEquals("OneSignal#lifecycleInit")) this.lifecycleInit(result); + else replyNotImplemented(result); } private void optIn(MethodCall call, Result reply) { OneSignal.getUser().getPushSubscription().optIn(); replySuccess(reply, null); } + private void optOut(MethodCall call, Result reply) { OneSignal.getUser().getPushSubscription().optOut(); replySuccess(reply, null); @@ -53,16 +48,20 @@ private void optOut(MethodCall call, Result reply) { private void lifecycleInit(Result result) { OneSignal.getUser().getPushSubscription().addObserver(this); replySuccess(result, null); - } + } @Override public void onPushSubscriptionChange(PushSubscriptionChangedState changeState) { try { - invokeMethodOnUiThread("OneSignal#onPushSubscriptionChange", OneSignalSerializer.convertOnPushSubscriptionChange(changeState)); + invokeMethodOnUiThread( + "OneSignal#onPushSubscriptionChange", + OneSignalSerializer.convertOnPushSubscriptionChange(changeState)); } catch (JSONException e) { e.getStackTrace(); - Logging.error("Encountered an error attempting to convert PushSubscriptionChangedState object to hash map:" + e.toString(), null); - } + Logging.error( + "Encountered an error attempting to convert PushSubscriptionChangedState object to hash map:" + + e.toString(), + null); + } } - -} \ No newline at end of file +} diff --git a/android/src/main/java/com/onesignal/flutter/OneSignalSerializer.java b/android/src/main/java/com/onesignal/flutter/OneSignalSerializer.java index 7adffb3d..19239da1 100644 --- a/android/src/main/java/com/onesignal/flutter/OneSignalSerializer.java +++ b/android/src/main/java/com/onesignal/flutter/OneSignalSerializer.java @@ -1,37 +1,34 @@ package com.onesignal.flutter; -import com.onesignal.user.state.UserChangedState; -import com.onesignal.user.state.UserState; -import com.onesignal.user.subscriptions.PushSubscriptionChangedState; -import com.onesignal.user.subscriptions.PushSubscriptionState; import com.onesignal.inAppMessages.IInAppMessage; -import com.onesignal.inAppMessages.IInAppMessageClickResult; import com.onesignal.inAppMessages.IInAppMessageClickEvent; -import com.onesignal.inAppMessages.IInAppMessageWillDisplayEvent; +import com.onesignal.inAppMessages.IInAppMessageClickResult; +import com.onesignal.inAppMessages.IInAppMessageDidDismissEvent; import com.onesignal.inAppMessages.IInAppMessageDidDisplayEvent; import com.onesignal.inAppMessages.IInAppMessageWillDismissEvent; -import com.onesignal.inAppMessages.IInAppMessageDidDismissEvent; -import com.onesignal.notifications.INotification; +import com.onesignal.inAppMessages.IInAppMessageWillDisplayEvent; import com.onesignal.notifications.IActionButton; -import com.onesignal.notifications.INotificationWillDisplayEvent; -import com.onesignal.notifications.INotificationClickResult; +import com.onesignal.notifications.INotification; import com.onesignal.notifications.INotificationClickEvent; - -import org.json.JSONArray; -import org.json.JSONException; -import org.json.JSONObject; - +import com.onesignal.notifications.INotificationClickResult; +import com.onesignal.notifications.INotificationWillDisplayEvent; +import com.onesignal.user.state.UserChangedState; +import com.onesignal.user.state.UserState; +import com.onesignal.user.subscriptions.PushSubscriptionChangedState; +import com.onesignal.user.subscriptions.PushSubscriptionState; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.List; +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; class OneSignalSerializer { static HashMap convertNotificationToMap(INotification notification) throws JSONException { HashMap hash = new HashMap<>(); - hash.put("androidNotificationId", notification.getAndroidNotificationId()); if (notification.getGroupedNotifications() != null) { @@ -43,37 +40,27 @@ static HashMap convertNotificationToMap(INotification notificati hash.put("notificationId", notification.getNotificationId()); hash.put("title", notification.getTitle()); - if (notification.getBody() != null) - hash.put("body", notification.getBody()); - if (notification.getSmallIcon() != null) - hash.put("smallIcon", notification.getSmallIcon()); - if (notification.getLargeIcon() != null) - hash.put("largeIcon", notification.getLargeIcon()); - if (notification.getBigPicture() != null) - hash.put("bigPicture", notification.getBigPicture()); + if (notification.getBody() != null) hash.put("body", notification.getBody()); + if (notification.getSmallIcon() != null) hash.put("smallIcon", notification.getSmallIcon()); + if (notification.getLargeIcon() != null) hash.put("largeIcon", notification.getLargeIcon()); + if (notification.getBigPicture() != null) hash.put("bigPicture", notification.getBigPicture()); if (notification.getSmallIconAccentColor() != null) hash.put("smallIconAccentColor", notification.getSmallIconAccentColor()); - if (notification.getLaunchURL() != null) - hash.put("launchUrl", notification.getLaunchURL()); - if (notification.getSound() != null) - hash.put("sound", notification.getSound()); - if (notification.getLedColor() != null) - hash.put("ledColor", notification.getLedColor()); + if (notification.getLaunchURL() != null) hash.put("launchUrl", notification.getLaunchURL()); + if (notification.getSound() != null) hash.put("sound", notification.getSound()); + if (notification.getLedColor() != null) hash.put("ledColor", notification.getLedColor()); hash.put("lockScreenVisibility", notification.getLockScreenVisibility()); - if (notification.getGroupKey() != null) - hash.put("groupKey", notification.getGroupKey()); - if (notification.getGroupMessage() != null) - hash.put("groupMessage", notification.getGroupMessage()); + if (notification.getGroupKey() != null) hash.put("groupKey", notification.getGroupKey()); + if (notification.getGroupMessage() != null) hash.put("groupMessage", notification.getGroupMessage()); if (notification.getFromProjectNumber() != null) hash.put("fromProjectNumber", notification.getFromProjectNumber()); - if (notification.getCollapseId() != null) - hash.put("collapseId", notification.getCollapseId()); + if (notification.getCollapseId() != null) hash.put("collapseId", notification.getCollapseId()); hash.put("priority", notification.getPriority()); - if (notification.getAdditionalData() != null && notification.getAdditionalData().length() > 0) + if (notification.getAdditionalData() != null + && notification.getAdditionalData().length() > 0) hash.put("additionalData", convertJSONObjectToHashMap(notification.getAdditionalData())); if (notification.getActionButtons() != null) { - hash.put("buttons", convertActionButtonsToMap - (notification.getActionButtons())); + hash.put("buttons", convertActionButtonsToMap(notification.getActionButtons())); } hash.put("rawPayload", notification.getRawPayload()); return hash; @@ -91,13 +78,15 @@ static List> convertActionButtonsToMap(List convertNotificationWillDisplayEventToMap(INotificationWillDisplayEvent event) throws JSONException { + static HashMap convertNotificationWillDisplayEventToMap(INotificationWillDisplayEvent event) + throws JSONException { HashMap hash = new HashMap<>(); - hash.put("notification", convertNotificationToMap(event.getNotification())); + hash.put("notification", convertNotificationToMap(event.getNotification())); return hash; } - private static HashMap convertNotificationClickResultToMap(INotificationClickResult result) throws JSONException { + private static HashMap convertNotificationClickResultToMap(INotificationClickResult result) + throws JSONException { HashMap hash = new HashMap<>(); hash.put("action_id", result.getActionId()); @@ -106,7 +95,8 @@ private static HashMap convertNotificationClickResultToMap(INoti return hash; } - static HashMap convertNotificationClickEventToMap(INotificationClickEvent event) throws JSONException { + static HashMap convertNotificationClickEventToMap(INotificationClickEvent event) + throws JSONException { HashMap hash = new HashMap<>(); hash.put("notification", convertNotificationToMap(event.getNotification())); @@ -115,7 +105,8 @@ static HashMap convertNotificationClickEventToMap(INotificationC return hash; } - static HashMap convertInAppMessageClickEventToMap(IInAppMessageClickEvent event) throws JSONException { + static HashMap convertInAppMessageClickEventToMap(IInAppMessageClickEvent event) + throws JSONException { HashMap hash = new HashMap<>(); hash.put("message", convertInAppMessageToMap(event.getMessage())); @@ -124,7 +115,8 @@ static HashMap convertInAppMessageClickEventToMap(IInAppMessageC return hash; } - static HashMap convertInAppMessageClickResultToMap(IInAppMessageClickResult result) throws JSONException { + static HashMap convertInAppMessageClickResultToMap(IInAppMessageClickResult result) + throws JSONException { HashMap hash = new HashMap<>(); hash.put("action_id", result.getActionId()); @@ -134,7 +126,8 @@ static HashMap convertInAppMessageClickResultToMap(IInAppMessage return hash; } - static HashMap convertInAppMessageWillDisplayEventToMap(IInAppMessageWillDisplayEvent event) throws JSONException { + static HashMap convertInAppMessageWillDisplayEventToMap(IInAppMessageWillDisplayEvent event) + throws JSONException { HashMap hash = new HashMap<>(); hash.put("message", convertInAppMessageToMap(event.getMessage())); @@ -142,7 +135,8 @@ static HashMap convertInAppMessageWillDisplayEventToMap(IInAppMe return hash; } - static HashMap convertInAppMessageDidDisplayEventToMap(IInAppMessageDidDisplayEvent event) throws JSONException { + static HashMap convertInAppMessageDidDisplayEventToMap(IInAppMessageDidDisplayEvent event) + throws JSONException { HashMap hash = new HashMap<>(); hash.put("message", convertInAppMessageToMap(event.getMessage())); @@ -150,7 +144,8 @@ static HashMap convertInAppMessageDidDisplayEventToMap(IInAppMes return hash; } - static HashMap convertInAppMessageWillDismissEventToMap(IInAppMessageWillDismissEvent event) throws JSONException { + static HashMap convertInAppMessageWillDismissEventToMap(IInAppMessageWillDismissEvent event) + throws JSONException { HashMap hash = new HashMap<>(); hash.put("message", convertInAppMessageToMap(event.getMessage())); @@ -158,7 +153,8 @@ static HashMap convertInAppMessageWillDismissEventToMap(IInAppMe return hash; } - static HashMap convertInAppMessageDidDismissEventToMap(IInAppMessageDidDismissEvent event) throws JSONException { + static HashMap convertInAppMessageDidDismissEventToMap(IInAppMessageDidDismissEvent event) + throws JSONException { HashMap hash = new HashMap<>(); hash.put("message", convertInAppMessageToMap(event.getMessage())); @@ -176,7 +172,6 @@ static HashMap convertInAppMessageToMap(IInAppMessage message) { static HashMap convertPushSubscriptionState(PushSubscriptionState state) throws JSONException { HashMap hash = new HashMap<>(); - hash.put("token", state.getToken()); hash.put("id", state.getId()); @@ -187,19 +182,19 @@ static HashMap convertPushSubscriptionState(PushSubscriptionStat static HashMap convertUserState(UserState state) throws JSONException { HashMap hash = new HashMap<>(); - + String onesignalId = setNullIfEmpty(state.getOnesignalId()); String externalId = setNullIfEmpty(state.getExternalId()); - + hash.put("onesignalId", onesignalId); hash.put("externalId", externalId); - + return hash; } - static HashMap convertOnPushSubscriptionChange(PushSubscriptionChangedState changedState) throws JSONException { + static HashMap convertOnPushSubscriptionChange(PushSubscriptionChangedState changedState) + throws JSONException { HashMap hash = new HashMap<>(); - hash.put("current", convertPushSubscriptionState(changedState.getCurrent())); hash.put("previous", convertPushSubscriptionState(changedState.getPrevious())); @@ -210,32 +205,29 @@ static HashMap convertOnPushSubscriptionChange(PushSubscriptionC static HashMap convertOnUserStateChange(UserChangedState changedState) throws JSONException { HashMap hash = new HashMap<>(); - hash.put("current", convertUserState(changedState.getCurrent())); - + return hash; } static HashMap convertJSONObjectToHashMap(JSONObject object) throws JSONException { HashMap hash = new HashMap<>(); - if (object == null || object == JSONObject.NULL) - return hash; + if (object == null || object == JSONObject.NULL) return hash; Iterator keys = object.keys(); while (keys.hasNext()) { String key = keys.next(); - if (object.isNull(key)) - continue; + if (object.isNull(key)) continue; Object val = object.get(key); if (val instanceof JSONArray) { - val = convertJSONArrayToList((JSONArray)val); + val = convertJSONArrayToList((JSONArray) val); } else if (val instanceof JSONObject) { - val = convertJSONObjectToHashMap((JSONObject)val); + val = convertJSONObjectToHashMap((JSONObject) val); } hash.put(key, val); @@ -250,10 +242,8 @@ private static List convertJSONArrayToList(JSONArray array) throws JSONE for (int i = 0; i < array.length(); i++) { Object val = array.get(i); - if (val instanceof JSONArray) - val = OneSignalSerializer.convertJSONArrayToList((JSONArray)val); - else if (val instanceof JSONObject) - val = convertJSONObjectToHashMap((JSONObject)val); + if (val instanceof JSONArray) val = OneSignalSerializer.convertJSONArrayToList((JSONArray) val); + else if (val instanceof JSONObject) val = convertJSONObjectToHashMap((JSONObject) val); list.add(val); } diff --git a/android/src/main/java/com/onesignal/flutter/OneSignalSession.java b/android/src/main/java/com/onesignal/flutter/OneSignalSession.java index b0e0a909..19d1ce49 100644 --- a/android/src/main/java/com/onesignal/flutter/OneSignalSession.java +++ b/android/src/main/java/com/onesignal/flutter/OneSignalSession.java @@ -1,7 +1,6 @@ package com.onesignal.flutter; import com.onesignal.OneSignal; - import io.flutter.plugin.common.BinaryMessenger; import io.flutter.plugin.common.MethodCall; import io.flutter.plugin.common.MethodChannel; @@ -19,14 +18,10 @@ static void registerWith(BinaryMessenger messenger) { @Override public void onMethodCall(MethodCall call, Result result) { - if (call.method.contentEquals("OneSignal#addOutcome")) - this.addOutcome(call, result); - else if (call.method.contentEquals("OneSignal#addUniqueOutcome")) - this.addUniqueOutcome(call, result); - else if (call.method.contentEquals("OneSignal#addOutcomeWithValue")) - this.addOutcomeWithValue(call, result); - else - replyNotImplemented(result); + if (call.method.contentEquals("OneSignal#addOutcome")) this.addOutcome(call, result); + else if (call.method.contentEquals("OneSignal#addUniqueOutcome")) this.addUniqueOutcome(call, result); + else if (call.method.contentEquals("OneSignal#addOutcomeWithValue")) this.addOutcomeWithValue(call, result); + else replyNotImplemented(result); } private void addOutcome(MethodCall call, Result result) { @@ -70,5 +65,4 @@ private void addOutcomeWithValue(MethodCall call, Result result) { OneSignal.getSession().addOutcomeWithValue(name, value.floatValue()); replySuccess(result, null); } - } diff --git a/android/src/main/java/com/onesignal/flutter/OneSignalUser.java b/android/src/main/java/com/onesignal/flutter/OneSignalUser.java index ed072840..835aed50 100644 --- a/android/src/main/java/com/onesignal/flutter/OneSignalUser.java +++ b/android/src/main/java/com/onesignal/flutter/OneSignalUser.java @@ -4,20 +4,16 @@ import com.onesignal.debug.internal.logging.Logging; import com.onesignal.user.state.IUserStateObserver; import com.onesignal.user.state.UserChangedState; - -import org.json.JSONException; - -import java.util.List; -import java.util.Map; - import io.flutter.plugin.common.BinaryMessenger; 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 java.util.List; +import java.util.Map; +import org.json.JSONException; -public class OneSignalUser extends FlutterMessengerResponder - implements MethodCallHandler, IUserStateObserver { +public class OneSignalUser extends FlutterMessengerResponder implements MethodCallHandler, IUserStateObserver { static void registerWith(BinaryMessenger messenger) { OneSignalUser controller = new OneSignalUser(); @@ -28,34 +24,20 @@ static void registerWith(BinaryMessenger messenger) { @Override public void onMethodCall(MethodCall call, Result result) { - if (call.method.contentEquals("OneSignal#setLanguage")) - this.setLanguage(call, result); - else if (call.method.contentEquals("OneSignal#getOnesignalId")) - this.getOnesignalId(call, result); - else if (call.method.contentEquals("OneSignal#getExternalId")) - this.getExternalId(call, result); - else if (call.method.contentEquals("OneSignal#addAliases")) - this.addAliases(call, result); - else if (call.method.contentEquals("OneSignal#removeAliases")) - this.removeAliases(call, result); - else if (call.method.contentEquals("OneSignal#addEmail")) - this.addEmail(call, result); - else if (call.method.contentEquals("OneSignal#removeEmail")) - this.removeEmail(call, result); - else if (call.method.contentEquals("OneSignal#addSms")) - this.addSms(call, result); - else if (call.method.contentEquals("OneSignal#removeSms")) - this.removeSms(call, result); - else if (call.method.contentEquals("OneSignal#addTags")) - this.addTags(call, result); - else if (call.method.contentEquals("OneSignal#removeTags")) - this.removeTags(call, result); - else if (call.method.contentEquals("OneSignal#getTags")) - this.getTags(call, result); - else if (call.method.contentEquals("OneSignal#lifecycleInit")) - this.lifecycleInit(result); - else - replyNotImplemented(result); + if (call.method.contentEquals("OneSignal#setLanguage")) this.setLanguage(call, result); + else if (call.method.contentEquals("OneSignal#getOnesignalId")) this.getOnesignalId(call, result); + else if (call.method.contentEquals("OneSignal#getExternalId")) this.getExternalId(call, result); + else if (call.method.contentEquals("OneSignal#addAliases")) this.addAliases(call, result); + else if (call.method.contentEquals("OneSignal#removeAliases")) this.removeAliases(call, result); + else if (call.method.contentEquals("OneSignal#addEmail")) this.addEmail(call, result); + else if (call.method.contentEquals("OneSignal#removeEmail")) this.removeEmail(call, result); + else if (call.method.contentEquals("OneSignal#addSms")) this.addSms(call, result); + else if (call.method.contentEquals("OneSignal#removeSms")) this.removeSms(call, result); + else if (call.method.contentEquals("OneSignal#addTags")) this.addTags(call, result); + else if (call.method.contentEquals("OneSignal#removeTags")) this.removeTags(call, result); + else if (call.method.contentEquals("OneSignal#getTags")) this.getTags(call, result); + else if (call.method.contentEquals("OneSignal#lifecycleInit")) this.lifecycleInit(result); + else replyNotImplemented(result); } private void setLanguage(MethodCall call, Result result) { @@ -95,7 +77,9 @@ private void addAliases(MethodCall call, Result result) { OneSignal.getUser().addAliases((Map) call.arguments); replySuccess(result, null); } catch (ClassCastException e) { - replyError(result, "OneSignal", + replyError( + result, + "OneSignal", "addAliases failed with error: " + e.getMessage() + "\n" + e.getStackTrace(), null); } @@ -108,7 +92,9 @@ private void removeAliases(MethodCall call, Result result) { OneSignal.getUser().removeAliases((List) call.arguments); replySuccess(result, null); } catch (ClassCastException e) { - replyError(result, "OneSignal", + replyError( + result, + "OneSignal", "removeAliases failed with error: " + e.getMessage() + "\n" + e.getStackTrace(), null); } @@ -141,7 +127,9 @@ private void addTags(MethodCall call, Result result) { OneSignal.getUser().addTags((Map) call.arguments); replySuccess(result, null); } catch (ClassCastException e) { - replyError(result, "OneSignal", + replyError( + result, + "OneSignal", "addTags failed with error: " + e.getMessage() + "\n" + e.getStackTrace(), null); } @@ -154,7 +142,9 @@ private void removeTags(MethodCall call, Result result) { OneSignal.getUser().removeTags((List) call.arguments); replySuccess(result, null); } catch (ClassCastException e) { - replyError(result, "OneSignal", + replyError( + result, + "OneSignal", "deleteTags failed with error: " + e.getMessage() + "\n" + e.getStackTrace(), null); } @@ -167,13 +157,12 @@ private void getTags(MethodCall call, Result result) { @Override public void onUserStateChange(UserChangedState userChangedState) { try { - invokeMethodOnUiThread("OneSignal#onUserStateChange", - OneSignalSerializer.convertOnUserStateChange(userChangedState)); + invokeMethodOnUiThread( + "OneSignal#onUserStateChange", OneSignalSerializer.convertOnUserStateChange(userChangedState)); } catch (JSONException e) { e.getStackTrace(); Logging.error( - "Encountered an error attempting to convert UserChangedState object to hash map:" - + e.toString(), + "Encountered an error attempting to convert UserChangedState object to hash map:" + e.toString(), null); } } diff --git a/build.gradle b/build.gradle new file mode 100644 index 00000000..e18284bc --- /dev/null +++ b/build.gradle @@ -0,0 +1,43 @@ +buildscript { + repositories { + google() + mavenCentral() + } + dependencies { + classpath 'com.diffplug.spotless:spotless-plugin-gradle:6.25.0' + } +} + +repositories { + google() + mavenCentral() +} + +apply plugin: 'com.diffplug.spotless' + +spotless { + java { + target 'android/**/*.java', 'example/android/app/**/*.java' + targetExclude '**/build/**', '**/GeneratedPluginRegistrant.java' + palantirJavaFormat('2.28.0') + removeUnusedImports() + trimTrailingWhitespace() + endWithNewline() + } + + cpp { + target 'ios/**/*.m', 'ios/**/*.h' + def clangVersion = "clang-format --version".execute().text.trim().split(" ")[2] + clangFormat(clangVersion) + trimTrailingWhitespace() + endWithNewline() + } + + format 'xml', { + target '**/*.xml' + trimTrailingWhitespace() + indentWithSpaces(4) + endWithNewline() + } +} + diff --git a/ios/Classes/OSFlutterCategories.h b/ios/Classes/OSFlutterCategories.h index 289e70d2..329e80b8 100644 --- a/ios/Classes/OSFlutterCategories.h +++ b/ios/Classes/OSFlutterCategories.h @@ -13,8 +13,8 @@ * 1. The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * - * 2. All copies of substantial portions of the Software may only be used in connection - * with services provided by OneSignal. + * 2. All copies of substantial portions of the Software may only be used in + * connection with services provided by OneSignal. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, diff --git a/ios/Classes/OSFlutterCategories.m b/ios/Classes/OSFlutterCategories.m index 942dcd6f..db3ac0ec 100644 --- a/ios/Classes/OSFlutterCategories.m +++ b/ios/Classes/OSFlutterCategories.m @@ -13,8 +13,8 @@ * 1. The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * - * 2. All copies of substantial portions of the Software may only be used in connection - * with services provided by OneSignal. + * 2. All copies of substantial portions of the Software may only be used in + * connection with services provided by OneSignal. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, @@ -39,147 +39,168 @@ The OneSignal iOS SDK implements similar methods (`toDictionary`) @implementation OSNotification (Flutter) - (NSDictionary *)toJson { - NSMutableDictionary *json = [NSMutableDictionary new]; - - json[@"contentAvailable"] = @(self.contentAvailable); - json[@"mutableContent"] = @(self.mutableContent); - - if (self.rawPayload) { - NSError *jsonError; - NSData *data = [NSJSONSerialization dataWithJSONObject:self.rawPayload options:NSJSONWritingPrettyPrinted error:&jsonError]; - - if (!jsonError) { - NSString *rawPayloadString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; - json[@"rawPayload"] = rawPayloadString; - } + NSMutableDictionary *json = [NSMutableDictionary new]; + + json[@"contentAvailable"] = @(self.contentAvailable); + json[@"mutableContent"] = @(self.mutableContent); + + if (self.rawPayload) { + NSError *jsonError; + NSData *data = + [NSJSONSerialization dataWithJSONObject:self.rawPayload + options:NSJSONWritingPrettyPrinted + error:&jsonError]; + + if (!jsonError) { + NSString *rawPayloadString = + [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; + json[@"rawPayload"] = rawPayloadString; } - - if (self.notificationId) json[@"notificationId"] = self.notificationId; - if (self.templateName) json[@"templateName"] = self.templateName; - if (self.templateId) json[@"templateId"] = self.templateId; - if (self.badge) json[@"badge"] = @(self.badge); - if (self.badgeIncrement) json[@"badgeIncrement"] = @(self.badgeIncrement); - if (self.sound) json[@"sound"] = self.sound; - if (self.title) json[@"title"] = self.title; - if (self.subtitle) json[@"subtitle"] = self.subtitle; - if (self.body) json[@"body"] = self.body; - if (self.launchURL) json[@"launchUrl"] = self.launchURL; - if (self.additionalData) json[@"additionalData"] = self.additionalData; - if (self.attachments) json[@"attachments"] = self.attachments; - if (self.actionButtons) json[@"buttons"] = self.actionButtons; - if (self.category) json[@"category"] = self.category; - - return json; + } + + if (self.notificationId) + json[@"notificationId"] = self.notificationId; + if (self.templateName) + json[@"templateName"] = self.templateName; + if (self.templateId) + json[@"templateId"] = self.templateId; + if (self.badge) + json[@"badge"] = @(self.badge); + if (self.badgeIncrement) + json[@"badgeIncrement"] = @(self.badgeIncrement); + if (self.sound) + json[@"sound"] = self.sound; + if (self.title) + json[@"title"] = self.title; + if (self.subtitle) + json[@"subtitle"] = self.subtitle; + if (self.body) + json[@"body"] = self.body; + if (self.launchURL) + json[@"launchUrl"] = self.launchURL; + if (self.additionalData) + json[@"additionalData"] = self.additionalData; + if (self.attachments) + json[@"attachments"] = self.attachments; + if (self.actionButtons) + json[@"buttons"] = self.actionButtons; + if (self.category) + json[@"category"] = self.category; + + return json; } @end @implementation OSNotificationClickEvent (Flutter) - (NSDictionary *)toJson { - NSMutableDictionary *json = [NSMutableDictionary new]; + NSMutableDictionary *json = [NSMutableDictionary new]; - json[@"notification"] = self.notification.toJson; - json[@"result"] = self.result.toJson; + json[@"notification"] = self.notification.toJson; + json[@"result"] = self.result.toJson; - return json; + return json; } @end @implementation OSNotificationClickResult (Flutter) - (NSDictionary *)toJson { - NSMutableDictionary *json = [NSMutableDictionary new]; + NSMutableDictionary *json = [NSMutableDictionary new]; - json[@"action_id"] = self.actionId; - json[@"url"] = self.url; + json[@"action_id"] = self.actionId; + json[@"url"] = self.url; - return json; + return json; } @end @implementation OSNotificationWillDisplayEvent (Flutter) - (NSDictionary *)toJson { - NSMutableDictionary *json = [NSMutableDictionary new]; + NSMutableDictionary *json = [NSMutableDictionary new]; - json[@"notification"] = self.notification.toJson; + json[@"notification"] = self.notification.toJson; - return json; + return json; } @end @implementation OSInAppMessageWillDisplayEvent (Flutter) - (NSDictionary *)toJson { - NSMutableDictionary *json = [NSMutableDictionary new]; + NSMutableDictionary *json = [NSMutableDictionary new]; - json[@"message"] = self.message.toJson; + json[@"message"] = self.message.toJson; - return json; + return json; } @end @implementation OSInAppMessageDidDisplayEvent (Flutter) - (NSDictionary *)toJson { - NSMutableDictionary *json = [NSMutableDictionary new]; + NSMutableDictionary *json = [NSMutableDictionary new]; - json[@"message"] = self.message.toJson; + json[@"message"] = self.message.toJson; - return json; + return json; } @end @implementation OSInAppMessageWillDismissEvent (Flutter) - (NSDictionary *)toJson { - NSMutableDictionary *json = [NSMutableDictionary new]; + NSMutableDictionary *json = [NSMutableDictionary new]; - json[@"message"] = self.message.toJson; + json[@"message"] = self.message.toJson; - return json; + return json; } @end @implementation OSInAppMessageDidDismissEvent (Flutter) - (NSDictionary *)toJson { - NSMutableDictionary *json = [NSMutableDictionary new]; + NSMutableDictionary *json = [NSMutableDictionary new]; - json[@"message"] = self.message.toJson; + json[@"message"] = self.message.toJson; - return json; + return json; } @end @implementation OSInAppMessageClickEvent (Flutter) - (NSDictionary *)toJson { - NSMutableDictionary *json = [NSMutableDictionary new]; + NSMutableDictionary *json = [NSMutableDictionary new]; - json[@"message"] = self.message.toJson; - json[@"result"] = self.result.toJson; + json[@"message"] = self.message.toJson; + json[@"result"] = self.result.toJson; - return json; + return json; } @end @implementation OSInAppMessageClickResult (Flutter) - (NSDictionary *)toJson { - NSMutableDictionary *json = [NSMutableDictionary new]; + NSMutableDictionary *json = [NSMutableDictionary new]; - json[@"action_id"] = self.actionId; - json[@"url"] = self.url; - json[@"closing_message"] = @(self.closingMessage); + json[@"action_id"] = self.actionId; + json[@"url"] = self.url; + json[@"closing_message"] = @(self.closingMessage); - return json; + return json; } @end @implementation OSInAppMessage (Flutter) - (NSDictionary *)toJson { - NSMutableDictionary *json = [NSMutableDictionary new]; + NSMutableDictionary *json = [NSMutableDictionary new]; - json[@"message_id"] = self.messageId; + json[@"message_id"] = self.messageId; - return json; + return json; } @end @implementation NSError (Flutter) - (FlutterError *)flutterError { - return [FlutterError errorWithCode:[NSString stringWithFormat:@"%i", (int)self.code] message:self.localizedDescription details:nil]; + return [FlutterError + errorWithCode:[NSString stringWithFormat:@"%i", (int)self.code] + message:self.localizedDescription + details:nil]; } @end diff --git a/ios/Classes/OSFlutterDebug.h b/ios/Classes/OSFlutterDebug.h index 18fa73e9..cbeeb147 100644 --- a/ios/Classes/OSFlutterDebug.h +++ b/ios/Classes/OSFlutterDebug.h @@ -13,8 +13,8 @@ * 1. The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * - * 2. All copies of substantial portions of the Software may only be used in connection - * with services provided by OneSignal. + * 2. All copies of substantial portions of the Software may only be used in + * connection with services provided by OneSignal. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, @@ -25,12 +25,11 @@ * THE SOFTWARE. */ - -#import #import +#import -@interface OSFlutterDebug : NSObject +@interface OSFlutterDebug : NSObject -@property (strong, nonatomic) FlutterMethodChannel *channel; +@property(strong, nonatomic) FlutterMethodChannel *channel; @end diff --git a/ios/Classes/OSFlutterDebug.m b/ios/Classes/OSFlutterDebug.m index 87c877b5..f443157a 100644 --- a/ios/Classes/OSFlutterDebug.m +++ b/ios/Classes/OSFlutterDebug.m @@ -13,8 +13,8 @@ * 1. The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * - * 2. All copies of substantial portions of the Software may only be used in connection - * with services provided by OneSignal. + * 2. All copies of substantial portions of the Software may only be used in + * connection with services provided by OneSignal. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, @@ -26,39 +26,43 @@ */ #import "OSFlutterDebug.h" -#import #import "OSFlutterCategories.h" +#import @implementation OSFlutterDebug -+ (void)registerWithRegistrar:(NSObject*)registrar { - OSFlutterDebug *instance = [OSFlutterDebug new]; ++ (void)registerWithRegistrar:(NSObject *)registrar { + OSFlutterDebug *instance = [OSFlutterDebug new]; - instance.channel = [FlutterMethodChannel - methodChannelWithName:@"OneSignal#debug" - binaryMessenger:[registrar messenger]]; + instance.channel = + [FlutterMethodChannel methodChannelWithName:@"OneSignal#debug" + binaryMessenger:[registrar messenger]]; - [registrar addMethodCallDelegate:instance channel:instance.channel]; + [registrar addMethodCallDelegate:instance channel:instance.channel]; } -- (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result { - if ([@"OneSignal#setLogLevel" isEqualToString:call.method]) - [self setLogLevel:call withResult:result]; - else if ([@"OneSignal#setAlertLevel" isEqualToString:call.method]) - [self setAlertLevel:call withResult:result]; - else - result(FlutterMethodNotImplemented); +- (void)handleMethodCall:(FlutterMethodCall *)call + result:(FlutterResult)result { + if ([@"OneSignal#setLogLevel" isEqualToString:call.method]) + [self setLogLevel:call withResult:result]; + else if ([@"OneSignal#setAlertLevel" isEqualToString:call.method]) + [self setAlertLevel:call withResult:result]; + else + result(FlutterMethodNotImplemented); } -- (void)setLogLevel:(FlutterMethodCall *)call withResult:(FlutterResult)result{ - ONE_S_LOG_LEVEL logLevel = (ONE_S_LOG_LEVEL)[call.arguments[@"logLevel"] intValue]; - [OneSignal.Debug setLogLevel:logLevel]; - result(nil); +- (void)setLogLevel:(FlutterMethodCall *)call withResult:(FlutterResult)result { + ONE_S_LOG_LEVEL logLevel = + (ONE_S_LOG_LEVEL)[call.arguments[@"logLevel"] intValue]; + [OneSignal.Debug setLogLevel:logLevel]; + result(nil); } -- (void)setAlertLevel:(FlutterMethodCall *)call withResult:(FlutterResult)result{ - ONE_S_LOG_LEVEL visualLogLevel = (ONE_S_LOG_LEVEL)[call.arguments[@"visualLevel"] intValue]; - [OneSignal.Debug setAlertLevel:visualLogLevel]; - result(nil); +- (void)setAlertLevel:(FlutterMethodCall *)call + withResult:(FlutterResult)result { + ONE_S_LOG_LEVEL visualLogLevel = + (ONE_S_LOG_LEVEL)[call.arguments[@"visualLevel"] intValue]; + [OneSignal.Debug setAlertLevel:visualLogLevel]; + result(nil); } @end diff --git a/ios/Classes/OSFlutterInAppMessages.h b/ios/Classes/OSFlutterInAppMessages.h index 0afb8ce1..2ce71e51 100644 --- a/ios/Classes/OSFlutterInAppMessages.h +++ b/ios/Classes/OSFlutterInAppMessages.h @@ -13,8 +13,8 @@ * 1. The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * - * 2. All copies of substantial portions of the Software may only be used in connection - * with services provided by OneSignal. + * 2. All copies of substantial portions of the Software may only be used in + * connection with services provided by OneSignal. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, @@ -25,14 +25,14 @@ * THE SOFTWARE. */ - -#import #import +#import #import -@interface OSFlutterInAppMessages : NSObject +@interface OSFlutterInAppMessages + : NSObject -@property (strong, nonatomic) FlutterMethodChannel *channel; +@property(strong, nonatomic) FlutterMethodChannel *channel; + (instancetype)sharedInstance; @end diff --git a/ios/Classes/OSFlutterInAppMessages.m b/ios/Classes/OSFlutterInAppMessages.m index 397a736e..a8f538a3 100644 --- a/ios/Classes/OSFlutterInAppMessages.m +++ b/ios/Classes/OSFlutterInAppMessages.m @@ -13,8 +13,8 @@ * 1. The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * - * 2. All copies of substantial portions of the Software may only be used in connection - * with services provided by OneSignal. + * 2. All copies of substantial portions of the Software may only be used in + * connection with services provided by OneSignal. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, @@ -26,108 +26,119 @@ */ #import "OSFlutterInAppMessages.h" -#import #import "OSFlutterCategories.h" +#import @implementation OSFlutterInAppMessages + (instancetype)sharedInstance { - static OSFlutterInAppMessages *sharedInstance = nil; - static dispatch_once_t onceToken; - dispatch_once(&onceToken, ^{ - sharedInstance = [OSFlutterInAppMessages new]; - }); - return sharedInstance; + static OSFlutterInAppMessages *sharedInstance = nil; + static dispatch_once_t onceToken; + dispatch_once(&onceToken, ^{ + sharedInstance = [OSFlutterInAppMessages new]; + }); + return sharedInstance; } -+ (void)registerWithRegistrar:(NSObject*)registrar { - - OSFlutterInAppMessages.sharedInstance.channel = [FlutterMethodChannel - methodChannelWithName:@"OneSignal#inappmessages" - binaryMessenger:[registrar messenger]]; ++ (void)registerWithRegistrar:(NSObject *)registrar { + + OSFlutterInAppMessages.sharedInstance.channel = + [FlutterMethodChannel methodChannelWithName:@"OneSignal#inappmessages" + binaryMessenger:[registrar messenger]]; - [registrar addMethodCallDelegate:OSFlutterInAppMessages.sharedInstance channel:OSFlutterInAppMessages.sharedInstance.channel]; + [registrar + addMethodCallDelegate:OSFlutterInAppMessages.sharedInstance + channel:OSFlutterInAppMessages.sharedInstance.channel]; } -- (void)handleMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result { - if ([@"OneSignal#addTrigger" isEqualToString:call.method]) - [self addTriggers:call withResult:result]; - else if ([@"OneSignal#addTriggers" isEqualToString:call.method]) - [self addTriggers:call withResult:result]; - else if ([@"OneSignal#removeTrigger" isEqualToString:call.method]) - [self removeTrigger:call withResult:result]; - else if ([@"OneSignal#removeTriggers" isEqualToString:call.method]) - [self removeTriggers:call withResult:result]; - else if ([@"OneSignal#clearTriggers" isEqualToString:call.method]) - [self clearTriggers:call withResult:result]; - else if ([@"OneSignal#paused" isEqualToString:call.method]) - [self paused:call withResult:result]; - else if ([@"OneSignal#arePaused" isEqualToString:call.method]) - result(@([OneSignal.InAppMessages paused])); - else if ([@"OneSignal#lifecycleInit" isEqualToString:call.method]) - [self lifecycleInit:call withResult:result]; - else - result(FlutterMethodNotImplemented); - +- (void)handleMethodCall:(FlutterMethodCall *)call + result:(FlutterResult)result { + if ([@"OneSignal#addTrigger" isEqualToString:call.method]) + [self addTriggers:call withResult:result]; + else if ([@"OneSignal#addTriggers" isEqualToString:call.method]) + [self addTriggers:call withResult:result]; + else if ([@"OneSignal#removeTrigger" isEqualToString:call.method]) + [self removeTrigger:call withResult:result]; + else if ([@"OneSignal#removeTriggers" isEqualToString:call.method]) + [self removeTriggers:call withResult:result]; + else if ([@"OneSignal#clearTriggers" isEqualToString:call.method]) + [self clearTriggers:call withResult:result]; + else if ([@"OneSignal#paused" isEqualToString:call.method]) + [self paused:call withResult:result]; + else if ([@"OneSignal#arePaused" isEqualToString:call.method]) + result(@([OneSignal.InAppMessages paused])); + else if ([@"OneSignal#lifecycleInit" isEqualToString:call.method]) + [self lifecycleInit:call withResult:result]; + else + result(FlutterMethodNotImplemented); } - (void)addTriggers:(FlutterMethodCall *)call withResult:(FlutterResult)result { - NSDictionary *triggers = call.arguments; - [OneSignal.InAppMessages addTriggers:triggers]; - result(nil); + NSDictionary *triggers = call.arguments; + [OneSignal.InAppMessages addTriggers:triggers]; + result(nil); } -- (void)removeTrigger:(FlutterMethodCall *)call withResult:(FlutterResult)result { - NSString *key = call.arguments; - [OneSignal.InAppMessages removeTrigger:key]; - result(nil); +- (void)removeTrigger:(FlutterMethodCall *)call + withResult:(FlutterResult)result { + NSString *key = call.arguments; + [OneSignal.InAppMessages removeTrigger:key]; + result(nil); } -- (void)removeTriggers:(FlutterMethodCall *)call withResult:(FlutterResult)result { - NSArray *keys = call.arguments; - [OneSignal.InAppMessages removeTriggers:keys]; - result(nil); +- (void)removeTriggers:(FlutterMethodCall *)call + withResult:(FlutterResult)result { + NSArray *keys = call.arguments; + [OneSignal.InAppMessages removeTriggers:keys]; + result(nil); } -- (void)clearTriggers:(FlutterMethodCall *)call withResult:(FlutterResult)result { - [OneSignal.InAppMessages clearTriggers]; - result(nil); +- (void)clearTriggers:(FlutterMethodCall *)call + withResult:(FlutterResult)result { + [OneSignal.InAppMessages clearTriggers]; + result(nil); } - (void)paused:(FlutterMethodCall *)call withResult:(FlutterResult)result { - BOOL pause = [call.arguments boolValue]; - [OneSignal.InAppMessages paused:pause]; - result(nil); + BOOL pause = [call.arguments boolValue]; + [OneSignal.InAppMessages paused:pause]; + result(nil); } -- (void)lifecycleInit:(FlutterMethodCall *)call withResult:(FlutterResult)result { - [OneSignal.InAppMessages addClickListener:OSFlutterInAppMessages.sharedInstance]; - [OneSignal.InAppMessages addLifecycleListener:OSFlutterInAppMessages.sharedInstance]; +- (void)lifecycleInit:(FlutterMethodCall *)call + withResult:(FlutterResult)result { + [OneSignal.InAppMessages + addClickListener:OSFlutterInAppMessages.sharedInstance]; + [OneSignal.InAppMessages + addLifecycleListener:OSFlutterInAppMessages.sharedInstance]; } - - #pragma mark In App Message Click -- (void)onClickInAppMessage:(OSInAppMessageClickEvent * _Nonnull)event { - [self.channel invokeMethod:@"OneSignal#onClickInAppMessage" arguments:event.toJson]; +- (void)onClickInAppMessage:(OSInAppMessageClickEvent *_Nonnull)event { + [self.channel invokeMethod:@"OneSignal#onClickInAppMessage" + arguments:event.toJson]; } #pragma mark OSInAppMessageLifecycleListener -- (void)onWillDisplayInAppMessage:(OSInAppMessageWillDisplayEvent *) event { - [self.channel invokeMethod:@"OneSignal#onWillDisplayInAppMessage" arguments:event.toJson]; +- (void)onWillDisplayInAppMessage:(OSInAppMessageWillDisplayEvent *)event { + [self.channel invokeMethod:@"OneSignal#onWillDisplayInAppMessage" + arguments:event.toJson]; } -- (void)onDidDisplayInAppMessage:(OSInAppMessageDidDisplayEvent *) event { - [self.channel invokeMethod:@"OneSignal#onDidDisplayInAppMessage" arguments:event.toJson]; +- (void)onDidDisplayInAppMessage:(OSInAppMessageDidDisplayEvent *)event { + [self.channel invokeMethod:@"OneSignal#onDidDisplayInAppMessage" + arguments:event.toJson]; } -- (void)onWillDismissInAppMessage:(OSInAppMessageWillDismissEvent *) event { - [self.channel invokeMethod:@"OneSignal#onWillDismissInAppMessage" arguments:event.toJson]; +- (void)onWillDismissInAppMessage:(OSInAppMessageWillDismissEvent *)event { + [self.channel invokeMethod:@"OneSignal#onWillDismissInAppMessage" + arguments:event.toJson]; } -- (void)onDidDismissInAppMessage:(OSInAppMessageDidDismissEvent *) event { - [self.channel invokeMethod:@"OneSignal#onDidDismissInAppMessage" arguments:event.toJson]; +- (void)onDidDismissInAppMessage:(OSInAppMessageDidDismissEvent *)event { + [self.channel invokeMethod:@"OneSignal#onDidDismissInAppMessage" + arguments:event.toJson]; } @end diff --git a/ios/Classes/OSFlutterLiveActivities.h b/ios/Classes/OSFlutterLiveActivities.h index 0e6e8208..22313079 100644 --- a/ios/Classes/OSFlutterLiveActivities.h +++ b/ios/Classes/OSFlutterLiveActivities.h @@ -13,8 +13,8 @@ * 1. The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * - * 2. All copies of substantial portions of the Software may only be used in connection - * with services provided by OneSignal. + * 2. All copies of substantial portions of the Software may only be used in + * connection with services provided by OneSignal. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, @@ -25,11 +25,11 @@ * THE SOFTWARE. */ -#import #import +#import -@interface OSFlutterLiveActivities : NSObject +@interface OSFlutterLiveActivities : NSObject -@property (strong, nonatomic) FlutterMethodChannel *channel; +@property(strong, nonatomic) FlutterMethodChannel *channel; -@end \ No newline at end of file +@end diff --git a/ios/Classes/OSFlutterLiveActivities.m b/ios/Classes/OSFlutterLiveActivities.m index b7c4a44d..aa2a1f80 100644 --- a/ios/Classes/OSFlutterLiveActivities.m +++ b/ios/Classes/OSFlutterLiveActivities.m @@ -13,8 +13,8 @@ * 1. The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * - * 2. All copies of substantial portions of the Software may only be used in connection - * with services provided by OneSignal. + * 2. All copies of substantial portions of the Software may only be used in + * connection with services provided by OneSignal. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, @@ -26,124 +26,164 @@ */ #import "OSFlutterLiveActivities.h" -#import -#import "OneSignalLiveActivities/OneSignalLiveActivities-Swift.h" #import "OSFlutterCategories.h" +#import "OneSignalLiveActivities/OneSignalLiveActivities-Swift.h" +#import @implementation OSFlutterLiveActivities -+ (void)registerWithRegistrar:(NSObject*)registrar { - OSFlutterLiveActivities *instance = [OSFlutterLiveActivities new]; ++ (void)registerWithRegistrar:(NSObject *)registrar { + OSFlutterLiveActivities *instance = [OSFlutterLiveActivities new]; - instance.channel = [FlutterMethodChannel - methodChannelWithName:@"OneSignal#liveactivities" - binaryMessenger:[registrar messenger]]; + instance.channel = + [FlutterMethodChannel methodChannelWithName:@"OneSignal#liveactivities" + binaryMessenger:[registrar messenger]]; - [registrar addMethodCallDelegate:instance channel:instance.channel]; + [registrar addMethodCallDelegate:instance channel:instance.channel]; } -- (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result { - if ([@"OneSignal#enterLiveActivity" isEqualToString:call.method]) - [self enterLiveActivity:call withResult:result]; - else if ([@"OneSignal#exitLiveActivity" isEqualToString:call.method]) - [self exitLiveActivity:call withResult:result]; - else if ([@"OneSignal#setPushToStartToken" isEqualToString:call.method]) - [self setPushToStartToken:call withResult:result]; - else if ([@"OneSignal#removePushToStartToken" isEqualToString:call.method]) - [self removePushToStartToken:call withResult:result]; - else if ([@"OneSignal#setupDefault" isEqualToString:call.method]) - [self setupDefault:call withResult:result]; - else if ([@"OneSignal#startDefault" isEqualToString:call.method]) - [self startDefault:call withResult:result]; - else - result(FlutterMethodNotImplemented); +- (void)handleMethodCall:(FlutterMethodCall *)call + result:(FlutterResult)result { + if ([@"OneSignal#enterLiveActivity" isEqualToString:call.method]) + [self enterLiveActivity:call withResult:result]; + else if ([@"OneSignal#exitLiveActivity" isEqualToString:call.method]) + [self exitLiveActivity:call withResult:result]; + else if ([@"OneSignal#setPushToStartToken" isEqualToString:call.method]) + [self setPushToStartToken:call withResult:result]; + else if ([@"OneSignal#removePushToStartToken" isEqualToString:call.method]) + [self removePushToStartToken:call withResult:result]; + else if ([@"OneSignal#setupDefault" isEqualToString:call.method]) + [self setupDefault:call withResult:result]; + else if ([@"OneSignal#startDefault" isEqualToString:call.method]) + [self startDefault:call withResult:result]; + else + result(FlutterMethodNotImplemented); } -- (void)enterLiveActivity:(FlutterMethodCall *)call withResult:(FlutterResult)result { - NSString *activityId = call.arguments[@"activityId"]; - NSString *token = call.arguments[@"token"]; +- (void)enterLiveActivity:(FlutterMethodCall *)call + withResult:(FlutterResult)result { + NSString *activityId = call.arguments[@"activityId"]; + NSString *token = call.arguments[@"token"]; - [OneSignal.LiveActivities enter:activityId withToken:token withSuccess:^(NSDictionary *results) { + [OneSignal.LiveActivities enter:activityId + withToken:token + withSuccess:^(NSDictionary *results) { result(results); - } withFailure:^(NSError *error) { + } + withFailure:^(NSError *error) { result(error.flutterError); - }]; + }]; } -- (void)exitLiveActivity:(FlutterMethodCall *)call withResult:(FlutterResult)result { - NSString *activityId = call.arguments[@"activityId"]; +- (void)exitLiveActivity:(FlutterMethodCall *)call + withResult:(FlutterResult)result { + NSString *activityId = call.arguments[@"activityId"]; - [OneSignal.LiveActivities exit:activityId withSuccess:^(NSDictionary *results) { + [OneSignal.LiveActivities exit:activityId + withSuccess:^(NSDictionary *results) { result(results); - } withFailure:^(NSError *error) { + } + withFailure:^(NSError *error) { result(error.flutterError); - }]; + }]; } -- (void)setPushToStartToken:(FlutterMethodCall *)call withResult:(FlutterResult)result { - NSString *activityType = call.arguments[@"activityType"]; - NSString *token = call.arguments[@"token"]; - NSError* err=nil; - - if (@available(iOS 17.2, *)) { - [OneSignalLiveActivitiesManagerImpl setPushToStartToken:activityType withToken:token error:&err]; - if (err) { - [OneSignalLog onesignalLog:ONE_S_LL_ERROR message:[NSString stringWithFormat:@"activityType must be the name of your ActivityAttributes struct"]]; - } - } else { - [OneSignalLog onesignalLog:ONE_S_LL_ERROR message:[NSString stringWithFormat:@"cannot setPushToStartToken on iOS < 17.2"]]; +- (void)setPushToStartToken:(FlutterMethodCall *)call + withResult:(FlutterResult)result { + NSString *activityType = call.arguments[@"activityType"]; + NSString *token = call.arguments[@"token"]; + NSError *err = nil; + + if (@available(iOS 17.2, *)) { + [OneSignalLiveActivitiesManagerImpl setPushToStartToken:activityType + withToken:token + error:&err]; + if (err) { + [OneSignalLog + onesignalLog:ONE_S_LL_ERROR + message:[NSString + stringWithFormat:@"activityType must be the name of " + @"your ActivityAttributes struct"]]; } - - result(nil); + } else { + [OneSignalLog + onesignalLog:ONE_S_LL_ERROR + message:[NSString + stringWithFormat: + @"cannot setPushToStartToken on iOS < 17.2"]]; + } + + result(nil); } -- (void)removePushToStartToken:(FlutterMethodCall *)call withResult:(FlutterResult)result { - NSString *activityType = call.arguments[@"activityType"]; - NSError* err=nil; - - if (@available(iOS 17.2, *)) { - [OneSignalLiveActivitiesManagerImpl removePushToStartToken:activityType error:&err]; - if (err) { - [OneSignalLog onesignalLog:ONE_S_LL_ERROR message:[NSString stringWithFormat:@"activityType must be the name of your ActivityAttributes struct"]]; - } - } else { - [OneSignalLog onesignalLog:ONE_S_LL_ERROR message:[NSString stringWithFormat:@"cannot removePushToStartToken on iOS < 17.2"]]; +- (void)removePushToStartToken:(FlutterMethodCall *)call + withResult:(FlutterResult)result { + NSString *activityType = call.arguments[@"activityType"]; + NSError *err = nil; + + if (@available(iOS 17.2, *)) { + [OneSignalLiveActivitiesManagerImpl removePushToStartToken:activityType + error:&err]; + if (err) { + [OneSignalLog + onesignalLog:ONE_S_LL_ERROR + message:[NSString + stringWithFormat:@"activityType must be the name of " + @"your ActivityAttributes struct"]]; } - - result(nil); + } else { + [OneSignalLog + onesignalLog:ONE_S_LL_ERROR + message:[NSString + stringWithFormat: + @"cannot removePushToStartToken on iOS < 17.2"]]; + } + + result(nil); } -- (void)setupDefault:(FlutterMethodCall *)call withResult:(FlutterResult)result { - NSDictionary *options = call.arguments[@"options"]; - - LiveActivitySetupOptions *laOptions = nil; - - if (options != [NSNull null]) { - laOptions = [LiveActivitySetupOptions alloc]; - [laOptions setEnablePushToStart:[options[@"enablePushToStart"] boolValue]]; - [laOptions setEnablePushToUpdate:[options[@"enablePushToUpdate"] boolValue]]; - } - - if (@available(iOS 16.1, *)) { - [OneSignalLiveActivitiesManagerImpl setupDefaultWithOptions:laOptions]; - } else { - [OneSignalLog onesignalLog:ONE_S_LL_ERROR message:[NSString stringWithFormat:@"cannot setupDefault on iOS < 16.1"]]; - } - - result(nil); +- (void)setupDefault:(FlutterMethodCall *)call + withResult:(FlutterResult)result { + NSDictionary *options = call.arguments[@"options"]; + + LiveActivitySetupOptions *laOptions = nil; + + if (options != [NSNull null]) { + laOptions = [LiveActivitySetupOptions alloc]; + [laOptions setEnablePushToStart:[options[@"enablePushToStart"] boolValue]]; + [laOptions + setEnablePushToUpdate:[options[@"enablePushToUpdate"] boolValue]]; + } + + if (@available(iOS 16.1, *)) { + [OneSignalLiveActivitiesManagerImpl setupDefaultWithOptions:laOptions]; + } else { + [OneSignalLog + onesignalLog:ONE_S_LL_ERROR + message:[NSString stringWithFormat: + @"cannot setupDefault on iOS < 16.1"]]; + } + + result(nil); } -- (void)startDefault:(FlutterMethodCall *)call withResult:(FlutterResult)result { - NSString *activityId = call.arguments[@"activityId"]; - NSDictionary *attributes = call.arguments[@"attributes"]; - NSDictionary *content = call.arguments[@"content"]; - - if (@available(iOS 16.1, *)) { - [OneSignalLiveActivitiesManagerImpl startDefault:activityId attributes:attributes content:content]; - } else { - [OneSignalLog onesignalLog:ONE_S_LL_ERROR message:[NSString stringWithFormat:@"cannot startDefault on iOS < 16.1"]]; - } - - result(nil); +- (void)startDefault:(FlutterMethodCall *)call + withResult:(FlutterResult)result { + NSString *activityId = call.arguments[@"activityId"]; + NSDictionary *attributes = call.arguments[@"attributes"]; + NSDictionary *content = call.arguments[@"content"]; + + if (@available(iOS 16.1, *)) { + [OneSignalLiveActivitiesManagerImpl startDefault:activityId + attributes:attributes + content:content]; + } else { + [OneSignalLog + onesignalLog:ONE_S_LL_ERROR + message:[NSString stringWithFormat: + @"cannot startDefault on iOS < 16.1"]]; + } + + result(nil); } @end diff --git a/ios/Classes/OSFlutterLocation.h b/ios/Classes/OSFlutterLocation.h index 9ce91621..519a1d6a 100644 --- a/ios/Classes/OSFlutterLocation.h +++ b/ios/Classes/OSFlutterLocation.h @@ -13,8 +13,8 @@ * 1. The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * - * 2. All copies of substantial portions of the Software may only be used in connection - * with services provided by OneSignal. + * 2. All copies of substantial portions of the Software may only be used in + * connection with services provided by OneSignal. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, @@ -25,12 +25,11 @@ * THE SOFTWARE. */ - -#import #import +#import -@interface OSFlutterLocation : NSObject +@interface OSFlutterLocation : NSObject -@property (strong, nonatomic) FlutterMethodChannel *channel; +@property(strong, nonatomic) FlutterMethodChannel *channel; @end diff --git a/ios/Classes/OSFlutterLocation.m b/ios/Classes/OSFlutterLocation.m index 14a72710..1d0d46fd 100644 --- a/ios/Classes/OSFlutterLocation.m +++ b/ios/Classes/OSFlutterLocation.m @@ -13,8 +13,8 @@ * 1. The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * - * 2. All copies of substantial portions of the Software may only be used in connection - * with services provided by OneSignal. + * 2. All copies of substantial portions of the Software may only be used in + * connection with services provided by OneSignal. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, @@ -26,42 +26,43 @@ */ #import "OSFlutterLocation.h" -#import #import "OSFlutterCategories.h" +#import @implementation OSFlutterLocation -+ (void)registerWithRegistrar:(NSObject*)registrar { - OSFlutterLocation *instance = [OSFlutterLocation new]; ++ (void)registerWithRegistrar:(NSObject *)registrar { + OSFlutterLocation *instance = [OSFlutterLocation new]; - instance.channel = [FlutterMethodChannel - methodChannelWithName:@"OneSignal#location" - binaryMessenger:[registrar messenger]]; + instance.channel = + [FlutterMethodChannel methodChannelWithName:@"OneSignal#location" + binaryMessenger:[registrar messenger]]; - [registrar addMethodCallDelegate:instance channel:instance.channel]; + [registrar addMethodCallDelegate:instance channel:instance.channel]; } -- (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result { - if ([@"OneSignal#requestPermission" isEqualToString:call.method]) - [self requestPermission:call withResult:result]; - else if ([@"OneSignal#setShared" isEqualToString:call.method]) - [self setLocationShared:call withResult:result]; - else if ([@"OneSignal#isShared" isEqualToString:call.method]) - result(@([OneSignal.Location isShared])); - else - result(FlutterMethodNotImplemented); +- (void)handleMethodCall:(FlutterMethodCall *)call + result:(FlutterResult)result { + if ([@"OneSignal#requestPermission" isEqualToString:call.method]) + [self requestPermission:call withResult:result]; + else if ([@"OneSignal#setShared" isEqualToString:call.method]) + [self setLocationShared:call withResult:result]; + else if ([@"OneSignal#isShared" isEqualToString:call.method]) + result(@([OneSignal.Location isShared])); + else + result(FlutterMethodNotImplemented); } -- (void)setLocationShared:(FlutterMethodCall *)call withResult:(FlutterResult)result { - BOOL locationShared = [call.arguments boolValue]; - [OneSignal.Location setShared:locationShared]; - result(nil); +- (void)setLocationShared:(FlutterMethodCall *)call + withResult:(FlutterResult)result { + BOOL locationShared = [call.arguments boolValue]; + [OneSignal.Location setShared:locationShared]; + result(nil); } -- (void)requestPermission:(FlutterMethodCall *)call withResult:(FlutterResult)result { - [OneSignal.Location requestPermission]; - result(nil); +- (void)requestPermission:(FlutterMethodCall *)call + withResult:(FlutterResult)result { + [OneSignal.Location requestPermission]; + result(nil); } - - @end diff --git a/ios/Classes/OSFlutterNotifications.h b/ios/Classes/OSFlutterNotifications.h index 480efe80..255248ca 100644 --- a/ios/Classes/OSFlutterNotifications.h +++ b/ios/Classes/OSFlutterNotifications.h @@ -13,8 +13,8 @@ * 1. The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * - * 2. All copies of substantial portions of the Software may only be used in connection - * with services provided by OneSignal. + * 2. All copies of substantial portions of the Software may only be used in + * connection with services provided by OneSignal. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, @@ -25,16 +25,17 @@ * THE SOFTWARE. */ - -#import #import +#import #import -@interface OSFlutterNotifications : NSObject +@interface OSFlutterNotifications + : NSObject -@property (strong, nonatomic) FlutterMethodChannel *channel; +@property(strong, nonatomic) FlutterMethodChannel *channel; + (instancetype)sharedInstance; -@property (strong, nonatomic) NSMutableDictionary* onWillDisplayEventCache; -@property (strong, nonatomic) NSMutableDictionary* preventedDefaultCache; +@property(strong, nonatomic) NSMutableDictionary *onWillDisplayEventCache; +@property(strong, nonatomic) NSMutableDictionary *preventedDefaultCache; @end diff --git a/ios/Classes/OSFlutterNotifications.m b/ios/Classes/OSFlutterNotifications.m index 9aa4beaa..242ee4b4 100644 --- a/ios/Classes/OSFlutterNotifications.m +++ b/ios/Classes/OSFlutterNotifications.m @@ -13,8 +13,8 @@ * 1. The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * - * 2. All copies of substantial portions of the Software may only be used in connection - * with services provided by OneSignal. + * 2. All copies of substantial portions of the Software may only be used in + * connection with services provided by OneSignal. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, @@ -26,154 +26,196 @@ */ #import "OSFlutterNotifications.h" +#import "OSFlutterCategories.h" #import #import -#import "OSFlutterCategories.h" @implementation OSFlutterNotifications + (instancetype)sharedInstance { - static OSFlutterNotifications *sharedInstance = nil; - static dispatch_once_t onceToken; - dispatch_once(&onceToken, ^{ - sharedInstance = [OSFlutterNotifications new]; - sharedInstance.onWillDisplayEventCache = [NSMutableDictionary new]; - sharedInstance.preventedDefaultCache = [NSMutableDictionary new]; - }); - return sharedInstance; + static OSFlutterNotifications *sharedInstance = nil; + static dispatch_once_t onceToken; + dispatch_once(&onceToken, ^{ + sharedInstance = [OSFlutterNotifications new]; + sharedInstance.onWillDisplayEventCache = [NSMutableDictionary new]; + sharedInstance.preventedDefaultCache = [NSMutableDictionary new]; + }); + return sharedInstance; } -+ (void)registerWithRegistrar:(NSObject*)registrar { ++ (void)registerWithRegistrar:(NSObject *)registrar { - OSFlutterNotifications.sharedInstance.channel = [FlutterMethodChannel - methodChannelWithName:@"OneSignal#notifications" - binaryMessenger:[registrar messenger]]; + OSFlutterNotifications.sharedInstance.channel = + [FlutterMethodChannel methodChannelWithName:@"OneSignal#notifications" + binaryMessenger:[registrar messenger]]; - [registrar addMethodCallDelegate:OSFlutterNotifications.sharedInstance channel:OSFlutterNotifications.sharedInstance.channel]; + [registrar + addMethodCallDelegate:OSFlutterNotifications.sharedInstance + channel:OSFlutterNotifications.sharedInstance.channel]; } -- (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result { - if ([@"OneSignal#permission" isEqualToString:call.method]) - result(@([OneSignal.Notifications permission])); - else if ([@"OneSignal#permissionNative" isEqualToString:call.method]) - [self permissionNative:call withResult:result]; - else if ([@"OneSignal#canRequest" isEqualToString:call.method]) - result(@([OneSignal.Notifications canRequestPermission])); - else if ([@"OneSignal#clearAll" isEqualToString:call.method]) - [self clearAll:call withResult:result]; - else if ([@"OneSignal#requestPermission" isEqualToString:call.method]) - [self requestPermission:call withResult:result]; - else if ([@"OneSignal#registerForProvisionalAuthorization" isEqualToString:call.method]) - [self registerForProvisionalAuthorization:call withResult:result]; - else if ([@"OneSignal#displayNotification" isEqualToString:call.method]) - [self displayNotification:call withResult:result]; - else if ([@"OneSignal#preventDefault" isEqualToString:call.method]) - [self preventDefault:call withResult:result]; - else if ([@"OneSignal#lifecycleInit" isEqualToString:call.method]) - [self lifecycleInit:call withResult:result]; - else if ([@"OneSignal#proceedWithWillDisplay" isEqualToString:call.method]) - [self proceedWithWillDisplay:call withResult:result]; - else if ([@"OneSignal#addNativeClickListener" isEqualToString:call.method]) - [self registerClickListener:call withResult:result]; - else - result(FlutterMethodNotImplemented); +- (void)handleMethodCall:(FlutterMethodCall *)call + result:(FlutterResult)result { + if ([@"OneSignal#permission" isEqualToString:call.method]) + result(@([OneSignal.Notifications permission])); + else if ([@"OneSignal#permissionNative" isEqualToString:call.method]) + [self permissionNative:call withResult:result]; + else if ([@"OneSignal#canRequest" isEqualToString:call.method]) + result(@([OneSignal.Notifications canRequestPermission])); + else if ([@"OneSignal#clearAll" isEqualToString:call.method]) + [self clearAll:call withResult:result]; + else if ([@"OneSignal#requestPermission" isEqualToString:call.method]) + [self requestPermission:call withResult:result]; + else if ([@"OneSignal#registerForProvisionalAuthorization" + isEqualToString:call.method]) + [self registerForProvisionalAuthorization:call withResult:result]; + else if ([@"OneSignal#displayNotification" isEqualToString:call.method]) + [self displayNotification:call withResult:result]; + else if ([@"OneSignal#preventDefault" isEqualToString:call.method]) + [self preventDefault:call withResult:result]; + else if ([@"OneSignal#lifecycleInit" isEqualToString:call.method]) + [self lifecycleInit:call withResult:result]; + else if ([@"OneSignal#proceedWithWillDisplay" isEqualToString:call.method]) + [self proceedWithWillDisplay:call withResult:result]; + else if ([@"OneSignal#addNativeClickListener" isEqualToString:call.method]) + [self registerClickListener:call withResult:result]; + else + result(FlutterMethodNotImplemented); } -- (void)permissionNative:(FlutterMethodCall *)call withResult:(FlutterResult)result { - OSNotificationPermission permission = [OneSignal.Notifications permissionNative]; - result(@((int)permission)); +- (void)permissionNative:(FlutterMethodCall *)call + withResult:(FlutterResult)result { + OSNotificationPermission permission = + [OneSignal.Notifications permissionNative]; + result(@((int)permission)); } -- (void)clearAll:(FlutterMethodCall *)call withResult:(FlutterResult)result { - [OneSignal.Notifications clearAll]; - result(nil); +- (void)clearAll:(FlutterMethodCall *)call withResult:(FlutterResult)result { + [OneSignal.Notifications clearAll]; + result(nil); } -- (void)requestPermission:(FlutterMethodCall *)call withResult:(FlutterResult)result { - BOOL fallbackToSettings = [call.arguments[@"fallbackToSettings"] boolValue]; - - [OneSignal.Notifications requestPermission:^(BOOL accepted) { - result(@(accepted)); - } fallbackToSettings:fallbackToSettings]; +- (void)requestPermission:(FlutterMethodCall *)call + withResult:(FlutterResult)result { + BOOL fallbackToSettings = [call.arguments[@"fallbackToSettings"] boolValue]; + + [OneSignal.Notifications + requestPermission:^(BOOL accepted) { + result(@(accepted)); + } + fallbackToSettings:fallbackToSettings]; } -- (void)registerForProvisionalAuthorization:(FlutterMethodCall *)call withResult:(FlutterResult)result { - [OneSignal.Notifications registerForProvisionalAuthorization:^(BOOL accepted) { - result(@(accepted)); - }]; +- (void)registerForProvisionalAuthorization:(FlutterMethodCall *)call + withResult:(FlutterResult)result { + [OneSignal.Notifications + registerForProvisionalAuthorization:^(BOOL accepted) { + result(@(accepted)); + }]; } -- (void)lifecycleInit:(FlutterMethodCall *)call withResult:(FlutterResult)result { - [OneSignal.Notifications addForegroundLifecycleListener:self]; - [OneSignal.Notifications addPermissionObserver:self]; - result(nil); +- (void)lifecycleInit:(FlutterMethodCall *)call + withResult:(FlutterResult)result { + [OneSignal.Notifications addForegroundLifecycleListener:self]; + [OneSignal.Notifications addPermissionObserver:self]; + result(nil); } -- (void)registerClickListener:(FlutterMethodCall *)call withResult:(FlutterResult)result { - [OneSignal.Notifications addClickListener:self]; - result(nil); +- (void)registerClickListener:(FlutterMethodCall *)call + withResult:(FlutterResult)result { + [OneSignal.Notifications addClickListener:self]; + result(nil); } - (void)onNotificationPermissionDidChange:(BOOL)permission { - [self.channel invokeMethod:@"OneSignal#onNotificationPermissionDidChange" arguments:@{@"permission" : @(permission)}]; + [self.channel invokeMethod:@"OneSignal#onNotificationPermissionDidChange" + arguments:@{@"permission" : @(permission)}]; } #pragma mark Received in Notification Lifecycle Event - (void)onWillDisplayNotification:(OSNotificationWillDisplayEvent *)event { - self.onWillDisplayEventCache[event.notification.notificationId] = event; - /// Our bridge layer needs to preventDefault so that the Flutter listener has time to preventDefault before the notification is displayed - [event preventDefault]; - [self.channel invokeMethod:@"OneSignal#onWillDisplayNotification" arguments:event.toJson]; + self.onWillDisplayEventCache[event.notification.notificationId] = event; + /// Our bridge layer needs to preventDefault so that the Flutter listener has + /// time to preventDefault before the notification is displayed + [event preventDefault]; + [self.channel invokeMethod:@"OneSignal#onWillDisplayNotification" + arguments:event.toJson]; } - /// Our bridge layer needs to preventDefault so that the Flutter listener has time to preventDefault before the notification is displayed - /// This function is called after all of the flutter listeners have responded to the willDisplay event. - /// If any of them have called preventDefault we will not call display(). Otherwise we will display. -- (void)proceedWithWillDisplay:(FlutterMethodCall *)call withResult:(FlutterResult)result { - NSString *notificationId = call.arguments[@"notificationId"]; - OSNotificationWillDisplayEvent *event = self.onWillDisplayEventCache[notificationId]; - if (!event) { - [OneSignalLog onesignalLog:ONE_S_LL_ERROR message:[NSString stringWithFormat:@"OneSignal (objc): could not find notification will display event for notification with id: %@", notificationId]]; - return; - } - if (self.preventedDefaultCache[notificationId]) { - return; - } - [event.notification display]; - result(nil); +/// Our bridge layer needs to preventDefault so that the Flutter listener has +/// time to preventDefault before the notification is displayed This function is +/// called after all of the flutter listeners have responded to the willDisplay +/// event. If any of them have called preventDefault we will not call display(). +/// Otherwise we will display. +- (void)proceedWithWillDisplay:(FlutterMethodCall *)call + withResult:(FlutterResult)result { + NSString *notificationId = call.arguments[@"notificationId"]; + OSNotificationWillDisplayEvent *event = + self.onWillDisplayEventCache[notificationId]; + if (!event) { + [OneSignalLog + onesignalLog:ONE_S_LL_ERROR + message:[NSString + stringWithFormat: + @"OneSignal (objc): could not find notification " + @"will display event for notification with id: %@", + notificationId]]; + return; + } + if (self.preventedDefaultCache[notificationId]) { + return; + } + [event.notification display]; + result(nil); } -- (void)preventDefault:(FlutterMethodCall *)call withResult:(FlutterResult)result { - NSString *notificationId = call.arguments[@"notificationId"]; - OSNotificationWillDisplayEvent *event = self.onWillDisplayEventCache[notificationId]; - - if (!event) { - [OneSignalLog onesignalLog:ONE_S_LL_ERROR message:[NSString stringWithFormat:@"OneSignal (objc): could not find notification will display event for notification with id: %@", notificationId]]; - return; - } - [event preventDefault]; - self.preventedDefaultCache[event.notification.notificationId] = event; - result(nil); +- (void)preventDefault:(FlutterMethodCall *)call + withResult:(FlutterResult)result { + NSString *notificationId = call.arguments[@"notificationId"]; + OSNotificationWillDisplayEvent *event = + self.onWillDisplayEventCache[notificationId]; + + if (!event) { + [OneSignalLog + onesignalLog:ONE_S_LL_ERROR + message:[NSString + stringWithFormat: + @"OneSignal (objc): could not find notification " + @"will display event for notification with id: %@", + notificationId]]; + return; + } + [event preventDefault]; + self.preventedDefaultCache[event.notification.notificationId] = event; + result(nil); } -- (void)displayNotification:(FlutterMethodCall *)call withResult:(FlutterResult)result { - NSString *notificationId = call.arguments[@"notificationId"]; - OSNotificationWillDisplayEvent *event = self.onWillDisplayEventCache[notificationId]; - - if (!event) { - [OneSignalLog onesignalLog:ONE_S_LL_ERROR message:[NSString stringWithFormat:@"OneSignal (objc): could not find notification will display event for notification with id: %@", notificationId]]; - return; - } - [event.notification display]; - result(nil); +- (void)displayNotification:(FlutterMethodCall *)call + withResult:(FlutterResult)result { + NSString *notificationId = call.arguments[@"notificationId"]; + OSNotificationWillDisplayEvent *event = + self.onWillDisplayEventCache[notificationId]; + + if (!event) { + [OneSignalLog + onesignalLog:ONE_S_LL_ERROR + message:[NSString + stringWithFormat: + @"OneSignal (objc): could not find notification " + @"will display event for notification with id: %@", + notificationId]]; + return; + } + [event.notification display]; + result(nil); } #pragma mark Notification Click -- (void)onClickNotification:(OSNotificationClickEvent * _Nonnull)event { - [self.channel invokeMethod:@"OneSignal#onClickNotification" arguments:event.toJson]; +- (void)onClickNotification:(OSNotificationClickEvent *_Nonnull)event { + [self.channel invokeMethod:@"OneSignal#onClickNotification" + arguments:event.toJson]; } - @end diff --git a/ios/Classes/OSFlutterPushSubscription.h b/ios/Classes/OSFlutterPushSubscription.h index 2f9b2210..fe7c94fa 100644 --- a/ios/Classes/OSFlutterPushSubscription.h +++ b/ios/Classes/OSFlutterPushSubscription.h @@ -13,8 +13,8 @@ * 1. The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * - * 2. All copies of substantial portions of the Software may only be used in connection - * with services provided by OneSignal. + * 2. All copies of substantial portions of the Software may only be used in + * connection with services provided by OneSignal. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, @@ -25,14 +25,14 @@ * THE SOFTWARE. */ - -#import #import +#import #import -@interface OSFlutterPushSubscription : NSObject +@interface OSFlutterPushSubscription + : NSObject + (instancetype)sharedInstance; -@property (strong, nonatomic) FlutterMethodChannel *channel; +@property(strong, nonatomic) FlutterMethodChannel *channel; @end diff --git a/ios/Classes/OSFlutterPushSubscription.m b/ios/Classes/OSFlutterPushSubscription.m index 933cfc40..45b41aa2 100644 --- a/ios/Classes/OSFlutterPushSubscription.m +++ b/ios/Classes/OSFlutterPushSubscription.m @@ -13,8 +13,8 @@ * 1. The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * - * 2. All copies of substantial portions of the Software may only be used in connection - * with services provided by OneSignal. + * 2. All copies of substantial portions of the Software may only be used in + * connection with services provided by OneSignal. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, @@ -26,57 +26,60 @@ */ #import "OSFlutterPushSubscription.h" +#import "OSFlutterCategories.h" #import #import -#import "OSFlutterCategories.h" @implementation OSFlutterPushSubscription -+ (void)registerWithRegistrar:(NSObject*)registrar { - OSFlutterPushSubscription *instance = [OSFlutterPushSubscription new]; ++ (void)registerWithRegistrar:(NSObject *)registrar { + OSFlutterPushSubscription *instance = [OSFlutterPushSubscription new]; - instance.channel = [FlutterMethodChannel - methodChannelWithName:@"OneSignal#pushsubscription" - binaryMessenger:[registrar messenger]]; + instance.channel = + [FlutterMethodChannel methodChannelWithName:@"OneSignal#pushsubscription" + binaryMessenger:[registrar messenger]]; - [registrar addMethodCallDelegate:instance channel:instance.channel]; + [registrar addMethodCallDelegate:instance channel:instance.channel]; } -- (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result { - if ([@"OneSignal#pushSubscriptionId" isEqualToString:call.method]) - result(OneSignal.User.pushSubscription.id); - else if ([@"OneSignal#pushSubscriptionToken" isEqualToString:call.method]) - result(OneSignal.User.pushSubscription.token); - else if ([@"OneSignal#pushSubscriptionOptedIn" isEqualToString:call.method]) - result(@(OneSignal.User.pushSubscription.optedIn)); - else if ([@"OneSignal#optIn" isEqualToString:call.method]) - [self optIn:call withResult:result]; - else if ([@"OneSignal#optOut" isEqualToString:call.method]) - [self optOut:call withResult:result]; - else if ([@"OneSignal#lifecycleInit" isEqualToString:call.method]) - [self lifecycleInit:call withResult:result]; - else - result(FlutterMethodNotImplemented); +- (void)handleMethodCall:(FlutterMethodCall *)call + result:(FlutterResult)result { + if ([@"OneSignal#pushSubscriptionId" isEqualToString:call.method]) + result(OneSignal.User.pushSubscription.id); + else if ([@"OneSignal#pushSubscriptionToken" isEqualToString:call.method]) + result(OneSignal.User.pushSubscription.token); + else if ([@"OneSignal#pushSubscriptionOptedIn" isEqualToString:call.method]) + result(@(OneSignal.User.pushSubscription.optedIn)); + else if ([@"OneSignal#optIn" isEqualToString:call.method]) + [self optIn:call withResult:result]; + else if ([@"OneSignal#optOut" isEqualToString:call.method]) + [self optOut:call withResult:result]; + else if ([@"OneSignal#lifecycleInit" isEqualToString:call.method]) + [self lifecycleInit:call withResult:result]; + else + result(FlutterMethodNotImplemented); } - (void)optIn:(FlutterMethodCall *)call withResult:(FlutterResult)result { - [OneSignal.User.pushSubscription optIn]; - result(nil); + [OneSignal.User.pushSubscription optIn]; + result(nil); } - (void)optOut:(FlutterMethodCall *)call withResult:(FlutterResult)result { - [OneSignal.User.pushSubscription optOut]; - result(nil); + [OneSignal.User.pushSubscription optOut]; + result(nil); } -- (void)lifecycleInit:(FlutterMethodCall *)call withResult:(FlutterResult)result { - [OneSignal.User.pushSubscription addObserver:self]; - result(nil); +- (void)lifecycleInit:(FlutterMethodCall *)call + withResult:(FlutterResult)result { + [OneSignal.User.pushSubscription addObserver:self]; + result(nil); } -- (void)onPushSubscriptionDidChangeWithState:(OSPushSubscriptionChangedState *)state { - [self.channel invokeMethod:@"OneSignal#onPushSubscriptionChange" arguments:state.jsonRepresentation]; +- (void)onPushSubscriptionDidChangeWithState: + (OSPushSubscriptionChangedState *)state { + [self.channel invokeMethod:@"OneSignal#onPushSubscriptionChange" + arguments:state.jsonRepresentation]; } @end - diff --git a/ios/Classes/OSFlutterSession.h b/ios/Classes/OSFlutterSession.h index 484f95e5..0db13985 100644 --- a/ios/Classes/OSFlutterSession.h +++ b/ios/Classes/OSFlutterSession.h @@ -13,8 +13,8 @@ * 1. The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * - * 2. All copies of substantial portions of the Software may only be used in connection - * with services provided by OneSignal. + * 2. All copies of substantial portions of the Software may only be used in + * connection with services provided by OneSignal. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, @@ -25,12 +25,11 @@ * THE SOFTWARE. */ - -#import #import +#import -@interface OSFlutterSession : NSObject +@interface OSFlutterSession : NSObject -@property (strong, nonatomic) FlutterMethodChannel *channel; +@property(strong, nonatomic) FlutterMethodChannel *channel; @end diff --git a/ios/Classes/OSFlutterSession.m b/ios/Classes/OSFlutterSession.m index 9d0c33e3..a5aa511a 100644 --- a/ios/Classes/OSFlutterSession.m +++ b/ios/Classes/OSFlutterSession.m @@ -13,8 +13,8 @@ * 1. The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * - * 2. All copies of substantial portions of the Software may only be used in connection - * with services provided by OneSignal. + * 2. All copies of substantial portions of the Software may only be used in + * connection with services provided by OneSignal. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, @@ -26,50 +26,52 @@ */ #import "OSFlutterSession.h" -#import #import "OSFlutterCategories.h" +#import @implementation OSFlutterSession -+ (void)registerWithRegistrar:(NSObject*)registrar { - OSFlutterSession *instance = [OSFlutterSession new]; ++ (void)registerWithRegistrar:(NSObject *)registrar { + OSFlutterSession *instance = [OSFlutterSession new]; - instance.channel = [FlutterMethodChannel - methodChannelWithName:@"OneSignal#session" - binaryMessenger:[registrar messenger]]; + instance.channel = + [FlutterMethodChannel methodChannelWithName:@"OneSignal#session" + binaryMessenger:[registrar messenger]]; - [registrar addMethodCallDelegate:instance channel:instance.channel]; + [registrar addMethodCallDelegate:instance channel:instance.channel]; } -- (void)handleMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result { - if ([@"OneSignal#addOutcome" isEqualToString:call.method]) { - [self addOutcome:call withResult:result]; - } else if ([@"OneSignal#addUniqueOutcome" isEqualToString:call.method]) { - [self addUniqueOutcome:call withResult:result]; - } else if ([@"OneSignal#addOutcomeWithValue" isEqualToString:call.method]) { - [self addOutcomeWithValue:call withResult:result]; - } else { - result(FlutterMethodNotImplemented); - } +- (void)handleMethodCall:(FlutterMethodCall *)call + result:(FlutterResult)result { + if ([@"OneSignal#addOutcome" isEqualToString:call.method]) { + [self addOutcome:call withResult:result]; + } else if ([@"OneSignal#addUniqueOutcome" isEqualToString:call.method]) { + [self addUniqueOutcome:call withResult:result]; + } else if ([@"OneSignal#addOutcomeWithValue" isEqualToString:call.method]) { + [self addOutcomeWithValue:call withResult:result]; + } else { + result(FlutterMethodNotImplemented); + } } - (void)addOutcome:(FlutterMethodCall *)call withResult:(FlutterResult)result { - NSString *name = call.arguments; - [OneSignal.Session addOutcome:name]; - result(nil); + NSString *name = call.arguments; + [OneSignal.Session addOutcome:name]; + result(nil); } -- (void)addUniqueOutcome:(FlutterMethodCall *)call withResult:(FlutterResult)result { - NSString *name = call.arguments; - [OneSignal.Session addUniqueOutcome:name]; - result(nil); +- (void)addUniqueOutcome:(FlutterMethodCall *)call + withResult:(FlutterResult)result { + NSString *name = call.arguments; + [OneSignal.Session addUniqueOutcome:name]; + result(nil); } -- (void)addOutcomeWithValue:(FlutterMethodCall *)call withResult:(FlutterResult)result { - NSString *name = call.arguments[@"outcome_name"]; - NSNumber *value = call.arguments[@"outcome_value"]; - [OneSignal.Session addOutcomeWithValue:name value:value]; - result(nil); +- (void)addOutcomeWithValue:(FlutterMethodCall *)call + withResult:(FlutterResult)result { + NSString *name = call.arguments[@"outcome_name"]; + NSNumber *value = call.arguments[@"outcome_value"]; + [OneSignal.Session addOutcomeWithValue:name value:value]; + result(nil); } - @end diff --git a/ios/Classes/OSFlutterUser.h b/ios/Classes/OSFlutterUser.h index 6cbaf533..67042f59 100644 --- a/ios/Classes/OSFlutterUser.h +++ b/ios/Classes/OSFlutterUser.h @@ -13,8 +13,8 @@ * 1. The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * - * 2. All copies of substantial portions of the Software may only be used in connection - * with services provided by OneSignal. + * 2. All copies of substantial portions of the Software may only be used in + * connection with services provided by OneSignal. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, @@ -25,12 +25,11 @@ * THE SOFTWARE. */ - -#import #import +#import -@interface OSFlutterUser : NSObject +@interface OSFlutterUser : NSObject -@property (strong, nonatomic) FlutterMethodChannel *channel; +@property(strong, nonatomic) FlutterMethodChannel *channel; @end diff --git a/ios/Classes/OneSignalPlugin.h b/ios/Classes/OneSignalPlugin.h index 652f1f0f..5b3fcb1e 100644 --- a/ios/Classes/OneSignalPlugin.h +++ b/ios/Classes/OneSignalPlugin.h @@ -13,8 +13,8 @@ * 1. The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * - * 2. All copies of substantial portions of the Software may only be used in connection - * with services provided by OneSignal. + * 2. All copies of substantial portions of the Software may only be used in + * connection with services provided by OneSignal. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, @@ -28,7 +28,7 @@ #import #import -@interface OneSignalPlugin : NSObject +@interface OneSignalPlugin : NSObject // Do NOT initialize instances of this class. // You must only reference the shared instance. diff --git a/ios/Classes/OneSignalPlugin.m b/ios/Classes/OneSignalPlugin.m index a3046364..9309a8b7 100644 --- a/ios/Classes/OneSignalPlugin.m +++ b/ios/Classes/OneSignalPlugin.m @@ -13,8 +13,8 @@ * 1. The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * - * 2. All copies of substantial portions of the Software may only be used in connection - * with services provided by OneSignal. + * 2. All copies of substantial portions of the Software may only be used in + * connection with services provided by OneSignal. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, @@ -28,98 +28,101 @@ #import "OneSignalPlugin.h" #import "OSFlutterCategories.h" #import "OSFlutterDebug.h" -#import "OSFlutterUser.h" -#import "OSFlutterNotifications.h" -#import "OSFlutterSession.h" -#import "OSFlutterLocation.h" #import "OSFlutterInAppMessages.h" #import "OSFlutterLiveActivities.h" - +#import "OSFlutterLocation.h" +#import "OSFlutterNotifications.h" +#import "OSFlutterSession.h" +#import "OSFlutterUser.h" @interface OneSignalPlugin () -@property (strong, nonatomic) FlutterMethodChannel *channel; +@property(strong, nonatomic) FlutterMethodChannel *channel; @end @implementation OneSignalPlugin + (instancetype)sharedInstance { - static OneSignalPlugin *sharedInstance = nil; - static dispatch_once_t onceToken; - dispatch_once(&onceToken, ^{ - sharedInstance = [OneSignalPlugin new]; - }); - return sharedInstance; + static OneSignalPlugin *sharedInstance = nil; + static dispatch_once_t onceToken; + dispatch_once(&onceToken, ^{ + sharedInstance = [OneSignalPlugin new]; + }); + return sharedInstance; } #pragma mark FlutterPlugin -+ (void)registerWithRegistrar:(NSObject*)registrar { - - OneSignalWrapper.sdkType = @"flutter"; - OneSignalWrapper.sdkVersion = @"050305"; - [OneSignal initialize:nil withLaunchOptions:nil]; - - OneSignalPlugin.sharedInstance.channel = [FlutterMethodChannel - methodChannelWithName:@"OneSignal" - binaryMessenger:[registrar messenger]]; - - [registrar addMethodCallDelegate:OneSignalPlugin.sharedInstance channel:OneSignalPlugin.sharedInstance.channel]; - [OSFlutterDebug registerWithRegistrar:registrar]; - [OSFlutterUser registerWithRegistrar:registrar]; - [OSFlutterNotifications registerWithRegistrar:registrar]; - [OSFlutterSession registerWithRegistrar:registrar]; - [OSFlutterLocation registerWithRegistrar:registrar]; - [OSFlutterInAppMessages registerWithRegistrar:registrar]; - [OSFlutterLiveActivities registerWithRegistrar:registrar]; ++ (void)registerWithRegistrar:(NSObject *)registrar { + + OneSignalWrapper.sdkType = @"flutter"; + OneSignalWrapper.sdkVersion = @"050305"; + [OneSignal initialize:nil withLaunchOptions:nil]; + + OneSignalPlugin.sharedInstance.channel = + [FlutterMethodChannel methodChannelWithName:@"OneSignal" + binaryMessenger:[registrar messenger]]; + + [registrar addMethodCallDelegate:OneSignalPlugin.sharedInstance + channel:OneSignalPlugin.sharedInstance.channel]; + [OSFlutterDebug registerWithRegistrar:registrar]; + [OSFlutterUser registerWithRegistrar:registrar]; + [OSFlutterNotifications registerWithRegistrar:registrar]; + [OSFlutterSession registerWithRegistrar:registrar]; + [OSFlutterLocation registerWithRegistrar:registrar]; + [OSFlutterInAppMessages registerWithRegistrar:registrar]; + [OSFlutterLiveActivities registerWithRegistrar:registrar]; } -- (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result { - if ([@"OneSignal#initialize" isEqualToString:call.method]) - [self initialize:call withResult:result]; - else if ([@"OneSignal#login" isEqualToString:call.method]) - [self login:call withResult:result]; - else if ([@"OneSignal#logout" isEqualToString:call.method]) - [self logout:call withResult:result]; - else if ([@"OneSignal#consentRequired" isEqualToString:call.method]) - [self setConsentRequired:call withResult:result]; - else if ([@"OneSignal#consentGiven" isEqualToString:call.method]) - [self setConsentGiven:call withResult:result]; - else - result(FlutterMethodNotImplemented); +- (void)handleMethodCall:(FlutterMethodCall *)call + result:(FlutterResult)result { + if ([@"OneSignal#initialize" isEqualToString:call.method]) + [self initialize:call withResult:result]; + else if ([@"OneSignal#login" isEqualToString:call.method]) + [self login:call withResult:result]; + else if ([@"OneSignal#logout" isEqualToString:call.method]) + [self logout:call withResult:result]; + else if ([@"OneSignal#consentRequired" isEqualToString:call.method]) + [self setConsentRequired:call withResult:result]; + else if ([@"OneSignal#consentGiven" isEqualToString:call.method]) + [self setConsentGiven:call withResult:result]; + else + result(FlutterMethodNotImplemented); } #pragma mark Init -- (void)initialize:(FlutterMethodCall *)call withResult:(FlutterResult)result{ - [OneSignal initialize:call.arguments[@"appId"] withLaunchOptions:nil]; - result(nil); +- (void)initialize:(FlutterMethodCall *)call withResult:(FlutterResult)result { + [OneSignal initialize:call.arguments[@"appId"] withLaunchOptions:nil]; + result(nil); } #pragma mark Login Logout -- (void)login:(FlutterMethodCall *)call withResult:(FlutterResult)result{ - [OneSignal login:call.arguments[@"externalId"]]; - result(nil); +- (void)login:(FlutterMethodCall *)call withResult:(FlutterResult)result { + [OneSignal login:call.arguments[@"externalId"]]; + result(nil); } -- (void)logout:(FlutterMethodCall *)call withResult:(FlutterResult)result{ - [OneSignal logout]; - result(nil); +- (void)logout:(FlutterMethodCall *)call withResult:(FlutterResult)result { + [OneSignal logout]; + result(nil); } #pragma mark Privacy Consent -- (void)setConsentGiven:(FlutterMethodCall *)call withResult:(FlutterResult)result{ - BOOL granted = [call.arguments[@"granted"] boolValue]; - [OneSignal setConsentGiven:granted]; - result(nil); +- (void)setConsentGiven:(FlutterMethodCall *)call + withResult:(FlutterResult)result { + BOOL granted = [call.arguments[@"granted"] boolValue]; + [OneSignal setConsentGiven:granted]; + result(nil); } -- (void)setConsentRequired:(FlutterMethodCall *)call withResult:(FlutterResult)result{ - BOOL required = [call.arguments[@"required"] boolValue]; - [OneSignal setConsentRequired:required]; - result(nil); +- (void)setConsentRequired:(FlutterMethodCall *)call + withResult:(FlutterResult)result { + BOOL required = [call.arguments[@"required"] boolValue]; + [OneSignal setConsentRequired:required]; + result(nil); } @end diff --git a/pubspec.yaml b/pubspec.yaml index 2dcf8eb0..18188755 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -3,8 +3,12 @@ description: OneSignal is a free push notification service for mobile apps. This version: 5.3.5 homepage: https://github.com/OneSignal/OneSignal-Flutter-SDK +# Uses rps package for scripts +# E.g.`rps test` to run tests scripts: test: flutter test --coverage && dart run dlcov -c 95 --log=0 --include-untested-files=true + format-check: dart format --set-exit-if-changed --output=none . && android/gradlew spotlessCheck + format: dart format . && android/gradlew spotlessApply flutter: plugin: