diff --git a/Examples/OneSignalDemo/app/src/main/java/com/onesignal/sdktest/application/MainApplication.java b/Examples/OneSignalDemo/app/src/main/java/com/onesignal/sdktest/application/MainApplication.java index 7c48f4b070..3050d96fc4 100644 --- a/Examples/OneSignalDemo/app/src/main/java/com/onesignal/sdktest/application/MainApplication.java +++ b/Examples/OneSignalDemo/app/src/main/java/com/onesignal/sdktest/application/MainApplication.java @@ -1,12 +1,13 @@ package com.onesignal.sdktest.application; +import android.annotation.SuppressLint; import android.os.StrictMode; import android.util.Log; - import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.multidex.MultiDexApplication; +import com.onesignal.Continue; import com.onesignal.OneSignal; import com.onesignal.inAppMessages.IInAppMessageClickListener; import com.onesignal.inAppMessages.IInAppMessageClickEvent; @@ -28,8 +29,10 @@ import com.onesignal.user.state.IUserStateObserver; import com.onesignal.user.state.UserChangedState; import com.onesignal.user.state.UserState; - import org.json.JSONObject; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; public class MainApplication extends MultiDexApplication { private static final int SLEEP_TIME_TO_MIMIC_ASYNC_OPERATION = 2000; @@ -40,6 +43,7 @@ public MainApplication() { StrictMode.enableDefaults(); } + @SuppressLint("NewApi") @Override public void onCreate() { super.onCreate(); @@ -54,8 +58,18 @@ public void onCreate() { } OneSignalNotificationSender.setAppId(appId); + OneSignal.initWithContext(this, appId); + // Ensure calling requestPermission in a thread right after initWithContext does not crash + // This will reproduce result similar to Kotlin CouroutineScope.launch{}, which may potentially crash the app + ExecutorService executor = Executors.newSingleThreadExecutor(); + @SuppressLint({"NewApi", "LocalSuppress"}) CompletableFuture future = CompletableFuture.runAsync(() -> { + OneSignal.getNotifications().requestPermission(true, Continue.none()); + }, executor); + future.join(); // Waits for the task to complete + executor.shutdown(); + OneSignal.getInAppMessages().addLifecycleListener(new IInAppMessageLifecycleListener() { @Override public void onWillDisplay(@NonNull IInAppMessageWillDisplayEvent event) { diff --git a/OneSignalSDK/onesignal/core/src/main/java/com/onesignal/core/CoreModule.kt b/OneSignalSDK/onesignal/core/src/main/java/com/onesignal/core/CoreModule.kt index efc06d54a4..6d36caaf9f 100644 --- a/OneSignalSDK/onesignal/core/src/main/java/com/onesignal/core/CoreModule.kt +++ b/OneSignalSDK/onesignal/core/src/main/java/com/onesignal/core/CoreModule.kt @@ -32,7 +32,6 @@ import com.onesignal.core.internal.preferences.impl.PreferencesService import com.onesignal.core.internal.purchases.impl.TrackAmazonPurchase import com.onesignal.core.internal.purchases.impl.TrackGooglePurchase import com.onesignal.core.internal.startup.IStartableService -import com.onesignal.core.internal.startup.StartupService import com.onesignal.core.internal.time.ITime import com.onesignal.core.internal.time.impl.Time import com.onesignal.inAppMessages.IInAppMessagesManager @@ -54,7 +53,6 @@ internal class CoreModule : IModule { builder.register().provides() builder.register